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:
@@ -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]))
|
||||
Reference in New Issue
Block a user