fix(artifacts): preserve keyed archive integer precision
Integrate the numeric precision correction from #1135 with complete supplemental metadata handling. Keep decoded numbers with an explicit limitation for mixed XML content or unclassified integer text, rather than assigning another literal's exact decimal. Project nonnumeric UID and CF$UID dictionary values recursively while preserving numeric references, graph identity and original byte digests. Add filesystem, built CLI and MCP regressions for the review findings, adjacent XML representations and numeric reference controls. Retain the original native Foundation qualification fixtures and verifier. Verification: 84 focused tests; npm run check; npm run docs:check; npm run verify:test-discovery; npm run verify:keyed-archive (real Foundation, CLI and stdio MCP). No real Hopper or Ghidra verification was needed or run. Co-authored-by: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Signed-off-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
co-authored by
Rudy Mizrahi Celekli
parent
1d7115b7b4
commit
4034883e98
@@ -0,0 +1,163 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { build, buildBinary, parseBinary } from "plist";
|
||||
import { expect, it } from "vitest";
|
||||
import { inspectBundleKeyedArchive } from "../../../src/artifacts/apple/KeyedArchiveReader.js";
|
||||
import { keyedArchiveResultSchema } from "../../../src/domain/apple/keyedArchive.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
const scenarios = [
|
||||
"integer-collision",
|
||||
"integer-real-collision",
|
||||
"opaque-xml",
|
||||
"supplemental-binary-table",
|
||||
] as const;
|
||||
const fixture = async (scenario: (typeof scenarios)[number]) => {
|
||||
const root = await createTestTempDirectory("rea-integer-ambiguity-");
|
||||
const fields =
|
||||
scenario === "integer-collision"
|
||||
? { first: "__FIRST__", second: "__SECOND__" }
|
||||
: scenario === "integer-real-collision"
|
||||
? { first: "__FIRST__", real: "__REAL__" }
|
||||
: {
|
||||
first: "__FIRST__",
|
||||
real: "__REAL__",
|
||||
text: "<integer>9007199254740993</integer>",
|
||||
};
|
||||
const source = {
|
||||
$archiver: "NSKeyedArchiver",
|
||||
$version: 100000,
|
||||
$objects: ["$null", fields],
|
||||
$top: { root: { CF$UID: 1 } },
|
||||
};
|
||||
let bytes: Buffer;
|
||||
if (scenario === "supplemental-binary-table") {
|
||||
bytes = Buffer.from(
|
||||
buildBinary({
|
||||
...source,
|
||||
$objects: ["$null", { real: 9007199254740992 }],
|
||||
}),
|
||||
);
|
||||
const trailer = bytes.length - 32;
|
||||
// Add one unreachable offset entry backed by the first trailer byte. The
|
||||
// locked primary decoder accepts it; supplemental pre-trailer capacity does not.
|
||||
bytes.writeBigUInt64BE(
|
||||
bytes.readBigUInt64BE(trailer + 8) + 1n,
|
||||
trailer + 8,
|
||||
);
|
||||
const original = parseBinary(bytes);
|
||||
expect(original).toMatchObject({
|
||||
$objects: ["$null", { real: 9007199254740992 }],
|
||||
});
|
||||
} else {
|
||||
let xml = build(source)
|
||||
.replace(
|
||||
"<string>__FIRST__</string>",
|
||||
`<integer>${scenario === "integer-collision" ? "9007199254740992" : "9007199254740993"}</integer>`,
|
||||
)
|
||||
.replace(
|
||||
"<string>__SECOND__</string>",
|
||||
"<integer>9007199254740993</integer>",
|
||||
)
|
||||
.replace(
|
||||
"<string>__REAL__</string>",
|
||||
`<real>${scenario === "opaque-xml" ? "9007199254740996" : "9007199254740992"}</real>`,
|
||||
);
|
||||
if (scenario === "opaque-xml")
|
||||
xml = xml.replace(
|
||||
"</plist>",
|
||||
"<!-- <integer>9007199254740993</integer><real>9007199254740992</real> --></plist>",
|
||||
);
|
||||
bytes = Buffer.from(xml);
|
||||
}
|
||||
const path = join(root, "archive.plist");
|
||||
await writeFile(path, bytes);
|
||||
return {
|
||||
root,
|
||||
path,
|
||||
digest: createHash("sha256").update(bytes).digest("hex"),
|
||||
};
|
||||
};
|
||||
const verify = (
|
||||
result: ReturnType<typeof keyedArchiveResultSchema.parse>,
|
||||
scenario: (typeof scenarios)[number],
|
||||
digest: string,
|
||||
) => {
|
||||
expect(result.archive_sha256).toBe(digest);
|
||||
expect(result.objects).toHaveLength(2);
|
||||
expect(result.references).toContainEqual(
|
||||
expect.objectContaining({ source: null, target: 1, status: "resolved" }),
|
||||
);
|
||||
const value = result.objects.find(({ id }) => id === 1)?.value;
|
||||
expect(value).toBeDefined();
|
||||
expect(value).not.toBeNull();
|
||||
expect(Array.isArray(value)).toBe(false);
|
||||
expect(typeof value).toBe("object");
|
||||
const fields = value as Record<string, unknown>;
|
||||
const precisionNote = result.limitations.some(
|
||||
(note) =>
|
||||
/integer|number/iu.test(note) &&
|
||||
/precision|exact range|rounded/iu.test(note),
|
||||
);
|
||||
if (scenario === "integer-collision") {
|
||||
// Neither colliding decoded slot may be assigned one arbitrary exact literal.
|
||||
expect(fields.first).toBe(9007199254740992);
|
||||
expect(fields.second).toBe(9007199254740992);
|
||||
expect(precisionNote).toBe(true);
|
||||
} else if (scenario === "integer-real-collision") {
|
||||
expect(fields.first).toBe(9007199254740992);
|
||||
expect(fields.real).toBe(9007199254740992);
|
||||
expect(precisionNote).toBe(true);
|
||||
} else if (scenario === "opaque-xml") {
|
||||
expect(fields.text).toBe("<integer>9007199254740993</integer>");
|
||||
expect(fields.real).toBe(9007199254740996);
|
||||
expect(fields.first).toBeDefined();
|
||||
const exact =
|
||||
typeof fields.first === "object" &&
|
||||
fields.first !== null &&
|
||||
JSON.stringify(fields.first) ===
|
||||
JSON.stringify({ $plist_type: "integer", decimal: "9007199254740993" });
|
||||
expect(exact || precisionNote).toBe(true);
|
||||
// If a literal count is reported, opaque content must not inflate it.
|
||||
const countNote = result.limitations.find((note) =>
|
||||
/integer literal\(s\)/u.test(note),
|
||||
);
|
||||
if (countNote !== undefined)
|
||||
expect(countNote).toMatch(/^1 observed integer literal/u);
|
||||
} else {
|
||||
expect(fields.real).toBe(9007199254740992);
|
||||
}
|
||||
};
|
||||
it.each(scenarios)(
|
||||
"filesystem retains numeric meaning for %s",
|
||||
async (scenario) => {
|
||||
const item = await fixture(scenario);
|
||||
const result = await inspectBundleKeyedArchive({
|
||||
bundlePath: item.root,
|
||||
targetSha256: item.digest,
|
||||
parameters: { path: "archive.plist", limit: 100 },
|
||||
});
|
||||
verify(result, scenario, item.digest);
|
||||
},
|
||||
);
|
||||
cliTest.for(scenarios)(
|
||||
"built CLI retains numeric meaning for %s",
|
||||
async (scenario, { cli }) => {
|
||||
const item = await fixture(scenario);
|
||||
const output = await cli.run({
|
||||
arguments: ["inspect-keyed-archive", item.path, "--json"],
|
||||
environment: { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" },
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
verify(
|
||||
keyedArchiveResultSchema.parse(
|
||||
parseEvidence(output.json).normalized_result,
|
||||
),
|
||||
scenario,
|
||||
item.digest,
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,61 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { inspectBundleKeyedArchive } from "../../../src/artifacts/apple/KeyedArchiveReader.js";
|
||||
import { keyedArchiveResultSchema } from "../../../src/domain/apple/keyedArchive.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import {
|
||||
keyedArchiveIntegerFormats,
|
||||
keyedArchiveIntegerCases,
|
||||
keyedArchiveIntegerFixture,
|
||||
expectKeyedArchiveIntegerCase,
|
||||
} from "../../fixtures/keyedArchiveIntegers.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
const cases = keyedArchiveIntegerFormats.flatMap((format) =>
|
||||
keyedArchiveIntegerCases.map((selectedCase) => ({ format, selectedCase })),
|
||||
);
|
||||
// Selected by the existing required native keyed-archive verifier, not Linux skips.
|
||||
describe.skipIf(process.platform !== "darwin")(
|
||||
"Foundation archive integer precision",
|
||||
() => {
|
||||
it.each(cases)(
|
||||
"filesystem preserves $format $selectedCase numeric meaning",
|
||||
async ({ format, selectedCase }) => {
|
||||
const fixture = await keyedArchiveIntegerFixture(format);
|
||||
const result = await inspectBundleKeyedArchive({
|
||||
bundlePath: fixture.root,
|
||||
targetSha256: fixture.digest,
|
||||
parameters: { path: fixture.selected.filename, limit: 100 },
|
||||
});
|
||||
expectKeyedArchiveIntegerCase(
|
||||
result,
|
||||
selectedCase,
|
||||
fixture.selected,
|
||||
fixture.digest,
|
||||
);
|
||||
},
|
||||
);
|
||||
cliTest.for(cases)(
|
||||
"built CLI preserves $format $selectedCase numeric meaning",
|
||||
async ({ format, selectedCase }, { cli }) => {
|
||||
const fixture = await keyedArchiveIntegerFixture(format);
|
||||
const output = await cli.run({
|
||||
arguments: ["inspect-keyed-archive", fixture.archivePath, "--json"],
|
||||
environment: {
|
||||
REA_LOG_LEVEL: "silent",
|
||||
REA_ANALYSIS_PROVIDER: "auto",
|
||||
},
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
const result = keyedArchiveResultSchema.parse(
|
||||
parseEvidence(output.json).normalized_result,
|
||||
);
|
||||
expectKeyedArchiveIntegerCase(
|
||||
result,
|
||||
selectedCase,
|
||||
fixture.selected,
|
||||
fixture.digest,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,39 @@
|
||||
import { it } from "vitest";
|
||||
|
||||
import { inspectBundleKeyedArchive } from "../../../src/artifacts/apple/KeyedArchiveReader.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import {
|
||||
archiveNumberMetadataCases,
|
||||
expectKeyedArchiveNumberMetadata,
|
||||
keyedArchiveNumberMetadataFixture,
|
||||
} from "../../fixtures/keyedArchiveNumberMetadata.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
it.each(archiveNumberMetadataCases)(
|
||||
"filesystem preserves numeric meaning for $name",
|
||||
async (item) => {
|
||||
const fixture = await keyedArchiveNumberMetadataFixture(item);
|
||||
const result = await inspectBundleKeyedArchive({
|
||||
bundlePath: fixture.root,
|
||||
targetSha256: fixture.digest,
|
||||
parameters: { path: "archive.plist" },
|
||||
});
|
||||
expectKeyedArchiveNumberMetadata(result, item, fixture.digest);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest.for(archiveNumberMetadataCases)(
|
||||
"built CLI preserves numeric meaning for $name",
|
||||
async (item, { cli }) => {
|
||||
const fixture = await keyedArchiveNumberMetadataFixture(item);
|
||||
const output = await cli.run({
|
||||
arguments: ["inspect-keyed-archive", fixture.path, "--json"],
|
||||
environment: { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" },
|
||||
});
|
||||
expectKeyedArchiveNumberMetadata(
|
||||
parseEvidence(output.json).normalized_result,
|
||||
item,
|
||||
fixture.digest,
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,115 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { build } from "plist";
|
||||
import { expect, it } from "vitest";
|
||||
import { inspectBundleKeyedArchive } from "../../../src/artifacts/apple/KeyedArchiveReader.js";
|
||||
import { keyedArchiveResultSchema } from "../../../src/domain/apple/keyedArchive.js";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
const cases = ["unsafe-integer", "integral-real"] as const;
|
||||
const fixture = async (kind: (typeof cases)[number]) => {
|
||||
const root = await createTestTempDirectory("rea-integer-scalar-");
|
||||
const xml = build({
|
||||
$archiver: "NSKeyedArchiver",
|
||||
$version: 100000,
|
||||
$objects: ["$null", "__VALUE__"],
|
||||
$top: { root: { CF$UID: 1 } },
|
||||
}).replace(
|
||||
"<string>__VALUE__</string>",
|
||||
kind === "unsafe-integer"
|
||||
? "<integer>9007199254740993</integer>"
|
||||
: "<real>9007199254740992</real>",
|
||||
);
|
||||
const bytes = Buffer.from(xml);
|
||||
const path = join(root, "archive.plist");
|
||||
await writeFile(path, bytes);
|
||||
return {
|
||||
root,
|
||||
path,
|
||||
digest: createHash("sha256").update(bytes).digest("hex"),
|
||||
};
|
||||
};
|
||||
const verify = (
|
||||
result: ReturnType<typeof keyedArchiveResultSchema.parse>,
|
||||
kind: (typeof cases)[number],
|
||||
digest: string,
|
||||
) => {
|
||||
expect(result.archive_sha256).toBe(digest);
|
||||
expect(result.roots).toEqual({ root: { CF$UID: 1 } });
|
||||
expect(
|
||||
result.objects.map(({ id, kind: objectKind, status }) => ({
|
||||
id,
|
||||
kind: objectKind,
|
||||
status,
|
||||
})),
|
||||
).toEqual([
|
||||
{ id: 0, kind: "nil", status: "nil" },
|
||||
{ id: 1, kind: "scalar", status: "observed" },
|
||||
]);
|
||||
expect(result.references).toEqual([
|
||||
{
|
||||
source: null,
|
||||
path: ["root"],
|
||||
target: 1,
|
||||
status: "resolved",
|
||||
raw: { CF$UID: 1 },
|
||||
},
|
||||
]);
|
||||
expect(result.total_objects).toBe(2);
|
||||
expect(result.total_references).toBe(1);
|
||||
expect(result.offset).toBe(0);
|
||||
expect(result.next_offset).toBeNull();
|
||||
expect(result.truncated).toBe(false);
|
||||
const value = result.objects.find(({ id }) => id === 1)?.value;
|
||||
expect(value).toBeDefined();
|
||||
if (kind === "integral-real") expect(value).toBe(9007199254740992);
|
||||
else {
|
||||
const exact =
|
||||
typeof value === "object" &&
|
||||
value !== null &&
|
||||
JSON.stringify(value) ===
|
||||
JSON.stringify({ $plist_type: "integer", decimal: "9007199254740993" });
|
||||
const disclosed = result.limitations.some(
|
||||
(note) =>
|
||||
/integer|number/iu.test(note) &&
|
||||
/precision|exact range|rounded/iu.test(note),
|
||||
);
|
||||
expect(exact || disclosed).toBe(true);
|
||||
}
|
||||
};
|
||||
it.each(cases)(
|
||||
"filesystem preserves primitive scalar graph for %s",
|
||||
async (kind) => {
|
||||
const item = await fixture(kind);
|
||||
verify(
|
||||
await inspectBundleKeyedArchive({
|
||||
bundlePath: item.root,
|
||||
targetSha256: item.digest,
|
||||
parameters: { path: "archive.plist", limit: 100 },
|
||||
}),
|
||||
kind,
|
||||
item.digest,
|
||||
);
|
||||
},
|
||||
);
|
||||
cliTest.for(cases)(
|
||||
"built CLI preserves primitive scalar graph for %s",
|
||||
async (kind, { cli }) => {
|
||||
const item = await fixture(kind);
|
||||
const output = await cli.run({
|
||||
arguments: ["inspect-keyed-archive", item.path, "--json"],
|
||||
environment: { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" },
|
||||
});
|
||||
expect(output.exitCode).toBe(0);
|
||||
verify(
|
||||
keyedArchiveResultSchema.parse(
|
||||
parseEvidence(output.json).normalized_result,
|
||||
),
|
||||
kind,
|
||||
item.digest,
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,44 @@
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { createBinarySession } from "../../../src/composition/binary.js";
|
||||
import { parseConfig } from "../../../src/config.js";
|
||||
import { keyedArchiveResultSchema } from "../../../src/domain/apple/keyedArchive.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import {
|
||||
archiveNumberMetadataCases,
|
||||
expectKeyedArchiveNumberMetadata,
|
||||
keyedArchiveNumberMetadataFixture,
|
||||
} from "../../fixtures/keyedArchiveNumberMetadata.js";
|
||||
import { mcpTest } from "../../support/mcp/mcpFixture.js";
|
||||
|
||||
mcpTest.for(archiveNumberMetadataCases)(
|
||||
"MCP preserves numeric meaning for $name",
|
||||
async (item, { mcp, onTestFinished }) => {
|
||||
const fixture = await keyedArchiveNumberMetadataFixture(item);
|
||||
const configured = parseConfig({ REA_ANALYSIS_PROVIDER: "auto" });
|
||||
if (!configured.ok) throw configured.error;
|
||||
const session = createBinarySession(configured.value);
|
||||
onTestFinished(async () => {
|
||||
await session.close();
|
||||
});
|
||||
const client = await mcp.connect(createServer(session, session));
|
||||
const opened = await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path: fixture.path },
|
||||
});
|
||||
expect(opened.isError, JSON.stringify(opened.content)).not.toBe(true);
|
||||
const result = await client.callTool({
|
||||
name: "inspect_keyed_archive",
|
||||
arguments: {},
|
||||
});
|
||||
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
|
||||
expectKeyedArchiveNumberMetadata(
|
||||
z
|
||||
.object({ result: keyedArchiveResultSchema })
|
||||
.parse(result.structuredContent).result,
|
||||
item,
|
||||
fixture.digest,
|
||||
);
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user