fix(artifacts): respect XML plist byte-order marks (#514)

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
Rudy Mizrahi Celekli
2026-10-05 14:59:21 +08:00
committed by GitHub
parent 80777d96da
commit 23c4eba5b3
5 changed files with 190 additions and 1 deletions
+22 -1
View File
@@ -1,4 +1,5 @@
import { createHash } from "node:crypto";
import { TextDecoder } from "node:util";
import { parse, parseBinary } from "plist";
import { z } from "zod";
import type { JsonValue } from "../domain/jsonValue.js";
@@ -22,13 +23,33 @@ export const decodeKeyedArchiveBytes = (
const binary = bytes.subarray(0, 8).toString("ascii") === "bplist00";
const parsed: unknown = binary
? parseBinary(bytes)
: parse(bytes.toString("utf8"));
: parse(decodeXmlPlistText(bytes));
return {
archive_format: binary ? ("binary-plist" as const) : ("xml-plist" as const),
...projectKeyedArchive(normalizePlist(parsed), selection),
};
};
const decodeXmlPlistText = (bytes: Buffer): string => {
const encoding =
bytes[0] === 0xff && bytes[1] === 0xfe
? "utf-16le"
: bytes[0] === 0xfe && bytes[1] === 0xff
? "utf-16be"
: "utf-8";
const text = new TextDecoder(encoding, { fatal: true }).decode(bytes);
const declared = /^<\?xml\s[^?]*\bencoding\s*=\s*["']([^"']+)["']/u
.exec(text)?.[1]
?.toLowerCase();
if (
declared !== undefined &&
((encoding !== "utf-8" && declared !== encoding && declared !== "utf-16") ||
(encoding === "utf-8" && declared.startsWith("utf-16")))
)
throw new TypeError("XML encoding declaration disagrees with its bytes");
return text;
};
const normalizePlist = (value: unknown, depth = 0): JsonValue => {
if (depth > 128) throw new RangeError("Plist exceeds 128 nesting levels");
if (value instanceof Uint8Array)
@@ -0,0 +1,84 @@
import { createHash } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { buildBinary, parse } from "plist";
import { expect, it } from "vitest";
import { inspectBundleKeyedArchive } from "../../../../src/artifacts/KeyedArchiveReader.js";
import { nativeFixture } from "../../../fixtures/nativeCommands.js";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
it.each(["utf8", "utf8-bom", "utf16le", "utf16be", "binary"] as const)(
"reads the same captured Foundation graph encoded as %s",
async (encoding) => {
const root = await createTestTempDirectory("rea-keyed-encoding-");
const xml = await nativeFixture("archive-xml-encoding/archive.xml");
const utf16 = Buffer.from(xml.replace("UTF-8", "UTF-16"), "utf16le");
const bytes =
encoding === "utf8"
? Buffer.from(xml)
: encoding === "utf8-bom"
? Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(xml)])
: encoding === "binary"
? Buffer.from(buildBinary(parse(xml)))
: Buffer.concat([
Buffer.from(
encoding === "utf16le" ? [0xff, 0xfe] : [0xfe, 0xff],
),
encoding === "utf16le" ? utf16 : utf16.swap16(),
]);
await writeFile(join(root, "archive.plist"), bytes);
const digest = createHash("sha256").update(bytes).digest("hex");
const result = await inspectBundleKeyedArchive({
bundlePath: root,
targetSha256: digest,
parameters: { path: "archive.plist", offset: 0, limit: 100 },
});
expect(result.archive_sha256).toBe(digest);
expect(result.archive_format).toBe(
encoding === "binary" ? "binary-plist" : "xml-plist",
);
expect(result.objects).toContainEqual(
expect.objectContaining({ value: "Café" }),
);
expect(
result.references.some((reference) => reference.status === "resolved"),
).toBe(true);
},
);
it("rejects an incomplete UTF-16 code unit instead of substituting text", async () => {
const root = await createTestTempDirectory("rea-keyed-encoding-");
await writeFile(join(root, "archive.plist"), Buffer.from([0xff, 0xfe, 0x3c]));
await expect(
inspectBundleKeyedArchive({
bundlePath: root,
targetSha256: "a".repeat(64),
parameters: { path: "archive.plist" },
}),
).rejects.toMatchObject({ reason: "format" });
});
it.each(["utf8-as-utf16", "utf16-as-utf8"] as const)(
"rejects contradictory XML declaration %s",
async (kind) => {
const root = await createTestTempDirectory("rea-keyed-encoding-");
const xml = await nativeFixture("archive-xml-encoding/archive.xml");
const bytes =
kind === "utf8-as-utf16"
? Buffer.from(xml.replace("UTF-8", "UTF-16"))
: Buffer.concat([
Buffer.from([0xff, 0xfe]),
Buffer.from(xml, "utf16le"),
]);
await writeFile(join(root, "archive.plist"), bytes);
await expect(
inspectBundleKeyedArchive({
bundlePath: root,
targetSha256: "a".repeat(64),
parameters: { path: "archive.plist" },
}),
).rejects.toMatchObject({ reason: "format" });
},
);
@@ -0,0 +1,17 @@
# Foundation XML archive encoding fixture
`producer.swift` creates an actual NSKeyedArchiver XML representation, then
uses Foundation string encoding to produce BOM-marked UTF-16 little- and
big-endian representations of the same XML graph. Foundation's
PropertyListSerialization reads both representations successfully; `plutil
-lint` also accepts all three files.
```sh
xcrun swiftc producer.swift -o writer
./writer archive.xml little-endian.plist big-endian.plist
plutil -lint archive.xml little-endian.plist big-endian.plist
```
Only the UTF-8 XML source capture is committed. The tests encode its same text
as UTF-16 so no compiled binaries or binary fixtures are committed. This is
format/filesystem verification; no archived classes are instantiated by REA.
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>$archiver</key>
<string>NSKeyedArchiver</string>
<key>$objects</key>
<array>
<string>$null</string>
<dict>
<key>$class</key>
<dict>
<key>CF$UID</key>
<integer>4</integer>
</dict>
<key>NS.keys</key>
<array>
<dict>
<key>CF$UID</key>
<integer>2</integer>
</dict>
</array>
<key>NS.objects</key>
<array>
<dict>
<key>CF$UID</key>
<integer>3</integer>
</dict>
</array>
</dict>
<string>text</string>
<string>Café</string>
<dict>
<key>$classes</key>
<array>
<string>NSDictionary</string>
<string>NSObject</string>
</array>
<key>$classname</key>
<string>NSDictionary</string>
</dict>
</array>
<key>$top</key>
<dict>
<key>root</key>
<dict>
<key>CF$UID</key>
<integer>1</integer>
</dict>
</dict>
<key>$version</key>
<integer>100000</integer>
</dict>
</plist>
@@ -0,0 +1,13 @@
import Foundation
let archiver = NSKeyedArchiver(requiringSecureCoding: false)
archiver.outputFormat = .xml
archiver.encode(["text": "Café"] as NSDictionary, forKey: NSKeyedArchiveRootObjectKey)
archiver.finishEncoding()
let xml = String(data: archiver.encodedData, encoding: .utf8)!
let utf16 = xml.replacingOccurrences(of: "UTF-8", with: "UTF-16").data(using: .utf16)!
let _: Any = try PropertyListSerialization.propertyList(from: utf16, options: [], format: nil)
try archiver.encodedData.write(to: URL(fileURLWithPath: CommandLine.arguments[1]))
try utf16.write(to: URL(fileURLWithPath: CommandLine.arguments[2]))
let bigEndian = Data([0xfe, 0xff]) + xml.replacingOccurrences(of: "UTF-8", with: "UTF-16").data(using: .utf16BigEndian)!
let _: Any = try PropertyListSerialization.propertyList(from: bigEndian, options: [], format: nil)
try bigEndian.write(to: URL(fileURLWithPath: CommandLine.arguments[3]))