fix(artifacts): share the examined-entry allowance across readers (#13479)

* test(artifacts): strengthen path and wide traversal checks

* fix(artifacts): share the examined-entry allowance across readers
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 16:50:45 +00:00
1 parent 230105c681
commit 9560457196
5 files changed
+182 -91

No files matched your search

@@ -68,6 +68,79 @@ it.skipIf(process.platform === "win32")(
},
);
it("shares the examined-entry allowance across directory and embedded archive traversal", async () => {
const root = await createTestTempDirectory("rea-inventory-shared-budget-");
const archive = asarHeaderBytes(
'{"files":{"main.js":{"size":0,"offset":"0"}}}',
);
await writeFile(join(root, "app.asar"), archive);
await writeFile(join(root, "sibling.txt"), "ordinary input");
const options = {
acquisitionLimits: { maximumEntries: 2, maximumBytes: 2_048 },
};
await expect(
inventoryArtifact(root, {
acquisitionLimits: { ...options.acquisitionLimits, maximumEntries: NaN },
}),
).rejects.toMatchObject({ _tag: "InternalInvariantError" });
const bounded = await inventoryArtifact(root, options);
expect(bounded.coverage).toEqual({
scope: "container-entries",
status: "unknown",
});
expect(bounded.occurrences).toHaveLength(3); // The root does not consume a member slot.
expect(bounded.limitations).toEqual(
expect.arrayContaining([
expect.stringContaining("acquisition allowance exhausted"),
]),
);
await rm(join(root, "sibling.txt"));
const exact = await inventoryArtifact(root, options);
expect(exact.coverage).toEqual({
scope: "container-entries",
status: "complete",
});
expect(
exact.occurrences.map(({ logical_path }) => logical_path).sort(),
).toEqual([".", "app.asar", "app.asar/main.js"]);
});
it.skipIf(process.platform === "win32")(
"charges an omitted directory entry to the inventory allowance shared with embedded members",
async () => {
const root = await createTestTempDirectory("rea-inventory-omitted-budget-");
await writeFile(
join(root, "app.asar"),
asarHeaderBytes('{"files":{"main.js":{"size":0,"offset":"0"}}}'),
);
await promisify(execFile)("mkfifo", [join(root, "pipe")]);
const bounded = await inventoryArtifact(root, {
acquisitionLimits: { maximumEntries: 2, maximumBytes: 2_048 },
});
expect(bounded.coverage).toEqual({
scope: "container-entries",
status: "unknown",
});
const sawMember = bounded.occurrences.some(
({ logical_path }) => logical_path === "app.asar/main.js",
);
const sawPipe = bounded.limitations.some((message) =>
message.includes('omitted fifo at "pipe"'),
);
// Native listings can put either child first. Only one can use the budget
// slot that remains after admitting the archive itself.
expect(Number(sawMember) + Number(sawPipe)).toBe(1);
expect(bounded.limitations).toEqual(
expect.arrayContaining([
expect.stringContaining("acquisition allowance exhausted"),
]),
);
},
);
it("rejects a root path replaced between its initial stat and open", async () => {
const root = await createTestTempDirectory("rea-artifact-root-replaced-");
const path = join(root, "input.bin");
@@ -1,4 +1,4 @@
import { mkdir } from "node:fs/promises";
import { mkdir, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
@@ -24,7 +24,7 @@ const missingChildren = (paths: Iterable<string>): string[] => {
const traverse = async (
root: string,
maximumEntries: number,
stopAfter?: { readonly entries: number; readonly reason: Error },
onEntry?: (path: string, controller: AbortController) => void | Promise<void>,
): Promise<{ readonly paths: string[]; readonly failure: unknown }> => {
const reader = new DirectoryArtifactReader(root);
const controller = new AbortController();
@@ -34,8 +34,8 @@ const traverse = async (
maximumEntries,
})) {
paths.push(entry.path);
if (paths.length === stopAfter?.entries)
controller.abort(stopAfter.reason);
const observation = onEntry?.(entry.path, controller);
if (observation !== undefined) await observation;
}
return { paths, failure: undefined };
} catch (failure: unknown) {
@@ -68,19 +68,31 @@ describe("directory traversal of a parent wider than call arguments allow", () =
});
it("schedules the artifact reader's children within the selected entry budget", async () => {
// Cancel once the parent listing is complete. The next cancellation
// checkpoint follows scheduling its children for descent.
const reason = new Error("Stop after the wide parent listing");
const listed = await traverse(root, CHILD_DIRECTORIES, {
entries: CHILD_DIRECTORIES,
reason,
const reason = new Error("Stop after visiting a scheduled child");
let marker: string | undefined;
const listed = await traverse(
root,
CHILD_DIRECTORIES + 1,
(path, controller) => {
if (marker === undefined) {
// Populate the first observed child so native directory order does not
// require walking every scheduled directory to witness descent.
marker = `${path}/probe.txt`;
return writeFile(join(root, marker), "scheduled child");
}
if (path === marker) controller.abort(reason);
return undefined;
},
).finally(async () => {
if (marker !== undefined) await rm(join(root, marker), { force: true });
});
expect(listed.failure).toBeInstanceOf(ArtifactReaderFailure);
expect(listed.failure).toMatchObject({
reason: "cancelled",
cause: reason,
});
expect(listed.paths).toHaveLength(CHILD_DIRECTORIES);
expect(listed.paths).toHaveLength(CHILD_DIRECTORIES + 1);
expect(listed.paths.at(-1)).toBe(marker);
expect(missingChildren(listed.paths)).toEqual([]);
// A smaller selected allowance stays a typed refusal at its actual bound.