124 lines
4.2 KiB
TypeScript
124 lines
4.2 KiB
TypeScript
import { expect, it } from "vitest";
|
|
|
|
import { omitPrototypeKeys, parseXmlPropertyList } from "./propertyListKeys.js";
|
|
|
|
const plist = (body: string) =>
|
|
`<?xml version="1.0" encoding="UTF-8"?><plist version="1.0">${body}</plist>`;
|
|
|
|
it.each([
|
|
"<key>__proto__</key>",
|
|
"<key><![CDATA[__proto__]]></key>",
|
|
"<key>__proto__</key>",
|
|
"<key >__proto__</key >",
|
|
"<key\n>__proto__</key>",
|
|
"<key>__proto__<!-- trailing comment --></key>",
|
|
"<key><![CDATA[__proto__]]><!-- trailing comment --></key>",
|
|
])("omits and counts a dictionary keyed %s", (key) => {
|
|
const parsed = parseXmlPropertyList(
|
|
plist(
|
|
`<dict><key>CFBundleExecutable</key><string>App</string>${key}<dict><key>polluted</key><true/></dict><key>list</key><array><dict>${key}<string>x</string><key>kept</key><integer>1</integer></dict></array></dict>`,
|
|
),
|
|
);
|
|
expect(parsed).toEqual({
|
|
value: { CFBundleExecutable: "App", list: [{ kept: 1 }] },
|
|
omittedPrototypeKeys: 2,
|
|
});
|
|
expect(Object.getPrototypeOf(parsed.value)).toBe(Object.prototype);
|
|
});
|
|
|
|
it("keeps __proto__ text in strings, comments, and similar keys", () => {
|
|
const text = plist(
|
|
"<dict><!-- <key>__proto__</key> --><key> __proto__ </key><string><![CDATA[<key>__proto__</key>]]></string><key>__rea_prototype_key__</key><string>__proto__</string></dict>",
|
|
);
|
|
expect(parseXmlPropertyList(text)).toEqual({
|
|
value: {
|
|
" __proto__ ": "<key>__proto__</key>",
|
|
__rea_prototype_key__: "__proto__",
|
|
},
|
|
omittedPrototypeKeys: 0,
|
|
});
|
|
});
|
|
|
|
it("keeps an entity-encoded key that decodes to the placeholder", () => {
|
|
const text = plist(
|
|
"<dict><key>__rea_prototype_key__</key><string>kept</string><key>__proto__</key><string>x</string></dict>",
|
|
);
|
|
expect(parseXmlPropertyList(text)).toEqual({
|
|
value: { __rea_prototype_key__: "kept" },
|
|
omittedPrototypeKeys: 1,
|
|
});
|
|
});
|
|
|
|
it("keeps entity-like text beyond Unicode in comments and CDATA", () => {
|
|
const text = plist(
|
|
"<!-- � --><dict><key>raw</key><string><![CDATA[�]]></string><key>__proto__</key><string>x</string></dict>",
|
|
);
|
|
expect(parseXmlPropertyList(text)).toEqual({
|
|
value: { raw: "�" },
|
|
omittedPrototypeKeys: 1,
|
|
});
|
|
});
|
|
|
|
it("omits own __proto__ entries that JSON decoding creates", () => {
|
|
const decoded: unknown = JSON.parse(
|
|
'{"__proto__":{"x":1},"a":[{"__proto__":2,"b":3}]}',
|
|
);
|
|
expect(omitPrototypeKeys(decoded)).toEqual({
|
|
value: { a: [{ b: 3 }] },
|
|
omittedPrototypeKeys: 2,
|
|
});
|
|
});
|
|
|
|
it("preserves the pinned decoder's first-child key semantics", () => {
|
|
const text = plist(
|
|
"<dict><key>__pro<![CDATA[to__]]></key><string>kept</string><key>__proto__</key><string>omitted</string></dict>",
|
|
);
|
|
expect(parseXmlPropertyList(text)).toEqual({
|
|
value: { __pro: "kept" },
|
|
omittedPrototypeKeys: 1,
|
|
});
|
|
});
|
|
|
|
it("preserves OpenStep dictionary and array autodetection", () => {
|
|
expect(
|
|
parseXmlPropertyList(
|
|
" { CFBundleExecutable = App; values = (one, two); __rea_prototype_key__ = kept; }",
|
|
),
|
|
).toEqual({
|
|
value: {
|
|
CFBundleExecutable: "App",
|
|
values: ["one", "two"],
|
|
__rea_prototype_key__: "kept",
|
|
},
|
|
omittedPrototypeKeys: 0,
|
|
});
|
|
expect(parseXmlPropertyList("(one, two)")).toEqual({
|
|
value: ["one", "two"],
|
|
omittedPrototypeKeys: 0,
|
|
});
|
|
});
|
|
|
|
it.each(["\n", "\r\n", "\r", "\u0085", "\u2028", "\u2029"])(
|
|
"preserves unrelated source values around %j line endings",
|
|
(newline) => {
|
|
const text = plist(
|
|
`<dict>${newline}<key>kept key</key><string>a b…c
d</string>${newline}<key${newline}>__proto__<!--tail--></key><string>omitted</string></dict>`,
|
|
);
|
|
expect(parseXmlPropertyList(text)).toEqual({
|
|
value: { "kept\rkey": "a\rb\u0085c\u2028d" },
|
|
omittedPrototypeKeys: 1,
|
|
});
|
|
},
|
|
);
|
|
|
|
it("locates sparse omitted keys across newline-dense source", () => {
|
|
const lines = "\n".repeat(64 * 1024);
|
|
const text = plist(
|
|
`<dict>${lines}<key >__proto__</key><string>omitted</string><key>nested</key><dict>${lines}<key>__proto__<!--tail--></key><string>omitted</string><key>kept</key><true/></dict></dict>`,
|
|
);
|
|
expect(parseXmlPropertyList(text)).toEqual({
|
|
value: { nested: { kept: true } },
|
|
omittedPrototypeKeys: 2,
|
|
});
|
|
});
|