import { expect, it } from "vitest"; import { omitPrototypeKeys, parseXmlPropertyList } from "./propertyListKeys.js"; const plist = (body: string) => `${body}`; it.each([ "__proto__", "", "__proto__", "__proto__", "__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, }); }); it("preserves the pinned decoder's first-child key semantics", () => { const text = plist( "__prokept__proto__omitted", ); 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, }); }); /** The single-member chain of a nested dictionary, down to its deepest value. */ const deepestValue = (value: unknown): unknown => { let cursor = value; while ( cursor !== null && typeof cursor === "object" && !Array.isArray(cursor) && "k" in cursor ) { cursor = (cursor as Record).k; } return cursor; }; /** The single-member chain of a nested array, down to its deepest value. */ const deepestEntry = (value: unknown): unknown => { let cursor = value; while (Array.isArray(cursor) && cursor.length === 1) cursor = cursor[0]; return cursor; }; const nestedDictionaries = (depth: number) => plist( "k".repeat(depth) + "x" + "".repeat(depth), ); const nestedArrays = (depth: number) => plist( "".repeat(depth) + "x" + "".repeat(depth), ); it("rejects XML nesting beyond the projection limit with the limit diagnostic", () => { // `plist` and the prototype-key traversal recurse once per level, so the // depth check must report the same bounded-resource error as the projection // instead of letting those recursive passes exhaust the call stack. const limit = new RangeError("Plist exceeds 128 nesting levels"); expect(() => parseXmlPropertyList(nestedDictionaries(129))).toThrow(limit); expect(() => parseXmlPropertyList(nestedArrays(129))).toThrow(limit); expect(() => parseXmlPropertyList(nestedDictionaries(3000))).toThrow(limit); expect(() => parseXmlPropertyList(nestedArrays(4000))).toThrow(limit); }); it("decodes XML property lists exactly at the projection nesting limit", () => { expect( deepestValue(parseXmlPropertyList(nestedDictionaries(128)).value), ).toBe("x"); expect(deepestEntry(parseXmlPropertyList(nestedArrays(128)).value)).toBe("x"); expect( parseXmlPropertyList( plist("".repeat(128) + "" + "".repeat(128)), ).value, ).toBeDefined(); }); it("does not count markup inside comments, CDATA, or the doctype as nesting", () => { const text = ` ">]> a]]> bx `; expect(parseXmlPropertyList(text)).toEqual({ value: { a: "", b: ["x"] }, omittedPrototypeKeys: 0, }); }); it.each(['"', "'"])( "counts containers with quoted XML delimiters using %s attributes", (quote) => { const opening = `${quote}>k`; const text = plist( opening.repeat(3000) + "x" + "".repeat(3000), ); expect(() => parseXmlPropertyList(text)).toThrow( new RangeError("Plist exceeds 128 nesting levels"), ); expect( parseXmlPropertyList( plist( "" + `${quote}/>`.repeat(130) + "", ), ).value, ).toEqual(Array.from({ length: 130 }, () => ({}))); }, ); it("ignores complete DOCTYPE internal subsets when checking value depth", () => { const text = ` ".repeat(130)}"> ]>x`; expect(parseXmlPropertyList(text)).toEqual({ value: "x", omittedPrototypeKeys: 0, }); }); it("bounds nested plist wrappers before recursive decoding", () => { const text = "".repeat(3000) + "x" + "".repeat(3000); expect(() => parseXmlPropertyList(text)).toThrow( new RangeError("Plist exceeds 128 nesting levels"), ); expect( parseXmlPropertyList(plist("x")).value, ).toEqual(["x"]); }); it.each(["\n", "\r\n", "\r", "\u0085", "\u2028", "\u2029"])( "preserves unrelated source values around %j line endings", (newline) => { const text = plist( `${newline}kept keya b…c
d${newline}__proto__omitted`, ); 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( `${lines}__proto__omittednested${lines}__proto__omittedkept`, ); expect(parseXmlPropertyList(text)).toEqual({ value: { nested: { kept: true } }, omittedPrototypeKeys: 2, }); });