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
fix(apple): charge XML references only where the parser expands them
The previous commit charged every "&" in the document as a reference,
including literal ampersands inside CDATA sections and comments. A valid
plist with one CDATA string of 1,000,000 ampersands was refused although
it is a single node the decoder holds as one string.

Count "&" during the existing token walk, only in text runs and in the
attribute values of start tags, so comments, CDATA, PIs and declarations
add no reference charge and the scan stays a single pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
kaoru0822-kitauji and claude committed Oct 30, 2026
commit 0be3ca3115ad84f0571d1daa4dee01d086b55466
31 changes: 31 additions & 0 deletions src/artifacts/apple/PropertyListDecodeBudget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ it.each(["é", "_", ":", "雪", "\u{1d4b3}"])(
},
);

it("refuses text whose entity references exceed the decode budget", () => {
const parsed = parsePlistXmlSource(
xmlPlist(`<array><string>${"&amp;".repeat(1_000_000)}</string></array>`),
);
expect(parsed.ok).toBe(false);
if (parsed.ok) throw new Error("Expected a decode budget refusal");
expect(parsed.error).toBeInstanceOf(AnalysisResourceConstraintError);
});

// Opaque sections hold literal ampersands, not references the parser expands.
it.each([
["a CDATA string", `<string><![CDATA[${"&".repeat(1_000_000)}]]></string>`],
["a comment", `<!--${"&".repeat(1_000_000)}--><true/>`],
])("admits literal ampersands in %s", (_name, value) => {
const parsed = parsePlistXmlSource(xmlPlist(`<array>${value}</array>`));
if (!parsed.ok) throw parsed.error;
expect(parsed.value.value).toEqual([
value.startsWith("<string>") ? "&".repeat(1_000_000) : true,
]);
});

it("charges references only in text and attribute values", () => {
const mixed =
'<array><string a="&amp;">&lt;<![CDATA[&&&]]>&gt;</string><?pi &?></array>';
const estimate = (body: string) =>
preflightPropertyListDecode(xmlPlist(body), Number.MAX_SAFE_INTEGER)
.estimatedBytes;
// Same length and nodes without any "&": only the three references differ.
expect(estimate(mixed) - estimate(mixed.replaceAll("&", "x"))).toBe(3 * 64);
});

it("admits an ordinary pretty-printed XML plist", () => {
const entry = (index: number) =>
`\t<dict>\n${Array.from(
Expand Down
49 changes: 34 additions & 15 deletions src/artifacts/apple/PropertyListDecodeBudget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,8 @@ const estimateXmlPlistExpansion = (
) => Error = propertyListDecodeBudgetFailure,
): number => {
const xml = xmlText ?? decodeXmlPlistText(bytes);
// 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.
// The decoded text and the DOM's copies of its character data.
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;
}
const elementBytes =
XML_ELEMENT_BYTES +
(numberProjection ? XML_NUMBER_PROJECTION_ELEMENT_BYTES : 0);
Expand All @@ -108,6 +99,7 @@ const estimateXmlPlistExpansion = (
`XML plist internal entity expansion exceeds ${budget}`,
);
if (token.text) estimate += XML_NODE_BYTES;
estimate += token.references * XML_REFERENCE_BYTES;
if (token.kind === "element")
estimate += elementBytes + token.attributes * XML_NODE_BYTES;
else if (token.kind === "node") estimate += XML_NODE_BYTES;
Expand All @@ -118,24 +110,41 @@ const estimateXmlPlistExpansion = (

/**
* Consume each XML span once; malformed tokens cannot restart suffix scans.
* Each token reports the DOM node it creates (none for an end tag) and
* whether character data, which becomes a text node, precedes it.
* Each token reports the DOM node it creates (none for an end tag), whether
* character data, which becomes a text node, precedes it, and the references
* the parser expands in that text and in the tag's attribute values. Opaque
* comment, CDATA, PI and declaration content holds no references.
*/
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;
/** "&" in the preceding text and in this start tag's attribute values. */
readonly references: number;
readonly internalSubset: boolean;
}> {
const textReferences = (from: number, to: number): number => {
let count = 0;
for (let index = from; index < to; index += 1)
if (xml.charCodeAt(index) === 0x26) count += 1;
return count;
};
let cursor = 0;
while (cursor < xml.length) {
const start = xml.indexOf("<", cursor);
if (start < 0) {
yield { kind: "none", text: true, attributes: 0, internalSubset: false };
yield {
kind: "none",
text: true,
attributes: 0,
references: textReferences(cursor, xml.length),
internalSubset: false,
};
return;
}
const text = start > cursor;
const references = textReferences(cursor, start);
const opaqueEnd = xml.startsWith("<!--", start)
? "-->"
: xml.startsWith("<![CDATA[", start)
Expand All @@ -147,7 +156,13 @@ function* xmlPreflightTokens(xml: string): Generator<{
const end = xml.indexOf(opaqueEnd, start + 2);
if (end < 0) return;
cursor = end + opaqueEnd.length;
yield { kind: "node", text, attributes: 0, internalSubset: false };
yield {
kind: "node",
text,
attributes: 0,
references,
internalSubset: false,
};
continue;
}
// Classify by the character after "<" rather than a name pattern: any
Expand All @@ -157,14 +172,17 @@ function* xmlPreflightTokens(xml: string): Generator<{
const doctype = /^<!DOCTYPE\b/iu.test(xml.slice(start, start + 10));
const declaration = xml[start + 1] === "!";
const endTag = xml[start + 1] === "/";
const element = !declaration && !endTag;
let quote: string | undefined;
let internalSubset = false;
let attributes = 0;
let attributeReferences = 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 (element && character === "&") attributeReferences += 1;
} else if (character === '"' || character === "'") quote = character;
else if (character === ">") break;
else if (character === "=") attributes += 1;
Expand All @@ -173,9 +191,10 @@ function* xmlPreflightTokens(xml: string): Generator<{
if (end === xml.length) return;
cursor = end + 1;
yield {
kind: endTag ? "none" : declaration ? "node" : "element",
kind: element ? "element" : declaration ? "node" : "none",
text,
attributes,
references: references + attributeReferences,
internalSubset,
};
}
Expand Down
Loading