import { expect, it } from "vitest";
import { omitPrototypeKeys, parseXmlPropertyList } from "./propertyListKeys.js";
const plist = (body: string) =>
`${body}`;
it.each([
"__proto__",
"",
"__proto__",
])("omits and counts a dictionary keyed %s", (key) => {
const parsed = parseXmlPropertyList(
plist(
`CFBundleExecutableApp${key}pollutedlist${key}xkept1`,
),
);
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(
" __proto__ __proto__]]>__rea_prototype_key____proto__",
);
expect(parseXmlPropertyList(text)).toEqual({
value: {
" __proto__ ": "__proto__",
__rea_prototype_key__: "__proto__",
},
omittedPrototypeKeys: 0,
});
});
it("keeps an entity-encoded key that decodes to the placeholder", () => {
const text = plist(
"__rea_prototype_key__kept__proto__x",
);
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(
"raw__proto__x",
);
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,
});
});