Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(plist): reject malformed XML before value decoding

morlutoc7e9e096d3e2
diff --git a/src/domain/propertyListKeys.ts b/src/domain/propertyListKeys.ts
index 47f65f35a..de929146b 100644
--- a/src/domain/propertyListKeys.ts
+++ b/src/domain/propertyListKeys.ts
@@ -1,12 +1,8 @@
-import {
- DOMParser,
- normalizeLineEndings,
- type Document,
- type Node,
-} from "@xmldom/xmldom";
+import { normalizeLineEndings, type Document, type Node } from "@xmldom/xmldom";
import { parse, parseOpenStep } from "plist";
import { MAX_PLIST_DEPTH } from "./apple/plistValue.js";
+import { parseXmlDocument } from "./xmlDocument.js";
/** The dictionary key REA results cannot carry: validation strips it. */
const PROTOTYPE_KEY = "__proto__";
@@ -108,10 +104,14 @@ export const parseXmlPropertyList = (
// Preserve character references in the source: serializing a DOM would turn
// e.g. an encoded carriage return into a literal newline on the second parse.
const normalized = normalizeLineEndings(text);
- // The pinned XML parser is iterative. Reuse its document for the depth
- // preflight and key lookup so quoted attributes and declarations have the
- // same meaning at both boundaries, before plist's recursive value decode.
- const document = new DOMParser().parseFromString(normalized, "text/xml");
+ // Validate before plist's recovering XML decoder can turn malformed source
+ // into observed values. Reuse the validated DOM for depth and key ownership.
+ const parsed = parseXmlDocument(normalized, {
+ doctype: "allow",
+ lineEndings: "preserve",
+ });
+ if (!parsed.ok) throw parsed.error;
+ const document = parsed.value;
assertXmlPlistDepthWithinLimit(document);
const { substituted, placeholder } = substitutePrototypeKeys(
normalized,
diff --git a/tests/boundary/filesystem/keyedArchiveXmlSyntax.test.ts b/tests/boundary/filesystem/keyedArchiveXmlSyntax.test.ts
new file mode 100644
index 000000000..6814efbcf
--- /dev/null
+++ b/tests/boundary/filesystem/keyedArchiveXmlSyntax.test.ts
@@ -0,0 +1,68 @@
+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 { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+
+const archive = build({
+ $archiver: "NSKeyedArchiver",
+ $version: 100000,
+ $objects: ["$null", "value"],
+ $top: { root: { UID: 1 } },
+});
+
+it.each([
+ {
+ name: "unquoted attribute",
+ xml: archive.replace('version="1.0">', "version=1.0>"),
+ },
+ {
+ name: "undeclared entity",
+ xml: archive.replace(
+ "<string>value</string>",
+ "<string>&unknown;</string>",
+ ),
+ },
+ {
+ name: "forbidden character reference",
+ xml: archive.replace("<string>value</string>", "<string>&#0;</string>"),
+ },
+ {
+ name: "forbidden literal character",
+ xml: archive.replace("<string>value</string>", "<string>\u0000</string>"),
+ },
+])("refuses $name before registering archive observations", async ({ xml }) => {
+ const root = await createTestTempDirectory("rea-keyed-xml-syntax-");
+ await writeFile(join(root, "Archive.plist"), xml);
+ await expect(
+ inspectBundleKeyedArchive({
+ bundlePath: root,
+ targetSha256: "a".repeat(64),
+ parameters: { path: "Archive.plist" },
+ }),
+ ).rejects.toMatchObject({
+ reason: "format",
+ cause: { _tag: "XmlDocumentParseError", reason: "malformed" },
+ });
+});
+
+it("preserves valid Unicode, entity values and original archive identities", async () => {
+ const root = await createTestTempDirectory("rea-keyed-xml-valid-");
+ await writeFile(
+ join(root, "Archive.plist"),
+ archive.replace("<string>value</string>", "<string>� &amp; &#13;</string>"),
+ );
+ const result = await inspectBundleKeyedArchive({
+ bundlePath: root,
+ targetSha256: "a".repeat(64),
+ parameters: { path: "Archive.plist", offset: 1, limit: 1 },
+ });
+ expect(result.roots).toEqual({ root: { UID: 1 } });
+ expect(result.objects).toEqual([
+ expect.objectContaining({ id: 1, status: "observed", value: "� & \r" }),
+ ]);
+ expect(result.total_objects).toBe(2);
+ expect(result.next_offset).toBeNull();
+ expect(result.truncated).toBe(true);
+});