Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(apple): charge every XML element, attribute and reference
The XML preflight recognized only element names starting with an ASCII
letter, so names such as é, _, a CJK or astral character escaped the
element charge: 100,000 <é/> elements (0.5 MB) built an 84 MiB DOM before
the plist decoder rejected the tag. Attributes and entity or character
references were also uncharged; 20 attributes per element measured about
210 bytes each, and each reference about 26 bytes beyond its characters.

Classify markup by the character after "<" instead of a name pattern, so
anything other than an end tag, comment, CDATA, PI or declaration counts as
an element (saxes rejects malformed markup before the DOM is built). Charge
each unquoted "=" in a start tag as an attribute node and every "&" as a
reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
kaoru0822-kitauji and claude committed Oct 30, 2026
commit c940be453e8734c8f594c8e60ac3808f8ab2ba16
17 changes: 17 additions & 0 deletions src/artifacts/apple/PropertyListDecodeBudget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ it("refuses an XML plist whose DOM exceeds the native decode budget", () => {
expect(parsed.error.message).toContain("native plist decode budget");
});

// Saxes accepts any XML NameStartChar, and the DOM holds every element
// before the plist decoder rejects an unknown tag.
it.each(["é", "_", ":", "雪", "\u{1d4b3}"])(
"refuses 100,000 elements whose names start with %s before building the DOM",
(name) => {
const parsed = parsePlistXmlSource(
xmlPlist(`<array>${`<${name}/>`.repeat(100_000)}</array>`),
);
expect(parsed.ok).toBe(false);
if (parsed.ok) throw new Error("Expected a decode budget refusal");
expect(parsed.error).toBeInstanceOf(AnalysisResourceConstraintError);
},
);

it("admits an ordinary pretty-printed XML plist", () => {
const entry = (index: number) =>
`\t<dict>\n${Array.from(
Expand All @@ -41,6 +55,9 @@ it.each([
["text after elements", "<true/>\n", 1280 + 384],
["comments", "<!---->", 384],
["CDATA sections", "<string><![CDATA[a]]></string>", 1280 + 384],
["non-ASCII elements", "<é/>", 1280],
["attributes", '<true a="x>y" b="z"/>', 1280 + 2 * 384],
["references", "<string>&amp;&#x41;</string>", 1280 + 384 + 2 * 64],
])(
"charges %s for the DOM nodes the decoder builds",
(_name, item, perItem) => {
Expand Down
48 changes: 34 additions & 14 deletions src/artifacts/apple/PropertyListDecodeBudget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ export const preflightPropertyListDecode = (

/**
* Peak heap of the XML decoder's DOM: each element measured 1,000–1,160
* bytes, and each text, comment or CDATA node up to 240 bytes, with
* @xmldom/xmldom on 64-bit Node. Number projection adds literal metadata.
* bytes, each text, comment, CDATA or attribute node up to 240 bytes, and
* each entity or character reference about 26 bytes beyond its characters,
* with @xmldom/xmldom on 64-bit Node. Number projection adds literal metadata.
*/
const XML_ELEMENT_BYTES = 1280;
const XML_NUMBER_PROJECTION_ELEMENT_BYTES = 256;
const XML_NODE_BYTES = 384;
const XML_REFERENCE_BYTES = 64;

const estimateXmlPlistExpansion = (
bytes: Buffer,
Expand All @@ -81,8 +83,17 @@ const estimateXmlPlistExpansion = (
) => Error = propertyListDecodeBudgetFailure,
): number => {
const xml = xmlText ?? decodeXmlPlistText(bytes);
// The decoded text and the DOM's copies of its character data.
// The decoded text and the DOM's copies of its character data. Every "&"
// outside opaque sections starts a reference; counting all is an upper bound.
let estimate = xml.length * 4;
for (
let reference = xml.indexOf("&");
reference >= 0;
reference = xml.indexOf("&", reference + 1)
) {
estimate += XML_REFERENCE_BYTES;
if (estimate > maxBytes) return maxBytes + 1;
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
}
const elementBytes =
XML_ELEMENT_BYTES +
(numberProjection ? XML_NUMBER_PROJECTION_ELEMENT_BYTES : 0);
Expand All @@ -97,7 +108,8 @@ const estimateXmlPlistExpansion = (
`XML plist internal entity expansion exceeds ${budget}`,
);
if (token.text) estimate += XML_NODE_BYTES;
if (token.kind === "element") estimate += elementBytes;
if (token.kind === "element")
estimate += elementBytes + token.attributes * XML_NODE_BYTES;
else if (token.kind === "node") estimate += XML_NODE_BYTES;
if (estimate > maxBytes) return maxBytes + 1;
}
Expand All @@ -112,13 +124,15 @@ const estimateXmlPlistExpansion = (
function* xmlPreflightTokens(xml: string): Generator<{
readonly kind: "element" | "node" | "none";
readonly text: boolean;
/** Unquoted "=" signs; each attribute of a well-formed tag has one. */
readonly attributes: number;
readonly internalSubset: boolean;
}> {
let cursor = 0;
while (cursor < xml.length) {
const start = xml.indexOf("<", cursor);
if (start < 0) {
yield { kind: "none", text: true, internalSubset: false };
yield { kind: "none", text: true, attributes: 0, internalSubset: false };
return;
}
const text = start > cursor;
Expand All @@ -133,31 +147,37 @@ function* xmlPreflightTokens(xml: string): Generator<{
const end = xml.indexOf(opaqueEnd, start + 2);
if (end < 0) return;
cursor = end + opaqueEnd.length;
yield { kind: "node", text, internalSubset: false };
yield { kind: "node", text, attributes: 0, internalSubset: false };
continue;
}
// Classify by the character after "<" rather than a name pattern: any
// other markup starts an element, whatever its name's script. Saxes
// rejects malformed markup before the DOM is built, so over-counting it
// is harmless.
const doctype = /^<!DOCTYPE\b/iu.test(xml.slice(start, start + 10));
const element = /^<[A-Za-z]/u.test(xml.slice(start, start + 2));
const endTag = /^<\/[A-Za-z]/u.test(xml.slice(start, start + 3));
const declaration = xml[start + 1] === "!";
const endTag = xml[start + 1] === "/";
let quote: string | undefined;
let internalSubset = false;
let attributes = 0;
let end = start + 1;
for (; end < xml.length; end += 1) {
const character = xml[end];
if (quote !== undefined) {
if (character === quote) quote = undefined;
} else if (character === '"' || character === "'") quote = character;
else if (character === ">") break;
else if (character === "=") attributes += 1;
else if (doctype && character === "[") internalSubset = true;
}
if (end === xml.length) return;
cursor = end + 1;
if (doctype || element || endTag)
yield {
kind: element ? "element" : doctype ? "node" : "none",
text,
internalSubset,
};
yield {
kind: endTag ? "none" : declaration ? "node" : "element",
text,
attributes,
internalSubset,
};
}
}

Expand Down
Loading