fix(artifacts): apply the integrity policy to xar checksums and bound symlinks

- Inventory scans now verify xar extracted checksums themselves, under the
  caller's integrity policy. Under "fail" a mismatch still fails, now as
  artifact_integrity_mismatch with the logical path. Under
  "record-and-continue" the member stays in the graph as mismatched, with
  the declared and observed values, and is not expanded. A declared SHA-256
  becomes an integrity contradiction. Extraction keeps verifying in the reader.
- Read cpio symlink targets only up to 4096 bytes; longer declared targets
  are streamed past and recorded with a limitation instead of buffered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-07 12:50:52 +00:00
co-authored by Claude Opus 5.5
parent 667bf164f9
commit 7857d2d329
11 changed files with 212 additions and 43 deletions
+12 -1
View File
@@ -9,6 +9,8 @@ export type HashResult = {
readonly sha256: string;
readonly bytes: number;
readonly prefix: Buffer;
/** Digest in the additionally requested algorithm, when one was requested. */
readonly also?: string;
};
/** Bounded classification evidence; this is not an executable format size limit. */
@@ -27,8 +29,11 @@ export const abortIfNeeded = (signal?: AbortSignal): void => {
export const hashReadable = async (
stream: Readable,
signal?: AbortSignal,
alsoAlgorithm?: string,
): Promise<HashResult> => {
const hash = createHash("sha256");
const also =
alsoAlgorithm === undefined ? undefined : createHash(alsoAlgorithm);
const prefixes: Buffer[] = [];
let prefixBytes = 0;
let bytes = 0;
@@ -37,6 +42,7 @@ export const hashReadable = async (
const chunk = streamChunkToBuffer(raw);
bytes += chunk.length;
hash.update(chunk);
also?.update(chunk);
if (prefixBytes < ARTIFACT_CLASSIFICATION_PREFIX_BYTES) {
const selected = chunk.subarray(
0,
@@ -46,5 +52,10 @@ export const hashReadable = async (
prefixBytes += selected.length;
}
}
return { sha256: hash.digest("hex"), bytes, prefix: Buffer.concat(prefixes) };
return {
sha256: hash.digest("hex"),
bytes,
prefix: Buffer.concat(prefixes),
...(also === undefined ? {} : { also: also.digest("hex") }),
};
};
+9
View File
@@ -15,6 +15,15 @@ export interface ArtifactEntry {
readonly unpacked: boolean;
readonly limitations: readonly string[];
readonly adapterKey: string;
/**
* Container checksum of the member's bytes in an algorithm other than
* SHA-256 (xar declares SHA-1 or MD5). The scan verifies it under the
* caller's integrity policy.
*/
readonly declaredChecksum?: {
readonly algorithm: "sha1" | "md5" | "sha512";
readonly value: string;
};
/** The container records this member but does not hold its bytes, such as an unresolved hard link. */
readonly contentUnavailable?: boolean;
/** Archive format of this member's own bytes, when the scan should expand it. */
+12 -2
View File
@@ -13,6 +13,8 @@ const MAX_NAME_BYTES = 64 * 1024;
/** Hard-link bytes are buffered to serve every linked path, within these bounds. */
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;
const TRAILER = "TRAILER!!!";
const S_IFMT = 0o170000;
const S_IFDIR = 0o040000;
@@ -278,14 +280,22 @@ export class CpioArtifactReader implements ArtifactReader {
const type = header.mode & S_IFMT;
const path = memberPath(raw);
if (type === S_IFLNK) {
const target = await this.#collect(source, header, raw);
// A hostile header can declare a multi-gigabyte target; stream it past.
let target: Buffer | undefined;
if (header.fileSize > MAX_SYMLINK_TARGET_BYTES)
await drain(this.#verified(source, header, raw));
else target = await this.#collect(source, header, raw);
if (path !== undefined)
yield entryOf({
path,
kind: "symlink",
key,
header,
limitations: [`Symlink target: ${target.toString("utf8")}`],
limitations: [
target === undefined
? `Symlink target of ${header.fileSize} bytes exceeds ${MAX_SYMLINK_TARGET_BYTES} bytes and was not read.`
: `Symlink target: ${target.toString("utf8")}`,
],
});
return;
}
@@ -272,6 +272,39 @@ describe("installer package reader hardening", () => {
});
});
describe("cpio symlink targets", () => {
it("streams past an oversized symlink target and keeps reading", async () => {
const reader = new CpioArtifactReader(() =>
Promise.resolve(
Readable.from([
gzipCpio(
[
{ name: "./huge", mode: MODE.symlink, data: "x".repeat(4097) },
{ name: "./after", mode: MODE.file, data: "after" },
],
"crc",
),
]),
),
);
try {
const entries = await collect(reader);
expect(
entries.map(({ kind, path, limitations }) => [kind, path, limitations]),
).toEqual([
[
"symlink",
"huge",
["Symlink target of 4097 bytes exceeds 4096 bytes and was not read."],
],
["file", "after", []],
]);
} finally {
await reader.close();
}
});
});
describe("cpio hard links and CRC archives", () => {
const readAll = async (archive: Uint8Array) => {
const reader = new CpioArtifactReader(() =>
+31 -4
View File
@@ -151,6 +151,22 @@ const collectMembers = (toc: Element): XarMember[] => {
return members;
};
/** Expose a member's extracted checksum so a caller can apply its own policy. */
const declaredDigest = (
checksum: { readonly algorithm: string; readonly value: string } | undefined,
): Pick<ArtifactEntry, "declaredSha256" | "declaredChecksum"> => {
if (checksum === undefined) return { declaredSha256: null };
if (checksum.algorithm === "sha256")
return { declaredSha256: checksum.value };
const algorithm = checksum.algorithm;
return algorithm === "sha1" || algorithm === "md5" || algorithm === "sha512"
? {
declaredSha256: null,
declaredChecksum: { algorithm, value: checksum.value },
}
: { declaredSha256: null };
};
/** Verify a member's extracted checksum as its decoded bytes stream past. */
class ChecksumVerifier extends Transform {
readonly #hash: Hash;
@@ -203,7 +219,19 @@ export class XarArtifactReader implements ArtifactReader {
#heap = 0;
#members: readonly XarMember[] = [];
constructor(private readonly path: string) {}
readonly #verifyChecksums: boolean;
/**
* @param options.verifyChecksums Fail `open()` streams whose bytes disagree
* with the member's extracted checksum (default). Inventory passes false and
* verifies the declared checksum itself under the caller's integrity policy.
*/
constructor(
private readonly path: string,
options: { readonly verifyChecksums?: boolean } = {},
) {
this.#verifyChecksums = options.verifyChecksums ?? true;
}
async #load(signal?: AbortSignal): Promise<void> {
if (this.#handle !== undefined) return;
@@ -364,8 +392,7 @@ export class XarArtifactReader implements ArtifactReader {
member.data?.encoding === "application/octet-stream"
? this.#heap + member.data.offset
: null,
// xar declares SHA-1/MD5 checksums, which open() verifies itself.
declaredSha256: null,
...declaredDigest(member.data?.extractedChecksum),
unpacked: false,
limitations: [
...(member.kind === "symlink" && member.link !== undefined
@@ -409,7 +436,7 @@ export class XarArtifactReader implements ArtifactReader {
);
const checksum = data.extractedChecksum;
const verified =
checksum === undefined
checksum === undefined || !this.#verifyChecksums
? new PassThrough()
: new ChecksumVerifier(entry.path, checksum);
// Decoder errors become tagged failures; cancellation stays tagged.