fix(artifacts): preserve evidence on stream failures and bound cpio output
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { Readable } from "node:stream";
|
||||
import {
|
||||
PassThrough,
|
||||
Readable,
|
||||
Transform,
|
||||
type TransformCallback,
|
||||
} from "node:stream";
|
||||
import { createGunzip } from "node:zlib";
|
||||
|
||||
import type { ArtifactCommand } from "../domain/artifactGraph.js";
|
||||
@@ -15,6 +20,8 @@ const MAX_LINK_BUFFER_BYTES = 16 * 1024 * 1024;
|
||||
const MAX_LINK_BUFFER_TOTAL = 64 * 1024 * 1024;
|
||||
/** Longest symlink target read; PATH_MAX is 1024 on macOS and 4096 on Linux. */
|
||||
const MAX_SYMLINK_TARGET_BYTES = 4096;
|
||||
/** Total decompressed cpio bytes accepted from one archive (gzip-bomb budget). */
|
||||
const MAX_TOTAL_DECOMPRESSED_BYTES = 512 * 1024 * 1024;
|
||||
const TRAILER = "TRAILER!!!";
|
||||
const S_IFMT = 0o170000;
|
||||
const S_IFDIR = 0o040000;
|
||||
@@ -86,6 +93,29 @@ class ByteSource {
|
||||
}
|
||||
}
|
||||
|
||||
/** Bound total decompressed bytes so a gzip bomb cannot exhaust the process. */
|
||||
class DecodedBudget extends Transform {
|
||||
#seen = 0;
|
||||
|
||||
override _transform(
|
||||
chunk: Buffer,
|
||||
_encoding: BufferEncoding,
|
||||
done: TransformCallback,
|
||||
): void {
|
||||
this.#seen += chunk.length;
|
||||
if (this.#seen > MAX_TOTAL_DECOMPRESSED_BYTES) {
|
||||
done(
|
||||
new ArtifactReaderFailure(
|
||||
"limit",
|
||||
`cpio archive decodes beyond ${MAX_TOTAL_DECOMPRESSED_BYTES} bytes`,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
done(null, chunk);
|
||||
}
|
||||
}
|
||||
|
||||
interface CpioHeader {
|
||||
readonly format: "odc" | "newc" | "crc";
|
||||
readonly mode: number;
|
||||
@@ -250,7 +280,20 @@ export class CpioArtifactReader implements ArtifactReader {
|
||||
// Prevent unhandled source errors after the decoder fails first.
|
||||
compressed.destroy();
|
||||
});
|
||||
const source = new ByteSource(compressed.pipe(gunzip));
|
||||
const budget = new DecodedBudget();
|
||||
gunzip.on("error", (cause: unknown) => {
|
||||
budget.destroy(
|
||||
cause instanceof ArtifactReaderFailure
|
||||
? cause
|
||||
: new ArtifactReaderFailure("format", "cpio stream failed", {
|
||||
cause,
|
||||
}),
|
||||
);
|
||||
});
|
||||
budget.on("error", () => {
|
||||
compressed.destroy();
|
||||
});
|
||||
const source = new ByteSource(compressed.pipe(gunzip).pipe(budget));
|
||||
this.#source = source;
|
||||
for (let index = 0; ; index++) {
|
||||
if (signal?.aborted === true)
|
||||
|
||||
@@ -138,7 +138,26 @@ const visitArtifactEntries = async (
|
||||
logicalPath,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (!isUnavailableUnpackedEntry(cause, entry)) throw cause;
|
||||
if (!isUnavailableUnpackedEntry(cause, entry)) {
|
||||
// A streamed CRC, size-bound, or format failure is per-member
|
||||
// evidence loss under record-and-continue: keep the occurrence
|
||||
// unavailable with its diagnostic, without inventing a node.
|
||||
if (
|
||||
context.integrity.mode !== "record-and-continue" ||
|
||||
!(cause instanceof ArtifactReaderFailure) ||
|
||||
(cause.reason !== "integrity" &&
|
||||
cause.reason !== "limit" &&
|
||||
cause.reason !== "format")
|
||||
)
|
||||
throw cause;
|
||||
occurrence.hash_status = "unavailable";
|
||||
occurrence.limitations.push(
|
||||
`Member content could not be verified: ${cause.message}`,
|
||||
);
|
||||
context.occurrences.push(occurrence);
|
||||
context.occurrenceByPath.set(logicalPath, occurrence);
|
||||
continue;
|
||||
}
|
||||
occurrence.hash_status = "unavailable";
|
||||
occurrence.limitations.push(UNAVAILABLE_UNPACKED_LIMITATION);
|
||||
}
|
||||
@@ -204,42 +223,11 @@ const digestArtifactEntry = async (
|
||||
)
|
||||
return undefined;
|
||||
const checksum = entry.declaredChecksum;
|
||||
let digest: Awaited<ReturnType<typeof hashReadable>>;
|
||||
try {
|
||||
digest = await hashReadable(
|
||||
await currentReader.open(entry, context.signal),
|
||||
context.signal,
|
||||
checksum?.algorithm,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
// A CRC, archived-checksum, or size-bound failure while streaming is an
|
||||
// integrity mismatch for this member, not the whole inspection, when the
|
||||
// caller selected record-and-continue.
|
||||
if (
|
||||
context.integrity.mode !== "record-and-continue" ||
|
||||
!(cause instanceof ArtifactReaderFailure) ||
|
||||
(cause.reason !== "integrity" &&
|
||||
cause.reason !== "limit" &&
|
||||
cause.reason !== "format")
|
||||
)
|
||||
throw cause;
|
||||
return {
|
||||
node: createArtifactNode({
|
||||
sha256:
|
||||
"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
size: 0,
|
||||
kind: "resource",
|
||||
format: "unknown",
|
||||
executable: entry.executable,
|
||||
contentState: "embedded",
|
||||
limitations: [
|
||||
"Observed content contradicts declared integrity metadata and is untrusted.",
|
||||
],
|
||||
}),
|
||||
mismatched: true,
|
||||
mismatchDetails: [cause.message],
|
||||
};
|
||||
}
|
||||
const digest = await hashReadable(
|
||||
await currentReader.open(entry, context.signal),
|
||||
context.signal,
|
||||
checksum?.algorithm,
|
||||
);
|
||||
const sha256Mismatch =
|
||||
entry.declaredSha256 !== null && entry.declaredSha256 !== digest.sha256;
|
||||
const checksumMismatch =
|
||||
|
||||
Reference in New Issue
Block a user