fix(plist): read XML plists containing a __proto__ key
The `plist` XML decoder throws on the legal dictionary key `__proto__`
(its CVE-2022-22912 guard). An app whose XML Info.plist held such an
entry could not be opened at all ("app has no readable
CFBundleExecutable"), and entitlements, XML keyed archives, and
Interface Builder plists failed the same way. On the plutil JSON route,
validation silently dropped the key from `inspect_plist` results.
Decode XML plists through a shared reader that substitutes such keys
before the decoder runs, then omits and counts those entries.
`inspect_plist` and `inspect_keyed_archive` report the omission as a
limitation instead of failing or dropping it silently.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
507e8b3c7b
commit
a08be51e9f
@@ -0,0 +1,53 @@
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
omitPrototypeKeys,
|
||||
omittedPrototypeKeysLimitation,
|
||||
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>",
|
||||
])("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("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,
|
||||
});
|
||||
expect(omittedPrototypeKeysLimitation(1)).toBe(
|
||||
"1 dictionary entry keyed __proto__ was omitted because REA results cannot represent that key.",
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,88 @@
|
||||
import { parse } from "plist";
|
||||
|
||||
/** The dictionary key REA results cannot carry: validation strips it. */
|
||||
const PROTOTYPE_KEY = "__proto__";
|
||||
|
||||
/** A decoded plist without `__proto__` entries, and how many were omitted. */
|
||||
export interface PropertyListWithoutPrototypeKeys {
|
||||
readonly value: unknown;
|
||||
readonly omittedPrototypeKeys: number;
|
||||
}
|
||||
|
||||
/** Limitation for results whose source plist held `__proto__` entries. */
|
||||
export const omittedPrototypeKeysLimitation = (count: number): string =>
|
||||
`${String(count)} dictionary ${count === 1 ? "entry" : "entries"} keyed __proto__ ${count === 1 ? "was" : "were"} omitted because REA results cannot represent that key.`;
|
||||
|
||||
const withoutKey = (
|
||||
value: unknown,
|
||||
key: string,
|
||||
): PropertyListWithoutPrototypeKeys => {
|
||||
let omitted = 0;
|
||||
const strip = (item: unknown): unknown => {
|
||||
if (Array.isArray(item)) return item.map(strip);
|
||||
if (
|
||||
item === null ||
|
||||
typeof item !== "object" ||
|
||||
item instanceof Date ||
|
||||
item instanceof Uint8Array
|
||||
)
|
||||
return item;
|
||||
return Object.fromEntries(
|
||||
Object.entries(item).flatMap(([name, entry]) => {
|
||||
if (name !== key) return [[name, strip(entry)]];
|
||||
omitted += 1;
|
||||
return [];
|
||||
}),
|
||||
);
|
||||
};
|
||||
const stripped = strip(value);
|
||||
return { value: stripped, omittedPrototypeKeys: omitted };
|
||||
};
|
||||
|
||||
/** Remove own `__proto__` entries, such as JSON.parse creates, and count them. */
|
||||
export const omitPrototypeKeys = (
|
||||
value: unknown,
|
||||
): PropertyListWithoutPrototypeKeys => withoutKey(value, PROTOTYPE_KEY);
|
||||
|
||||
// Comments and CDATA text are consumed whole so only element keys match.
|
||||
const KEY_TOKEN =
|
||||
/<!--[\s\S]*?-->|<!\[CDATA\[[\s\S]*?\]\]>|<key>([^<]*)<\/key>|<key><!\[CDATA\[([\s\S]*?)\]\]><\/key>/gu;
|
||||
|
||||
const XML_ENTITIES: Readonly<Record<string, string>> = {
|
||||
amp: "&",
|
||||
apos: "'",
|
||||
gt: ">",
|
||||
lt: "<",
|
||||
quot: '"',
|
||||
};
|
||||
|
||||
const decodeXmlText = (text: string): string =>
|
||||
text.replace(
|
||||
/&(?:#x([\da-f]+)|#(\d+)|([a-z]+));/giu,
|
||||
(entity, hex?: string, decimal?: string, name?: string) => {
|
||||
if (hex !== undefined) return String.fromCodePoint(parseInt(hex, 16));
|
||||
if (decimal !== undefined)
|
||||
return String.fromCodePoint(parseInt(decimal, 10));
|
||||
return (name === undefined ? undefined : XML_ENTITIES[name]) ?? entity;
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* Decode an XML plist whose dictionaries may use the legal key `__proto__`,
|
||||
* which the `plist` decoder rejects outright. Those entries are omitted and
|
||||
* counted so callers can report them instead of failing the whole document.
|
||||
*/
|
||||
export const parseXmlPropertyList = (
|
||||
text: string,
|
||||
): PropertyListWithoutPrototypeKeys => {
|
||||
let placeholder = "__rea_prototype_key__";
|
||||
while (text.includes(placeholder)) placeholder = `_${placeholder}`;
|
||||
const substituted = text.replace(
|
||||
KEY_TOKEN,
|
||||
(token, plain?: string, cdata?: string) =>
|
||||
(plain === undefined ? cdata : decodeXmlText(plain)) === PROTOTYPE_KEY
|
||||
? `<key>${placeholder}</key>`
|
||||
: token,
|
||||
);
|
||||
return withoutKey(parse(substituted), placeholder);
|
||||
};
|
||||
Reference in New Issue
Block a user