import { createHash } from "node:crypto"; import { writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect } from "vitest"; import { keyedArchiveResultSchema } from "../../src/domain/apple/keyedArchive.js"; import { createTestTempDirectory } from "./temporaryDirectory.js"; /** Inert XML with producer-equivalent key spellings and unchanged ordinary data. */ export const keyedArchivePrototypeKeyFixture = async () => { const directory = await createTestTempDirectory("rea-keyed-xml-keys-"); const path = join(directory, "archive.plist"); const bytes = Buffer.from(` $archiverNSKeyedArchiver $version100000 $objects$null __proto__recorded outer value kepta b…c
d __rea_prototype_key__ordinary key nested recorded nested value value7 $top__proto__CF$UID1root CF$UID1 `); await writeFile(path, bytes); return { path, digest: createHash("sha256").update(bytes).digest("hex") }; }; /** Require original source identity and every decoded dictionary member. */ export const expectKeyedArchivePrototypeKeys = ( value: unknown, digest: string, ) => { const result = keyedArchiveResultSchema.parse(value); expect(result.archive_sha256).toBe(digest); expect(result.roots).toEqual({ ["__proto__"]: { CF$UID: 1 }, root: { CF$UID: 1 }, }); expect(Object.hasOwn(result.roots, "__proto__")).toBe(true); expect(result.references).toContainEqual( expect.objectContaining({ source: null, path: ["__proto__"], target: 1, status: "resolved", }), ); expect(result.objects).toHaveLength(2); expect(result.objects[1]).toMatchObject({ id: 1, kind: "dictionary", value: { ["__proto__"]: "recorded outer value", kept: "a\rb\u0085c\u2028d", __rea_prototype_key__: "ordinary key", nested: { ["__proto__"]: "recorded nested value", value: 7 }, }, }); expect( result.limitations.some((value) => value.includes("keyed __proto__")), ).toBe(false); };