Files
reverse-engineering-anything/src/domain/xmlDocument.ts
T
Adolanium 06761f2993 fix(xml): bound element depth at the XML syntax boundary (#14998)
* fix(xml): refuse element depth at the syntax check

saxes keeps one handler per event and walks the open-element stack on
every start tag. checkXmlSyntax now owns opentagstart and closetag, runs
every caller handler after the depth check, and refuses depth above 1024
as xml-element-depth. 1024 stays above the desktop XML view cap and the
plist nesting cap. This release adds no runtime override.

* fix(xml): keep the depth limit when package parsers read XML

XAR TOC, installer XML, Maven POM, and VSIX manifest parsing wrapped
every syntax failure as malformed or a node budget. A depth limit is
returned as the limit failure those callers already accept.

* test(xml): cover element depth past the syntax ceiling

A document one element past 1024 fails the syntax check, including a
XAR TOC that used to parse because a later opentagstart handler replaced
the depth guard. A sibling range after a branch at the ceiling still
matches.

* test(plist): keep nesting refusals inside the XML depth ceiling

Plist fixtures deeper than 1024 now fail at syntax, so the nesting-limit
cases sit on the deepest document the syntax check admits. One plist and
one keyed archive assert the syntax limit past that ceiling.

* fix(xml): type the depth guard as a saxes start tag

opentagstart emits a start tag, not the finished SaxesTagNS. The handler call failed typecheck on that assignment.

* fix(xml): report element depth from the package providers

A VSIX manifest or Maven POM past the syntax ceiling is an XML limit. Those providers only translated their own format, so the refusal became an I/O error.

* fix(xml): report element depth as a fixed allowance

* fix(firmware): report SVD element depth as the XML ceiling

* fix(nuget): preserve XML element depth on package XML

* fix(capture): preserve XML element depth in SAZ metadata

* style(nuget): format the package fixture

* fix(plist): report XML element depth from native inspection

* fix(pkg): carry the XML depth allowance through a deep TOC

* test(plist): narrow the XML depth refusal before projection

The native depth failure is an analysis error. Project it after that check.
2026-11-29 13:08:39 -05:00

573 lines
19 KiB
TypeScript

import {
DOMParser,
onErrorStopParsing,
type Document,
type Element,
type Node,
} from "@xmldom/xmldom";
import { SaxesParser, type SaxesStartTagNS, type SaxesTagNS } from "saxes";
import { err, ok, type Result } from "./result.js";
import {
decodeLimit,
malformedInput,
ParseFailureError,
unsupportedInput,
} from "./parseFailure.js";
import {
fixedAllowanceOptions,
type FixedAllowance,
} from "./fixedAllowanceRemediation.js";
import {
isParseLimit,
parseLimitConstraint,
} from "./parseFailureAnalysisError.js";
/** XML 1.0 literal line endings; Unicode separators remain source characters. */
export const XML_LINE_ENDING = /\r\n?|\n/gu;
/** Decode textual true/false after the caller applies its format's whitespace policy. */
export const parseTrueFalseText = (value: string | null): boolean | null => {
const folded = value?.toLowerCase();
if (folded === "true") return true;
if (folded === "false") return false;
return null;
};
/** Match an XML element node regardless of namespace. */
export const isXmlElement = (node: Node | null): node is Element =>
node !== null && node.nodeType === 1;
/** Match an element in no namespace, including under an explicit empty default. */
export const isUnqualifiedXmlElement = (node: Node | null): node is Element =>
isXmlElement(node) && node.namespaceURI === null;
/** Direct child elements in document order. An empty namespace selects unqualified elements. */
export function* iterateXmlChildElements(
element: Element,
local?: string,
namespace?: string,
): Generator<Element, undefined> {
for (
let child = element.firstChild;
child !== null;
child = child.nextSibling
) {
if (
isXmlElement(child) &&
(local === undefined || child.localName === local) &&
(namespace === undefined || (child.namespaceURI ?? "") === namespace)
)
yield child;
}
}
export const xmlChildElements = (element: Element): Element[] => [
...iterateXmlChildElements(element),
];
export const xmlChildrenNamed = (
element: Element,
local: string,
namespace?: string,
): Element[] => [...iterateXmlChildElements(element, local, namespace)];
export const xmlFirstChildNamed = (
element: Element,
local: string,
namespace?: string,
): Element | undefined =>
iterateXmlChildElements(element, local, namespace).next().value;
/** Read a qualified attribute, trimming its value and treating blank text as absent. */
export const xmlTrimmedAttribute = (
element: Element,
name: string,
): string | null => element.getAttribute(name)?.trim() || null;
/** Join direct text and CDATA in order, excluding descendants, then trim once. */
export const xmlTrimmedDirectText = (element: Element): string | null => {
const parts: string[] = [];
for (
let child = element.firstChild;
child !== null;
child = child.nextSibling
) {
if (child.nodeType === 3 || child.nodeType === 4)
parts.push(child.nodeValue ?? "");
}
return parts.join("").trim() || null;
};
const XML_FORMAT = "xml";
/**
* Open elements `checkXmlSyntax` retains. saxes with `xmlns` walks that stack
* for every start tag. 1024 is above the desktop XML view cap of 494 and the
* plist nesting cap of 128, so those later guards still see a document they
* accept. This release has no runtime override.
*/
export const XML_ELEMENT_DEPTH_LIMIT = 1024;
/** The syntax ceiling, reported as one fixed parser allowance. */
export const XML_ELEMENT_DEPTH_ALLOWANCE = {
name: "The XML element depth allowance",
kind: "parser",
limit: XML_ELEMENT_DEPTH_LIMIT,
unit: "levels",
} as const satisfies FixedAllowance & {
readonly limit: number;
readonly unit: string;
};
const xmlDepthRecovery = fixedAllowanceOptions(
XML_ELEMENT_DEPTH_ALLOWANCE.name,
XML_ELEMENT_DEPTH_ALLOWANCE.kind,
{
limit: XML_ELEMENT_DEPTH_ALLOWANCE.limit,
unit: XML_ELEMENT_DEPTH_ALLOWANCE.unit,
},
);
/** How XML failed: ill-formed syntax, a refused DOCTYPE, or the element-depth ceiling. */
export type XmlParseFailure =
| ParseFailureError<"malformed">
| ParseFailureError<"unsupported">
| ParseFailureError<"limit">;
const notWellFormed = (cause?: unknown): ParseFailureError<"malformed"> =>
malformedInput(
XML_FORMAT,
`XML is not well-formed${cause instanceof Error ? `: ${cause.message}` : ""}`,
{ cause },
);
/** A DOCTYPE, or its internal subset, that the document policy refuses. */
const unsupportedDoctype = (
field: "doctype" | "internal-subset",
): ParseFailureError<"unsupported"> =>
unsupportedInput(
XML_FORMAT,
field === "doctype"
? "XML document types are unsupported"
: "XML document type internal subsets are unsupported",
{ field },
);
/** A failure the syntax check threw itself. */
const isXmlFailure = (cause: unknown): cause is XmlParseFailure =>
cause instanceof ParseFailureError &&
cause.failure.format === XML_FORMAT &&
(cause.kind === "malformed" ||
cause.kind === "unsupported" ||
cause.kind === "limit");
const xmlDepthLimit = (depth: number): ParseFailureError<"limit"> =>
decodeLimit(
XML_FORMAT,
`XML element depth exceeds ${String(XML_ELEMENT_DEPTH_LIMIT)}. This release has no runtime override for this ceiling.`,
{
name: "xml-element-depth",
maximum: XML_ELEMENT_DEPTH_LIMIT,
required: depth,
},
);
/** The syntax ceiling, as distinct from a later format's own node budget. */
export const isXmlElementDepthLimit = (
cause: unknown,
): cause is ParseFailureError<"limit"> =>
isParseLimit(cause, XML_FORMAT) &&
cause.failure.limit.name === "xml-element-depth";
/**
* Public refusal for the syntax ceiling. Package providers keep their own
* format limits. `path` is set when the document is the selected artifact.
*/
export const xmlElementDepthConstraint = (
operation: string,
path: string | undefined,
cause: unknown,
) => {
if (!isXmlElementDepthLimit(cause)) return undefined;
return parseLimitConstraint(operation, "memory", cause, {
...(path === undefined ? {} : { path }),
...xmlDepthRecovery,
});
};
/** The failure the syntax check established; any other throw is ill-formed XML. */
const xmlFailure = (cause: unknown): XmlParseFailure =>
isXmlFailure(cause) ? cause : notWellFormed(cause);
/** Whether a DOCTYPE's text, as saxes reports it, opens an internal subset: a `[` outside its quoted literals. */
const declaresInternalSubset = (declaration: string): boolean =>
declaration.replace(/"[^"]*"|'[^']*'/gu, "").includes("[");
/** XML 1.0's `NameStartChar`, and the further characters `NameChar` allows. */
const NAME_START = String.raw`:A-Z_a-z\u{C0}-\u{D6}\u{D8}-\u{F6}\u{F8}-\u{2FF}\u{370}-\u{37D}\u{37F}-\u{1FFF}\u{200C}\u{200D}\u{2070}-\u{218F}\u{2C00}-\u{2FEF}\u{3001}-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFFD}\u{10000}-\u{EFFFF}`;
const NAME_MORE = String.raw`\-.0-9\u{B7}\u{300}-\u{36F}\u{203F}\u{2040}`;
/** XML element opening grammar shared by the desktop format readers. */
export const XML_ELEMENT_OPENING = String.raw`<[${NAME_START}][${NAME_START}${NAME_MORE}]*(?=[ \t\r\n]|/?>)`;
const XML_OPENING = new RegExp(
`^(?:<\\?xml\\b|<!DOCTYPE\\b|${XML_ELEMENT_OPENING})`,
"u",
);
/** Whether text begins with a possible XML declaration, DOCTYPE or start tag. */
export const opensLikeXml = (source: string): boolean =>
XML_OPENING.test(source);
/** XML 1.0's `PubidChar` but the apostrophe, which only a double-quoted literal may hold. */
const PUBID = String.raw`\n\r a-zA-Z0-9\-()+,./:=?;!*#@$_%`;
const SPACE = String.raw`[ \t\r\n]`;
/**
* A DOCTYPE without an internal subset, as XML 1.0's `doctypedecl` spells it
* between `<!DOCTYPE` and `>`: a name, then optionally `SYSTEM` and a literal,
* or `PUBLIC` and a public identifier and a literal.
*/
const EXTERNAL_DOCTYPE = new RegExp(
`^${SPACE}+[${NAME_START}][${NAME_START}${NAME_MORE}]*` +
`(?:${SPACE}+(?:SYSTEM|PUBLIC${SPACE}+(?:"[${PUBID}']*"|'[${PUBID}]*'))` +
`${SPACE}+(?:"[^"]*"|'[^']*'))?${SPACE}*$`,
"u",
);
/**
* Which DOCTYPE a document may have: any, none, or with `external` one
* without an internal subset, so the document declares no entity. Neither
* parser reads an external DTD.
*/
type DoctypePolicy = "allow" | "reject" | "external";
/**
* Read XML syntax as a stream, throwing at the first error; it holds only the
* open elements. Saxes reads a DOCTYPE without checking it, so the policy
* checks one it admits without an internal subset against XML's grammar.
* Element depth is refused here, before a listener or a later format guard
* sees the over-deep start tag.
*/
const checkXmlSyntax = (
source: string,
doctype: DoctypePolicy,
listen?: (syntax: SaxesParser<{ xmlns: true }>) => void,
nameSupported?: (name: string) => boolean,
): void => {
const syntax = new SaxesParser({
xmlns: true,
defaultXMLVersion: "1.0",
forceXMLVersion: true,
});
// saxes keeps one handler per event and silently overwrites the previous
// one. The depth guard owns opentagstart and closetag, then runs every
// handler a caller sets.
let depth = 0;
const openHandlers: ((tag: SaxesStartTagNS) => void)[] = [];
const closeHandlers: ((...args: readonly unknown[]) => void)[] = [];
syntax.on("opentagstart", (tag) => {
depth += 1;
if (depth > XML_ELEMENT_DEPTH_LIMIT) throw xmlDepthLimit(depth);
for (const handler of openHandlers) handler(tag);
});
syntax.on("closetag", (...args: readonly unknown[]) => {
for (const handler of closeHandlers) handler(...args);
if (depth > 0) depth -= 1;
});
const install = syntax.on.bind(syntax);
const remove = syntax.off.bind(syntax);
syntax.on = ((name, handler) => {
if (name === "opentagstart") {
openHandlers.push(handler as (tag: SaxesStartTagNS) => void);
return;
}
if (name === "closetag") {
closeHandlers.push(handler as (...args: readonly unknown[]) => void);
return;
}
install(name, handler);
}) as typeof syntax.on;
syntax.off = ((name) => {
if (name === "opentagstart") {
openHandlers.length = 0;
return;
}
if (name === "closetag") {
closeHandlers.length = 0;
return;
}
remove(name);
}) as typeof syntax.off;
listen?.(syntax);
const checkName = (name: string) => {
if (nameSupported !== undefined && !nameSupported(name))
throw notWellFormed();
};
if (nameSupported !== undefined) {
syntax.on("opentagstart", (tag) => checkName(tag.name));
syntax.on("attribute", (attribute) => checkName(attribute.name));
syntax.on("processinginstruction", (instruction) =>
checkName(instruction.target),
);
}
if (doctype !== "allow")
syntax.on("doctype", (declaration) => {
if (doctype === "reject") throw unsupportedDoctype("doctype");
if (declaresInternalSubset(declaration))
throw unsupportedDoctype("internal-subset");
if (!EXTERNAL_DOCTYPE.test(declaration)) throw notWellFormed();
checkName(declaration.trimStart().split(/[ \t\r\n]/u, 1)[0] ?? "");
});
syntax.write(source).close();
};
/**
* Whether XML passes the syntax check `parseXmlDocument` makes, without
* building a document. An internal subset's syntax needs the DOM parser, so
* the policy must refuse one.
*/
export const isWellFormedXml = (
source: string,
policy: {
readonly doctype: Exclude<DoctypePolicy, "allow">;
readonly nameSupported?: (name: string) => boolean;
},
): boolean => {
try {
checkXmlSyntax(source, policy.doctype, undefined, policy.nameSupported);
return true;
} catch {
return false;
}
};
/** An element as `xmlElementRanges` offers it: its namespace and local name, and an attribute's value by namespace and local name. */
export interface XmlElementFacts {
readonly uri: string;
readonly local: string;
readonly attribute: (uri: string, local: string) => string | undefined;
}
/**
* The source ranges of the elements `select` picks, from the `<` of the start
* tag to the `>` that ends the element, outermost first and none inside
* another picked one; read with the syntax check `parseXmlDocument` makes, so
* text in a comment, CDATA or an attribute is never an element.
*/
export const xmlElementRanges = (
source: string,
select: (element: XmlElementFacts) => boolean,
policy: { readonly doctype: Exclude<DoctypePolicy, "allow"> },
): Result<
readonly { readonly start: number; readonly end: number }[],
XmlParseFailure
> => {
const ranges: { start: number; end: number }[] = [];
// Each open element's start, and whether it was picked.
const open: { start: number; picked: boolean }[] = [];
let selectedAncestor = false;
try {
checkXmlSyntax(source, policy.doctype, (syntax) => {
// Saxes reports the opening after reading the name and one character past it, none of which is `<`.
syntax.on("opentagstart", () => {
open.push({
start: source.lastIndexOf("<", syntax.position - 1),
picked: false,
});
});
syntax.on("opentag", (tag: SaxesTagNS) => {
const element = open.at(-1);
if (element === undefined || selectedAncestor) return;
element.picked = select({
uri: tag.uri,
local: tag.local,
attribute: (uri, local) =>
Object.values(tag.attributes).find(
(attribute) => attribute.uri === uri && attribute.local === local,
)?.value,
});
selectedAncestor = element.picked;
});
syntax.on("closetag", () => {
const element = open.pop();
if (element?.picked === true) {
ranges.push({ start: element.start, end: syntax.position });
selectedAncestor = false;
}
});
});
return ok(ranges.sort((left, right) => left.start - right.start));
} catch (cause: unknown) {
return err(xmlFailure(cause));
}
};
/** Inclusive source ranges of attribute quotes, parsed without reading a DTD. */
export const xmlAttributeQuoteRanges = (
source: string,
): Result<
readonly { readonly start: number; readonly end: number }[],
{
readonly error: XmlParseFailure;
readonly ranges: readonly {
readonly start: number;
readonly end: number;
}[];
readonly examined: number;
}
> => {
const ranges: { start: number; end: number }[] = [];
let reader: SaxesParser<{ xmlns: true }> | undefined;
try {
checkXmlSyntax(source, "allow", (syntax) => {
reader = syntax;
syntax.on("attribute", () => {
const end = syntax.position - 1;
const quote = source[end];
if (quote !== '"' && quote !== "'") throw notWellFormed();
const start = source.lastIndexOf(quote, end - 1);
if (start < 0) throw notWellFormed();
ranges.push({ start, end });
});
});
return ok(ranges);
} catch (cause: unknown) {
return err({
error: xmlFailure(cause),
ranges,
examined: Math.max(0, (reader?.position ?? 0) - 1),
});
}
};
class StopXmlNodeCount extends Error {}
type XmlLineEndings = "preserve" | "normalize";
interface XmlNodeLimitFailure {
readonly kind: "node-limit";
readonly maximum: number;
readonly required: number;
}
/**
* Validate an XML document that disallows a DOCTYPE and count its projected
* nodes in the same stream. Honor the node ceiling before allocating a DOM.
*/
export const parseXmlDocumentWithinNodeLimit = (
source: string,
policy: {
readonly maximumNodes: number;
readonly lineEndings: XmlLineEndings;
},
): Result<Document, XmlParseFailure | XmlNodeLimitFailure> => {
let nodes = 0;
let doctypeSeen = false;
let failure: XmlParseFailure | undefined;
const count = () => {
nodes += 1;
if (nodes > policy.maximumNodes) throw new StopXmlNodeCount();
};
try {
checkXmlSyntax(source, "allow", (syntax) => {
// A counting failure unrelated to XML syntax must keep its own cause.
syntax.on("error", (cause) => {
throw notWellFormed(cause);
});
syntax.on("doctype", () => {
// The node ceiling can be reached even after a DOCTYPE was seen.
doctypeSeen = true;
});
syntax.on("opentagstart", count);
syntax.on("attribute", count);
syntax.on("text", count);
syntax.on("cdata", count);
syntax.on("comment", count);
syntax.on("processinginstruction", count);
});
} catch (cause: unknown) {
if (cause instanceof StopXmlNodeCount)
return err({
kind: "node-limit",
maximum: policy.maximumNodes,
required: nodes,
});
if (!isXmlFailure(cause)) throw cause;
failure = cause;
}
if (doctypeSeen) return err(unsupportedDoctype("doctype"));
return failure === undefined
? buildXmlDocument(source, policy.lineEndings)
: err(failure);
};
/** One boundary owns strict XML syntax, under a DOCTYPE policy, and the source-located DOM projection. */
export const parseXmlDocument = (
source: string,
policy: {
readonly doctype: DoctypePolicy;
readonly lineEndings: XmlLineEndings;
readonly nameSupported?: (name: string) => boolean;
},
): Result<Document, XmlParseFailure> => {
try {
checkXmlSyntax(source, policy.doctype, undefined, policy.nameSupported);
} catch (cause: unknown) {
return err(xmlFailure(cause));
}
return buildXmlDocument(source, policy.lineEndings);
};
const buildXmlDocument = (
source: string,
lineEndings: XmlLineEndings,
): Result<Document, XmlParseFailure> => {
try {
// Saxes owns well-formedness outside DTDs. The DOM parser additionally
// checks DTD syntax and builds source-located nodes; its advisory warnings
// (including literal U+FFFD) are not byte-decoding or syntax failures.
return ok(
new DOMParser({
normalizeLineEndings:
lineEndings === "preserve"
? (text) => text
: (text) => text.replace(XML_LINE_ENDING, "\n"),
onError: onErrorStopParsing,
}).parseFromString(source, "application/xml"),
);
} catch (cause: unknown) {
return err(xmlFailure(cause));
}
};
/** A DOCTYPE's name and external identifiers, null where it gives none. */
export interface DeclaredDocumentType {
readonly name: string;
readonly publicId: string | null;
readonly systemId: string | null;
}
/** A quoted literal's value; the DOM keeps the quotes, and holds "" where there is no literal. */
const literalValue = (literal: string): string | null =>
literal === "" ? null : literal.slice(1, -1);
/**
* The document's DOCTYPE as declared, or undefined without one. The public
* identifier is normalized as XML matches it, and as expat reports it: each
* run of spaces and line breaks becomes one space, and the ends are trimmed.
*/
export const declaredDocumentType = (
document: Document,
): DeclaredDocumentType | undefined => {
const doctype = document.doctype;
if (doctype === null) return undefined;
const publicId = literalValue(doctype.publicId);
return {
name: doctype.name,
publicId:
publicId === null
? null
: publicId.replace(/[ \r\n]+/gu, " ").replace(/^ | $/gu, ""),
systemId: literalValue(doctype.systemId),
};
};