fix(artifacts): preserve archive facts and Unicode path identity

This commit is contained in:
morluto
2026-10-10 05:13:53 +08:00
parent 70bad53847
commit 5751f5caac
2 changed files with 126 additions and 69 deletions
+79 -69
View File
@@ -25,6 +25,7 @@ import {
} from "../../domain/apple/keyedArchive.js";
import { DirectoryArtifactReader } from "../DirectoryArtifactReader.js";
import { ArtifactReaderFailure } from "../ArtifactReader.js";
import type { ArtifactEntry } from "../ArtifactReader.js";
import { decodeXmlPlistText } from "../../domain/propertyListXmlText.js";
@@ -47,7 +48,10 @@ export const decodeKeyedArchiveBytes = (
const parsed = binary
? { value: parseBinary(bytes), omittedPrototypeKeys: 0 }
: parseXmlPropertyList(xmlText ?? "");
const graph = projectKeyedArchive(normalizePlist(parsed.value), selection);
const { value: plistValue, unknownRealCount } = projectPlistValue(
parsed.value,
);
const graph = projectKeyedArchive(plistValue, selection);
const numbers = createPlistNumberProjection(
binary
? binaryPlistNumberLiterals(bytes)
@@ -76,17 +80,15 @@ export const decodeKeyedArchiveBytes = (
...(parsed.omittedPrototypeKeys === 0
? []
: [omittedPrototypeKeysLimitation(parsed.omittedPrototypeKeys)]),
...(unknownRealCount === 0
? []
: [
`${String(unknownRealCount)} non-finite real value(s) are reported as { "$plist_type": "real", "value": null } because JSON evidence cannot preserve NaN vs infinity.`,
]),
],
};
};
const normalizePlist = (value: unknown): JsonValue => {
const projected = projectPlistValue(value);
if (projected.unknownRealCount > 0)
throw new RangeError("Keyed archive contains a non-finite real");
return projected.value;
};
/**
* Point to the workflow that reads the selected kind of file on this host.
* decode_interface_builder is portable; inspect_plist requires macOS.
@@ -126,75 +128,83 @@ export const inspectBundleKeyedArchive = async (input: {
);
const reader = new DirectoryArtifactReader(input.bundlePath);
try {
// Match inventory spelling while retaining the raw path for I/O and evidence.
const wanted = selected.path.normalize("NFC");
const matches: ArtifactEntry[] = [];
for await (const entry of reader.entries(input.signal)) {
if (entry.path !== selected.path) continue;
if (entry.kind !== "file")
throw archivePathError(
"invalid_value",
`Archive path selects a ${entry.kind}, not a regular file: ${selected.path}`,
if (entry.path.normalize("NFC") !== wanted) continue;
matches.push(entry);
}
if (matches.length > 1)
throw archivePathError(
"invalid_value",
`Archive path matches ${String(matches.length)} Unicode-equivalent bundle entries; selection is ambiguous: ${selected.path}`,
);
const entry = matches[0];
if (entry === undefined)
throw archivePathError(
"invalid_value",
`No regular file exists at ${selected.path} in the active app bundle.`,
);
if (entry.kind !== "file")
throw archivePathError(
"invalid_value",
`Archive path selects a ${entry.kind}, not a regular file: ${selected.path}`,
);
if ((entry.declaredSize ?? 0) > MAX_BYTES)
throw new ArtifactReaderFailure("limit", "Keyed archive exceeds 64 MiB");
const stream = await reader.open(entry, input.signal);
const chunks: Buffer[] = [];
let size = 0;
for await (const chunk of stream) {
if (input.signal?.aborted) {
stream.destroy();
throw new ArtifactReaderFailure(
"cancelled",
"Archive inspection cancelled",
);
if ((entry.declaredSize ?? 0) > MAX_BYTES)
}
const bytes = z.instanceof(Buffer).parse(chunk);
size += bytes.length;
if (size > MAX_BYTES) {
stream.destroy();
throw new ArtifactReaderFailure(
"limit",
"Keyed archive exceeds 64 MiB",
);
const stream = await reader.open(entry, input.signal);
const chunks: Buffer[] = [];
let size = 0;
for await (const chunk of stream) {
if (input.signal?.aborted) {
stream.destroy();
throw new ArtifactReaderFailure(
"cancelled",
"Archive inspection cancelled",
);
}
const bytes = z.instanceof(Buffer).parse(chunk);
size += bytes.length;
if (size > MAX_BYTES) {
stream.destroy();
throw new ArtifactReaderFailure(
"limit",
"Keyed archive exceeds 64 MiB",
);
}
chunks.push(bytes);
}
const bytes = Buffer.concat(chunks, size);
try {
return keyedArchiveResultSchema.parse({
target_sha256: input.targetSha256,
archive_path: entry.path,
archive_sha256: createHash("sha256").update(bytes).digest("hex"),
...decodeKeyedArchiveBytes(bytes, selected),
});
} catch (cause) {
if (cause instanceof AnalysisInputError) throw cause;
// An intact file of another kind is not a damaged archive.
if (cause instanceof KeyedArchiveKindError)
throw new AnalysisUnsupportedTargetError(
"inspect_keyed_archive",
join(input.bundlePath, entry.path),
cause.message,
{
cause,
remediationAction: keyedArchiveKindRemediation(
cause.kind,
input.platform ?? process.platform,
),
},
);
throw new ArtifactReaderFailure(
cause instanceof RangeError ? "limit" : "format",
`Cannot decode selected Foundation keyed archive: ${cause instanceof Error ? cause.message : String(cause)}`,
{ cause },
);
}
chunks.push(bytes);
}
const bytes = Buffer.concat(chunks, size);
try {
return keyedArchiveResultSchema.parse({
target_sha256: input.targetSha256,
archive_path: entry.path,
archive_sha256: createHash("sha256").update(bytes).digest("hex"),
...decodeKeyedArchiveBytes(bytes, selected),
});
} catch (cause) {
if (cause instanceof AnalysisInputError) throw cause;
// An intact file of another kind is not a damaged archive.
if (cause instanceof KeyedArchiveKindError)
throw new AnalysisUnsupportedTargetError(
"inspect_keyed_archive",
join(input.bundlePath, entry.path),
cause.message,
{
cause,
remediationAction: keyedArchiveKindRemediation(
cause.kind,
input.platform ?? process.platform,
),
},
);
throw new ArtifactReaderFailure(
cause instanceof RangeError ? "limit" : "format",
`Cannot decode selected Foundation keyed archive: ${cause instanceof Error ? cause.message : String(cause)}`,
{ cause },
);
}
throw archivePathError(
"invalid_value",
`No regular file exists at ${selected.path} in the active app bundle.`,
);
} finally {
await reader.close();
}
@@ -1,3 +1,4 @@
import { createHash } from "node:crypto";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
@@ -167,6 +168,52 @@ it("keeps a damaged NSKeyedArchiver archive a format failure", async () => {
});
});
it("selects a decomposed bundle path by its normalized spelling and reports the raw identity", async () => {
const root = await bundle();
const rawPath = `Contents/Resources/Caf\u0065\u0301.plist`;
const bytes = Buffer.from(archive);
await writeFile(join(root, rawPath), bytes);
const result = await inspect(root, {
path: rawPath.normalize("NFC"),
});
expect(result.archive_path).toBe(rawPath);
expect(result.archive_sha256).toBe(
createHash("sha256").update(bytes).digest("hex"),
);
});
it("keeps an archive readable when a non-finite real is an explicit unknown", async () => {
const root = await bundle();
const bytes = Buffer.from(
build({
$archiver: "NSKeyedArchiver",
$version: 100000,
$objects: ["$null", Number.NaN],
$top: { root: { CF$UID: 1 } },
}),
);
await writeFile(
join(root, "Contents", "Resources", "NonFinite.plist"),
bytes,
);
const result = await inspect(root, {
path: "Contents/Resources/NonFinite.plist",
});
expect(result.objects).toContainEqual(
expect.objectContaining({
id: 1,
value: { $plist_type: "real", value: null },
}),
);
expect(result.limitations).toContain(
'1 non-finite real value(s) are reported as { "$plist_type": "real", "value": null } because JSON evidence cannot preserve NaN vs infinity.',
);
});
const asAnalysisError = (value: unknown): AnalysisError => {
if (!(value instanceof AnalysisError)) throw value;
return value;