fix(artifacts): commit package listings only after traversal (#13339)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 03:16:51 -05:00
1 parent 7b6e816b0b
commit 667fa3d646
2 files changed
+114 -148

No files matched your search

+92 -126
View File
@@ -1913,31 +1913,31 @@ describe("ReleaseExposureReader nested packages in a host", () => {
});
});
describe("ReleaseExposureReader packaged probe allowance", () => {
const withPostInventoryArchive = async <T>(
archive: string,
wrap: (reader: ArtifactReader) => ArtifactReader,
run: () => Promise<T>,
): Promise<T> => {
const actual = await vi.importActual<
typeof import("./inventory/ArtifactInventory.js")
>("./inventory/ArtifactInventory.js");
let inventoried = false;
inventoryRead.impl = async (...args) => {
const inventory = await actual.inventoryArtifactInScope(...args);
inventoried = true;
return inventory;
};
createdReaders.wrap = (reader, path) =>
path === archive && inventoried ? wrap(reader) : reader;
try {
return await run();
} finally {
inventoryRead.impl = undefined;
createdReaders.wrap = undefined;
}
const withPostInventoryArchive = async <T>(
archive: string,
wrap: (reader: ArtifactReader) => ArtifactReader,
run: () => Promise<T>,
): Promise<T> => {
const actual = await vi.importActual<
typeof import("./inventory/ArtifactInventory.js")
>("./inventory/ArtifactInventory.js");
let inventoried = false;
inventoryRead.impl = async (...args) => {
const inventory = await actual.inventoryArtifactInScope(...args);
inventoried = true;
return inventory;
};
createdReaders.wrap = (reader, path) =>
path === archive && inventoried ? wrap(reader) : reader;
try {
return await run();
} finally {
inventoryRead.impl = undefined;
createdReaders.wrap = undefined;
}
};
describe("ReleaseExposureReader packaged probe allowance", () => {
it("keeps probing after a zero-byte unread packaged image", async () => {
const root = await createTestTempDirectory("rea-release-unread-probe-");
const broken = Buffer.alloc(8192);
@@ -4038,41 +4038,17 @@ describe("ReleaseExposureReader cleanup-bearing read failures", () => {
{ reason: "close failed", resources: [resource] },
);
it("does not read a cleanup-incomplete limit failure as an oversized member", async () => {
const root = await createTestTempDirectory("rea-release-exposure-limit-");
await writeFile(join(root, "bundle.map"), "{}");
const real = stableRead.impl;
stableRead.impl = async (...args) => {
if (args[0].endsWith("bundle.map"))
throw cleanupFailure("limit", args[0]);
return (real as NonNullable<typeof real>)(...args);
};
const reader = new ReleaseExposureReader();
try {
const result = await reader.inspect(
{ path: root },
undefined,
ignoreText,
);
if (result.ok) throw new Error("expected cleanup-incomplete failure");
expect(result.error.cleanupIncomplete).toBe(true);
expect(result.error.cleanupResources.join("\n")).toContain("bundle.map");
} finally {
stableRead.impl = real;
await reader.close();
}
});
it("reports what it found before a member's cleanup failed", async () => {
it("preserves partial findings and the resource when a limited member fails cleanup", async () => {
const root = await createTestTempDirectory("rea-release-exposure-stop-");
await writeFile(
join(root, "a.js.map"),
JSON.stringify({ version: 3, sources: ["a.ts"], mappings: "" }),
);
await writeFile(join(root, "z.map"), "{}");
const member = join(root, "z.map");
await writeFile(member, "{}");
const real = stableRead.impl;
stableRead.impl = async (...args) => {
if (args[0].endsWith("z.map")) throw cleanupFailure("limit", args[0]);
if (args[0] === member) throw cleanupFailure("limit", member);
return (real as NonNullable<typeof real>)(...args);
};
const reader = new ReleaseExposureReader();
@@ -4086,6 +4062,7 @@ describe("ReleaseExposureReader cleanup-bearing read failures", () => {
);
if (result.ok) throw new Error("expected cleanup-incomplete failure");
expect(result.error.cleanupIncomplete).toBe(true);
expect(result.error.cleanupResources).toContain(member);
expect(interrupted).toHaveLength(1);
expect(interrupted[0]?.coverage).toBe("unknown");
expect(interrupted[0]?.source_maps.without_sources).toBe(1);
@@ -4199,87 +4176,76 @@ const sourceMapArchive = (): Buffer => {
};
describe("ReleaseExposureReader same-name members", () => {
it("scans every same-name package member instead of the last one twice", async () => {
const root = await createTestTempDirectory("rea-release-same-name-");
const archive = join(root, "app.asar");
await writeFile(
archive,
asarArchive([{ name: "config.txt", bytes: Buffer.from("x") }]),
);
const contents = new Map([
["first", "marker"],
["second", "harmless"],
]);
createdReaders.wrap = (source) => {
const list = source.entries.bind(source);
source.entries = async function* (signal) {
for await (const entry of list(signal)) {
if (entry.path !== "config.txt") {
yield entry;
continue;
}
for (const adapterKey of contents.keys())
yield { ...entry, adapterKey };
}
};
source.open = (entry) =>
Promise.resolve(Readable.from([contents.get(entry.adapterKey) ?? ""]));
return source;
};
try {
it.each(["complete", "interrupted"] as const)(
"keeps same-name member identity with %s archive listing",
async (listing) => {
const root = await createTestTempDirectory(
"rea-release-duplicate-members-",
);
const file = join(root, "app");
await writeFile(
file,
cArchiveExecutable({
layout: "2.1",
pythonVersion: 310,
members: ["", "first body", "second body"].map((body) => ({
name: "config.txt",
typeCode: "x",
compress: body !== "",
payload: Buffer.from(body),
})),
}),
);
const reader = new ReleaseExposureReader();
const texts: string[] = [];
const result = await reader.inspect(
{ path: archive },
undefined,
(text) => {
const inspect = () =>
reader.inspect({ path: file }, undefined, (text) => {
texts.push(text);
},
);
await reader.close();
if (!result.ok) throw result.error;
expect(texts).toEqual(expect.arrayContaining(["harmless", "marker"]));
} finally {
createdReaders.wrap = undefined;
}
});
});
try {
let shouldInterrupt = true;
const result =
listing === "complete"
? await inspect()
: await withPostInventoryArchive(
file,
(source) => {
const entries = source.entries.bind(source);
source.entries = async function* (signal, options) {
for await (const entry of entries(signal, options)) {
yield entry;
if (shouldInterrupt && entry.path === "config.txt") {
shouldInterrupt = false;
throw new Error("interrupted archive listing");
}
}
};
return source;
},
inspect,
);
if (!result.ok) throw result.error;
expect(texts).toEqual(
listing === "complete"
? ["", "first body", "second body"]
: ["", "second body"],
);
expect(result.value.coverage).toBe(
listing === "complete" ? "complete" : "unknown",
);
expect(result.value.text_members).toMatchObject(
listing === "complete"
? { scanned: 3, skipped: 0 }
: { scanned: 2, skipped: 1 },
);
} finally {
await reader.close();
}
},
);
});
describe("ReleaseExposureReader member identity", () => {
it("reads a compressed same-name member after an empty stored one", async () => {
const root = await createTestTempDirectory("rea-release-empty-first-");
const file = join(root, "app");
await writeFile(
file,
cArchiveExecutable({
layout: "2.1",
pythonVersion: 310,
members: [
{
name: "config.txt",
typeCode: "x",
compress: false,
payload: Buffer.alloc(0),
},
{
name: "config.txt",
typeCode: "x",
compress: true,
payload: Buffer.from("credential-marker"),
},
],
}),
);
const reader = new ReleaseExposureReader();
const texts: string[] = [];
const result = await reader.inspect({ path: file }, undefined, (text) => {
texts.push(text);
});
await reader.close();
if (!result.ok) throw result.error;
expect(texts).toContain("credential-marker");
});
it.each([
["app.js", "debug.json", { embedded: 1, skipped: 0 }],
["z.js", "a.json", { embedded: 0, skipped: 1 }],
+22 -22
View File
@@ -257,13 +257,17 @@ const namedMapReference = (
interface OpenContainer {
readonly path: string;
readonly reader: ArtifactReader;
entries: readonly ArtifactEntry[] | null;
listing: ContainerListing | null;
}
interface ContainerListing {
readonly entries: readonly ArtifactEntry[];
/**
* Entries by container-relative logical path, in listing order. Inventory
* numbers same-path occurrences in that order, so an occurrence's duplicate
* ordinal selects its entry no matter which read path reaches it first.
*/
readonly byPath: Map<string, ArtifactEntry[]>;
readonly byPath: ReadonlyMap<string, readonly ArtifactEntry[]>;
}
/** Read-only inventory and bounded member scan for one shipped artifact. */
@@ -3035,7 +3039,7 @@ const scanUnexpandedContainer = async (
containers: Map<string, OpenContainer>,
state: ScanState,
): Promise<void> => {
let entries: readonly ArtifactEntry[];
let listing: ContainerListing;
try {
const container = await openContainer(
containers,
@@ -3052,13 +3056,8 @@ const scanUnexpandedContainer = async (
noteUnexaminedPackage(state);
return;
}
entries = await containerEntries(
container,
state,
input.signal,
(entry) => {
noteUnlistedEntry(state, entry);
},
listing = await listContainer(container, state, input.signal, (entry) =>
noteUnlistedEntry(state, entry),
);
} catch (cause) {
rethrowReadFailure(cause);
@@ -3066,7 +3065,7 @@ const scanUnexpandedContainer = async (
pushLimitation(state, `Could not list ${filePath}.`);
return;
}
for (const entry of entries) {
for (const entry of listing.entries) {
if (input.signal?.aborted) throw cancelledBy(OPERATION, input.signal);
const location = `${filePath}/${entry.path}`;
if (hasCompanionName(entry.path)) state.companionPaths.add(location);
@@ -3187,8 +3186,8 @@ const readContainerMember = async (
input.signal,
);
if (container === null) return { kind: "unread" };
await containerEntries(container, state, input.signal);
const entry = container.byPath.get(entryPath(occurrence, parent))?.[
const { byPath } = await listContainer(container, state, input.signal);
const entry = byPath.get(entryPath(occurrence, parent))?.[
ordinals.get(occurrence.occurrence_id) ?? 0
];
if (entry === undefined || entry.kind !== "file") return { kind: "unread" };
@@ -3243,8 +3242,7 @@ const openContainer = async (
const container: OpenContainer = {
path: filePath,
reader,
entries: null,
byPath: new Map(),
listing: null,
};
containers.set(id, container);
if (expectedSha256 !== undefined)
@@ -3252,14 +3250,15 @@ const openContainer = async (
return container;
};
const containerEntries = async (
const listContainer = async (
container: OpenContainer,
state: ScanState,
signal: AbortSignal | undefined,
onPastCap?: (entry: ArtifactEntry) => void,
): Promise<readonly ArtifactEntry[]> => {
if (container.entries !== null) return container.entries;
): Promise<ContainerListing> => {
if (container.listing !== null) return container.listing;
const listed: ArtifactEntry[] = [];
const byPath = new Map<string, ArtifactEntry[]>();
const traversal = new ArtifactTraversalObservation();
let count = 0;
let truncated = false;
@@ -3279,16 +3278,17 @@ const containerEntries = async (
}
listed.push(entry);
const key = logicalPathNfc(entry.path).replace(/\/+$/u, "");
const sameName = container.byPath.get(key);
if (sameName === undefined) container.byPath.set(key, [entry]);
const sameName = byPath.get(key);
if (sameName === undefined) byPath.set(key, [entry]);
else sameName.push(entry);
}
const coverage = traversal.snapshot();
if (coverage.status === "unknown") state.coverageUnknown = true;
for (const limitation of coverage.limitations)
pushLimitation(state, limitation);
container.entries = listed;
return listed;
const listing = { entries: listed, byPath };
container.listing = listing;
return listing;
};
const readEntryBytes = async (