|
| 1 | +import { createHash } from "node:crypto"; |
| 2 | +import { mkdir, writeFile } from "node:fs/promises"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +import { describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +import { analyzeInterfaceBuilderBundle } from "./InterfaceBuilderAnalysis.js"; |
| 8 | +import { preflightPropertyListDecode } from "./InterfaceBuilderDecodeBudget.js"; |
| 9 | +import { parseXmlPropertyList } from "../../domain/propertyListKeys.js"; |
| 10 | +import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; |
| 11 | + |
| 12 | +const example = "<!DOCTYPE document [ harmless example ]>"; |
| 13 | +const archive = (content: string, comment = "") => |
| 14 | + `<?xml version="1.0"?><plist version="1.0">${comment}<dict><key>$archiver</key><string>NSKeyedArchiver</string><key>$objects</key><array><string>$null</string><dict><key>$class</key><dict><key>CF$UID</key><integer>2</integer></dict><key>title</key><string>${content}</string></dict><dict><key>$classname</key><string>NSButton</string><key>$classes</key><array><string>NSButton</string><string>NSObject</string></array></dict></array><key>$top</key><dict><key>root</key><dict><key>CF$UID</key><integer>1</integer></dict></dict></dict></plist>`; |
| 15 | + |
| 16 | +const inertDeclarations = [ |
| 17 | + { |
| 18 | + kind: "CDATA text", |
| 19 | + xml: archive(`<![CDATA[${example}]]>`), |
| 20 | + title: example, |
| 21 | + }, |
| 22 | + { |
| 23 | + kind: "XML comment", |
| 24 | + xml: archive("Build", `<!-- ${example} -->`), |
| 25 | + title: "Build", |
| 26 | + }, |
| 27 | +] as const; |
| 28 | + |
| 29 | +describe("inert XML declaration examples in Interface Builder archives", () => { |
| 30 | + it.each(inertDeclarations)( |
| 31 | + "preflights $kind without treating it as entity expansion", |
| 32 | + ({ xml }) => { |
| 33 | + expect(() => parseXmlPropertyList(xml)).not.toThrow(); |
| 34 | + expect( |
| 35 | + preflightPropertyListDecode(Buffer.from(xml), 16 * 1024 * 1024), |
| 36 | + ).toMatchObject({ |
| 37 | + binaryPrototypeKeyObserved: false, |
| 38 | + }); |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + it.each(inertDeclarations)( |
| 43 | + "retains $kind and archive provenance through bundle decoding", |
| 44 | + async ({ xml, title }) => { |
| 45 | + const root = await createTestTempDirectory("rea-ib-inert-declaration-"); |
| 46 | + const bundle = join(root, "Example.app"); |
| 47 | + await mkdir(bundle); |
| 48 | + const bytes = Buffer.from(xml); |
| 49 | + await writeFile(join(bundle, "Panel.nib"), bytes); |
| 50 | + |
| 51 | + const result = await analyzeInterfaceBuilderBundle({ |
| 52 | + bundlePath: bundle, |
| 53 | + targetSha256: "a".repeat(64), |
| 54 | + }); |
| 55 | + |
| 56 | + expect(result.documents).toContainEqual( |
| 57 | + expect.objectContaining({ |
| 58 | + relative_path: "Panel.nib", |
| 59 | + archive_sha256: createHash("sha256").update(bytes).digest("hex"), |
| 60 | + }), |
| 61 | + ); |
| 62 | + expect(result.graph.nodes.map(({ name }) => name)).toContain(title); |
| 63 | + expect(result.graph.coverage).toContainEqual( |
| 64 | + expect.objectContaining({ |
| 65 | + facet: "archive_decode", |
| 66 | + status: "complete", |
| 67 | + }), |
| 68 | + ); |
| 69 | + }, |
| 70 | + ); |
| 71 | + |
| 72 | + it("continues to refuse an actual internal entity declaration before decoding", () => { |
| 73 | + const xml = archive("&harmless;").replace( |
| 74 | + "<plist", |
| 75 | + '<!DOCTYPE plist [<!ENTITY harmless "value">]><plist', |
| 76 | + ); |
| 77 | + expect(() => |
| 78 | + preflightPropertyListDecode(Buffer.from(xml), 16 * 1024 * 1024), |
| 79 | + ).toThrow( |
| 80 | + "XML plist internal entity expansion exceeds the aggregate Interface Builder decode budget", |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("does not mistake a bracket in a quoted external identifier for an internal subset", () => { |
| 85 | + const xml = archive("Build").replace( |
| 86 | + "<plist", |
| 87 | + '<!DOCTYPE plist SYSTEM "example[edition].dtd"><plist', |
| 88 | + ); |
| 89 | + expect(() => parseXmlPropertyList(xml)).not.toThrow(); |
| 90 | + expect(() => |
| 91 | + preflightPropertyListDecode(Buffer.from(xml), 16 * 1024 * 1024), |
| 92 | + ).not.toThrow(); |
| 93 | + }); |
| 94 | + |
| 95 | + it.each([ |
| 96 | + '<!DOCTYPE plist SYSTEM "example>[edition].dtd">', |
| 97 | + "<!DOCTYPE plist SYSTEM 'example>[edition].dtd'>", |
| 98 | + "<?example <!DOCTYPE document [ harmless example ]> ?>", |
| 99 | + ])("consumes quoted or opaque declaration text once: %s", (declaration) => { |
| 100 | + const xml = archive("Build").replace("<plist", `${declaration}<plist`); |
| 101 | + expect(() => parseXmlPropertyList(xml)).not.toThrow(); |
| 102 | + expect( |
| 103 | + preflightPropertyListDecode(Buffer.from(xml), 16 * 1024 * 1024), |
| 104 | + ).toMatchObject({ binaryPrototypeKeyObserved: false }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments