fix(artifacts): budget retained entries and recover directory integrity

This commit is contained in:
morluto
2026-10-08 19:46:31 +08:00
parent d9a39e5b48
commit 1d27061a65
4 changed files with 101 additions and 5 deletions
+17 -1
View File
@@ -4,8 +4,12 @@ import { ArtifactReaderFailure } from "./ArtifactReader.js";
/** One decoded-byte ceiling shared by an archive and all nested readers. */
export class ArtifactDecodedBudget {
#used = 0;
#metadataUsed = 0;
constructor(private readonly maximum = 512 * 1024 * 1024) {}
constructor(
private readonly maximum = 512 * 1024 * 1024,
private readonly maximumMetadata = 128 * 1024 * 1024,
) {}
/** Charge decoding work, including repeated reads, before exposing a chunk. */
consume(bytes: number, path: string): void {
@@ -20,6 +24,18 @@ export class ArtifactDecodedBudget {
);
this.#used += bytes;
}
/** Charge retained path text, path-trie segments, and per-entry graph/index capacity. */
consumeEntry(path: string): void {
const segments = path.split("/").length;
const units = 1024 + Buffer.byteLength(path) * 2 + segments * 256;
if (units > this.maximumMetadata - this.#metadataUsed)
throw new ArtifactReaderFailure(
"limit",
`Retained archive metadata budget of ${this.maximumMetadata} capacity units exhausted at ${path}`,
);
this.#metadataUsed += units;
}
}
/** Decoder-stage budget/cancellation guard, independent of member metadata. */
+17 -2
View File
@@ -329,6 +329,7 @@ export class CpioArtifactReader implements ArtifactReader {
"cpio member name is not valid UTF-8",
);
}
this.decodedBudget.consumeEntry(raw);
if (raw === TRAILER) {
if (header.fileSize !== 0)
throw new ArtifactReaderFailure(
@@ -352,6 +353,20 @@ export class CpioArtifactReader implements ArtifactReader {
): AsyncGenerator<ArtifactEntry> {
const type = header.mode & S_IFMT;
const path = memberPath(raw);
if (type === S_IFDIR) {
const verified = await this.#recoverIntegrity(() =>
drain(this.#verified(source, header, raw)),
);
if (path !== undefined)
yield entryOf({
path,
kind: "directory",
key,
header,
limitations: "mismatch" in verified ? [verified.mismatch] : [],
});
return;
}
if (type === S_IFLNK) {
yield* this.#symlink(source, { header, raw, key }, path);
return;
@@ -395,7 +410,7 @@ export class CpioArtifactReader implements ArtifactReader {
this.#current = { key, path: raw, header };
this.#consumed = false;
if (path === undefined) return;
if (type !== S_IFREG && type !== S_IFDIR) {
if (type !== S_IFREG) {
// FIFOs, device nodes, sockets and other types are not expanded; keep
// an explicit unavailable occurrence instead of dropping the path.
const verified = await this.#recoverIntegrity(() =>
@@ -418,7 +433,7 @@ export class CpioArtifactReader implements ArtifactReader {
}
yield entryOf({
path,
kind: type === S_IFDIR ? "directory" : "file",
kind: "file",
key,
header,
limitations: [],
@@ -295,3 +295,64 @@ it("rejects a CRC trailer whose declared checksum is not empty-data zero", async
await reader.close();
}
});
it("bounds retained metadata independently of small decoded byte counts", async () => {
const members = Array.from({ length: 100 }, (_, index) => ({
name: `empty-${index}`,
mode: MODE.file,
}));
const reader = new CpioArtifactReader(
async () => Readable.from([gzipCpio(members)]),
"fail",
new ArtifactDecodedBudget(1024 * 1024, 32 * 1024),
);
let seen = 0;
try {
await expect(
(async () => {
for await (const entry of reader.entries()) {
void entry;
seen++;
}
})(),
).rejects.toMatchObject({
reason: "limit",
message: expect.stringContaining("Retained archive metadata budget"),
});
expect(seen).toBeGreaterThan(0);
expect(seen).toBeLessThan(members.length);
} finally {
await reader.close();
}
});
it("recovers directory CRC contradictions before yielding and reaches regular siblings", async () => {
const reader = new CpioArtifactReader(
async () =>
Readable.from([
gzipCpio(
[
{ name: "dir", mode: MODE.directory, check: 1 },
{ name: "dir/good", mode: MODE.file, data: "ok" },
],
"crc",
),
]),
"record-and-continue",
);
try {
const seen: string[] = [];
for await (const entry of reader.entries()) {
seen.push(entry.path);
if (entry.kind === "directory")
expect(entry.limitations).toContain(
"cpio CRC disagrees with content: dir",
);
else
expect((await buffer(await reader.open(entry))).toString()).toBe("ok");
}
expect(seen).toEqual(["dir", "dir/good"]);
} finally {
await reader.close();
}
});
+6 -2
View File
@@ -201,7 +201,10 @@ const classifyMemberType = (
};
/** Walk nested `<file>` elements in document order. */
const collectMembers = (toc: Element): XarMember[] => {
const collectMembers = (
toc: Element,
budget: ArtifactDecodedBudget,
): XarMember[] => {
const members: XarMember[] = [];
const paths = new XarPathBudget();
const pending: Array<{ readonly element: Element; readonly parent: string }> =
@@ -221,6 +224,7 @@ const collectMembers = (toc: Element): XarMember[] => {
)
throw new ArtifactReaderFailure("path", "xar member has an unsafe name");
const path = paths.join(next.parent, name);
budget.consumeEntry(path);
const type = textOf(next.element, "type")?.trim();
const mode = xarMode(textOf(next.element, "mode"));
const { kind, unsupportedType } = classifyMemberType(type);
@@ -502,7 +506,7 @@ export class XarArtifactReader implements ArtifactReader {
if (tocElement === undefined)
throw new ArtifactReaderFailure("format", "xar TOC has no <toc> element");
await this.#verifyToc(tocElement, compressed, headerChecksumAlg);
this.#members = collectMembers(tocElement);
this.#members = collectMembers(tocElement, this.decodedBudget);
for (const member of this.#members) {
if (this.#byPath.has(member.path))
throw new ArtifactReaderFailure(