fix(artifacts): bind member reads to emitted metadata

Reject Mach-O and ZIP entries whose metadata no longer matches the reader inventory, preventing a sibling adapter key from selecting bytes under another entry. Reuse the existing ASAR metadata snapshot and equality rules and read Mach-O ranges from owned records.

Move the real system-lipo test to the provider boundary suite, release its reader, and derive slice expectations from the host FAT table.

Validation: 68 focused artifact, ASAR and MCP tests plus ZIP cancellation passed. Real macOS lipo was exercised against /bin/ls; Ghidra, IDA and other deep providers were not run. Static, docs, architecture and test discovery checks passed.
This commit is contained in:
morluto
2026-10-11 03:53:50 +08:00
parent 3967cfa43e
commit 39da0b150b
7 changed files with 164 additions and 70 deletions
@@ -35,6 +35,48 @@ it("bounds a requested metadata read without rejecting the underlying valid ZIP"
}
});
it("binds ZIP opens to the emitted entry metadata", async () => {
const root = await createTestTempDirectory("rea-zip-entry-identity-");
const path = join(root, "entries.zip");
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("first.txt", new TextReader("first bytes"));
await writer.add("second.txt", new TextReader("second bytes"));
await writeFile(path, await writer.close());
const reader = new ZipArtifactReader(path, "zip");
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const first = entries[0];
const second = entries[1];
if (first === undefined || second === undefined)
throw new Error("Expected both ZIP entries");
await expect(
reader.open({ ...second, adapterKey: first.adapterKey }),
).rejects.toMatchObject({ reason: "integrity" });
await expect(
reader.open({ ...second, path: first.path }),
).rejects.toMatchObject({ reason: "integrity" });
await expect(
reader.open({ ...second, declaredSize: first.declaredSize }),
).rejects.toMatchObject({ reason: "integrity" });
const originalKey = second.adapterKey;
Reflect.set(second, "adapterKey", first.adapterKey);
await expect(reader.open(second)).rejects.toMatchObject({
reason: "integrity",
});
Reflect.set(second, "adapterKey", originalKey);
const stream = await reader.open({ ...second });
const chunks: Buffer[] = [];
for await (const chunk of stream) chunks.push(Buffer.from(chunk));
expect(Buffer.concat(chunks).toString()).toBe("second bytes");
} finally {
await reader.close();
}
});
it("rejects overlapping ZIP member data ranges through artifact inventory", async () => {
const root = await createTestTempDirectory("rea-zip-overlap-");
const path = join(root, "overlap.zip");
@@ -83,9 +83,10 @@ describe("artifact Mach-O slices", () => {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
expect(entries).toHaveLength(2);
const firstEntry = entries[0];
const secondEntry = entries[1];
expect(secondEntry).toBeDefined();
if (secondEntry === undefined) return;
if (firstEntry === undefined || secondEntry === undefined)
throw new Error("Expected both universal Mach-O slice entries");
expect(secondEntry).toMatchObject({
path: "slices/arm64",
byteOffset: 8192,
@@ -95,8 +96,26 @@ describe("artifact Mach-O slices", () => {
await expect(
reader.open({ ...secondEntry, adapterKey }),
).rejects.toMatchObject({ reason: "integrity" });
await expect(
reader.open({ ...secondEntry, adapterKey: firstEntry.adapterKey }),
).rejects.toMatchObject({ reason: "integrity" });
await expect(
reader.open({ ...secondEntry, path: firstEntry.path }),
).rejects.toMatchObject({ reason: "integrity" });
await expect(
reader.open({ ...secondEntry, byteOffset: firstEntry.byteOffset }),
).rejects.toMatchObject({ reason: "integrity" });
await expect(
reader.open({ ...secondEntry, declaredSize: arm.length + 1 }),
).rejects.toMatchObject({ reason: "integrity" });
const originalKey = secondEntry.adapterKey;
Reflect.set(secondEntry, "adapterKey", firstEntry.adapterKey);
await expect(reader.open(secondEntry)).rejects.toMatchObject({
reason: "integrity",
});
Reflect.set(secondEntry, "adapterKey", originalKey);
const chunks: Buffer[] = [];
const stream = await reader.open(secondEntry);
const stream = await reader.open({ ...secondEntry });
for await (const chunk of stream) chunks.push(Buffer.from(chunk));
expect(Buffer.concat(chunks)).toEqual(Buffer.from(arm));
expect(reader.provenance()).toEqual([
@@ -0,0 +1,44 @@
import { readFile } from "node:fs/promises";
import { describe, expect, it, onTestFinished } from "vitest";
import { MachOSliceArtifactReader } from "../../../../src/artifacts/MachOSliceArtifactReader.js";
/**
* Read a 32-bit FAT table directly. The host's /bin/ls changes between macOS
* releases, so the expected slice layout comes from the file itself.
*/
const fatSlices = async (
path: string,
): Promise<readonly (readonly [number, number])[]> => {
const bytes = await readFile(path);
expect(bytes.readUInt32BE(0)).toBe(0xca_fe_ba_be);
return Array.from({ length: bytes.readUInt32BE(4) }, (_, index) => {
const entry = 8 + index * 20;
return [bytes.readUInt32BE(entry + 8), bytes.readUInt32BE(entry + 12)];
});
};
describe("Mach-O slice reader with system lipo", () => {
it.skipIf(process.platform !== "darwin")(
"checks real /bin/ls slice ranges against its FAT table",
async () => {
const reader = new MachOSliceArtifactReader("/bin/ls", {});
onTestFinished(() => reader.close());
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
expect(new Set(entries.map(({ path }) => path)).size).toBe(
entries.length,
);
expect(entries.every(({ path }) => path.startsWith("slices/"))).toBe(
true,
);
expect(
entries.map(({ byteOffset, declaredSize }) => [
byteOffset,
declaredSize,
]),
).toEqual(await fatSlices("/bin/ls"));
},
);
});