Files
reverse-engineering-anything/src/domain/xmlDocument.test.ts
T
morluto 78fd365bdd refactor(macho): name load-command codes through the shared LC table (#10714)
* refactor(macho): name load-command codes through the shared LC table

* refactor(macho): name version-min and string commands through the shared tables

* refactor(parsers): report XML, web artifact and Mach-O failures through ParseFailure (#10760)

* refactor(parsers): report XML, web artifact and Mach-O failures through ParseFailure

* refactor(xml): type XML parse failures as malformed or unsupported only

* refactor(parsers): catch per-format document and source failure classes (#10794)

* refactor(parsers): report apktool strings and XML plist source failures through ParseFailure

* refactor(apktool): drop the unused ParseFailureError import

* refactor(parsers): retire the Mach-O, source-map, NDJSON and managed-graph failure classes (#10818)

* refactor(parsers): retire the Mach-O, source-map, NDJSON and managed-graph failure classes

* refactor(plist): report property-list and keyed-archive failures through ParseFailure (#10822)

* refactor(plist): report property-list and keyed-archive failures through ParseFailure

* refactor(kuna): report Kuna document failures through ParseFailure (#10824)

* refactor(kuna): report Kuna document failures through ParseFailure

* test(kuna): pin the limit and malformed facts of Kuna document failures

* refactor(plist): report plist decode budgets through ParseFailure (#10842)

* refactor(plist): report plist decode budgets through ParseFailure

The plist preflight owner throws a ParseFailure limit (plist-decode-bytes) for its byte budget, the shared PlistNestingLimitError for nesting depth, and a malformed failure for a reference cycle, instead of a per-format PropertyListDecodeBudgetFailure whose meaning each caller re-mapped through a fail callback. The fail option, PropertyListDecodeBudgetFailure and interfaceBuilderDecodeBudgetFailure are removed; the parser-format-failure-class baseline reaches zero.

* refactor(plist): report keyed-archive allowances as typed limit failures (#10844)

keyedArchive.ts and decodeKeyedArchiveBytes throw decodeLimit parse failures (keyed-archive-bytes, -object-table-entries, -values-per-object, -reference-edges) and malformed parse failures instead of plain RangeError/TypeError. KeyedArchiveReader no longer classifies every RangeError as a limit: only a limit parse failure or a host stack/capacity limit is one. The unreachable 128-level scan nesting check is removed; projectPlistValue already refuses the archive first.

* test(plist): expect the nesting limit as its ParseFailure carrier

* refactor(plist): report plist and NIBArchive input faults through ParseFailure and defects through invariantViolation (#10895)

* refactor(plist): report plist and NIBArchive input faults through ParseFailure and defects through invariantViolation

* fix(plist): report a cut-short NIB header or plist length record as truncated

* fix(native): project the selected Mach-O's parse failures through the shared owner

nativeMachoSourcePrimary mapped every non-limit ParseFailureError to
AnalysisOutputError, so a malformed or truncated selected Mach-O was
reported as unreadable tool output and an unsupported one lost its
category. It now uses parseFailureAnalysisError like the ELF, Go and
other byte-format providers: malformed and truncated bytes are an
invalid path, unsupported bytes an unsupported target, and a limit
still falls through to its producer's budget.
2026-11-15 18:24:35 +08:00

253 lines
8.3 KiB
TypeScript

import { expect, it } from "vitest";
import {
declaredDocumentType,
isWellFormedXml,
parseXmlDocument,
xmlElementRanges,
} from "./xmlDocument.js";
it.each([
'<root attr="�">😀 � &amp; &#xFFFD;</root>',
"<root><!-- � --><![CDATA[�]]></root>",
'<?xml version="1.0"?><root>\r\n �\rtext</root>',
"<!DOCTYPE root><root>�</root>",
'<!DOCTYPE root [<!ENTITY unused "�">]><root>�</root>',
])("preserves legal Unicode and source-located DOM nodes: %j", (source) => {
const parsed = parseXmlDocument(source, {
doctype: "allow",
lineEndings: "preserve",
});
if (!parsed.ok) throw parsed.error;
expect(parsed.value.documentElement?.tagName).toBe("root");
expect(parsed.value.documentElement?.lineNumber).toBeDefined();
expect(parsed.value.documentElement?.textContent).toContain("�");
});
it.each([
"<root attr=unquoted>�</root>",
'<root a="x"b="�"/>',
'<root attr="a" attr="�"/>',
"<root><broken>�</root>",
"<root>� & unknown</root>",
"<root>� &missing;</root>",
"<root>�\u0000</root>",
"<root>�&#xD800;</root>",
"<root>�\uFFFF</root>",
"<root>�]]></root>",
"<root><!-- invalid -- comment � --></root>",
"<root>�</root><second/>",
"<!DOCTYPE root [BOGUS]><root>�</root>",
"<!DOCTYPE root [<!ELEMENT broken>]><root>�</root>",
"<!DOCTYPE root SYSTEM><root>�</root>",
])("rejects malformed XML without warning-message matching: %j", (source) => {
const parsed = parseXmlDocument(source, {
doctype: "allow",
lineEndings: "preserve",
});
if (parsed.ok) throw new Error("Expected refusal");
expect(parsed.error.kind).toBe("malformed");
expect(parsed.error.cause).toBeDefined();
});
it("keeps literal line endings and character-reference values distinct", () => {
const parsed = parseXmlDocument("<root>\r\n&#13;\r�</root>", {
doctype: "allow",
lineEndings: "preserve",
});
if (!parsed.ok) throw parsed.error;
expect(parsed.value.documentElement?.textContent).toBe("\r\n\r\r�");
});
it.each([
'<root xmlns:p="urn:a" xmlns:q="urn:a" p:x="first" q:x="last"/>',
'<root xmlns:p="urn:a" xmlns:q="urn:a" q:x="last" p:x="first"/>',
'<root p:x="unbound"/>',
'<root xmlns:xml="urn:other"/>',
'<root xmlns:p="http://www.w3.org/2000/xmlns/"/>',
])("rejects namespace-invalid XML before DOM projection: %j", (source) => {
expect(
parseXmlDocument(source, { doctype: "reject", lineEndings: "preserve" }),
).toMatchObject({ ok: false, error: { kind: "malformed" } });
});
it.each([
'<root xmlns:p="urn:a" xmlns:q="urn:b" p:x="first" q:x="last"/>',
'<root xmlns="urn:a" xmlns:p="urn:a" x="first" p:x="last"/>',
'<root xmlns:p="urn:a" p:x="first" p:y="last"/>',
])("preserves distinct expanded attribute names: %j", (source) => {
const parsed = parseXmlDocument(source, {
doctype: "reject",
lineEndings: "preserve",
});
if (!parsed.ok) throw parsed.error;
const attributes = parsed.value.documentElement!.attributes;
const values = Array.from(
{ length: attributes.length },
(_, index) => attributes.item(index)?.value,
);
expect(values).toContain("first");
expect(values).toContain("last");
});
it("applies the consumer's explicit DTD policy", () => {
const parsed = parseXmlDocument(
'<!DOCTYPE root [<!ENTITY value "unused">]><root/>',
{ doctype: "reject", lineEndings: "preserve" },
);
if (parsed.ok) throw new Error("Expected unsupported DTD");
expect(parsed.error.failure).toMatchObject({
kind: "unsupported",
field: "doctype",
});
});
it.each([
"<!DOCTYPE root><root/>",
'<!DOCTYPE root SYSTEM "root[1].dtd"><root/>',
"<!DOCTYPE root PUBLIC '-//A//DTD B//EN' 'http://example.test/b.dtd'><root/>",
])(
"reads a DOCTYPE without an internal subset when only that is allowed: %j",
(source) => {
const parsed = parseXmlDocument(source, {
doctype: "external",
lineEndings: "normalize",
});
if (!parsed.ok) throw parsed.error;
expect(parsed.value.documentElement?.tagName).toBe("root");
},
);
it.each([
"<!DOCTYPE root []><root/>",
'<!DOCTYPE root SYSTEM "a.dtd" [<!ENTITY e "x">]><root/>',
'<!DOCTYPE root PUBLIC "-//A//EN" "a]b.dtd" [ ]><root/>',
])(
"refuses an internal subset when only an external DTD is allowed: %j",
(source) => {
expect(
parseXmlDocument(source, {
doctype: "external",
lineEndings: "normalize",
}),
).toMatchObject({
ok: false,
error: { failure: { kind: "unsupported", field: "internal-subset" } },
});
},
);
it.each([
['<!DOCTYPE root PUBLIC "-//A//DTD B//EN" "b.dtd"><root/>', true],
['<!DOCTYPE root SYSTEM "root[1].dtd"><root/>', true],
["<!DOCTYPE root\n><root/>", true],
["<!DOCTYPE root []><root/>", false],
['<!DOCTYPE root SYSTEM "a.dtd"><root>&e;</root>', false],
['<!DOCTYPE root PUBLIC "{bad}" "b.dtd"><root/>', false],
['<!DOCTYPE root PUBLIC "-//A//EN"><root/>', false],
["<!DOCTYPE root SYSTEM><root/>", false],
['<!DOCTYPE root SYSTEM "a.dtd" junk><root/>', false],
["<!DOCTYPEroot><root/>", false],
])(
"tells XML by the syntax pass alone as the DOM parse does under the external policy: %j",
(source, xml) => {
expect(isWellFormedXml(source, { doctype: "external" })).toBe(xml);
expect(
parseXmlDocument(source, {
doctype: "external",
lineEndings: "normalize",
}).ok,
).toBe(xml);
},
);
it("reports a document type's name and identifiers as declared, the public one normalized", () => {
const read = (source: string) => {
const parsed = parseXmlDocument(source, {
doctype: "external",
lineEndings: "normalize",
});
if (!parsed.ok) throw parsed.error;
return declaredDocumentType(parsed.value);
};
expect(
read(
'<!DOCTYPE abiword PUBLIC " -//ABISOURCE//DTD\r\n AWML 1.0 Strict//EN " "http://www.abisource.com/awml.dtd"><abiword/>',
),
).toEqual({
name: "abiword",
publicId: "-//ABISOURCE//DTD AWML 1.0 Strict//EN",
systemId: "http://www.abisource.com/awml.dtd",
});
expect(read("<!DOCTYPE a SYSTEM 'x y'><a/>")).toEqual({
name: "a",
publicId: null,
systemId: "x y",
});
expect(read('<!DOCTYPE a PUBLIC "" ""><a/>')).toEqual({
name: "a",
publicId: "",
systemId: "",
});
expect(read("<!DOCTYPE a><a/>")).toEqual({
name: "a",
publicId: null,
systemId: null,
});
expect(read("<a/>")).toBeUndefined();
});
it("applies XML line-ending normalization only when the consumer selects it", () => {
const parsed = parseXmlDocument('<root attr="a\r\nb">\r\nx</root>', {
doctype: "allow",
lineEndings: "normalize",
});
if (!parsed.ok) throw parsed.error;
expect(parsed.value.documentElement?.getAttribute("attr")).toBe("a b");
expect(parsed.value.documentElement?.textContent).toBe("\nx");
});
it("preserves Unicode separators under XML 1.0 normalization", () => {
const parsed = parseXmlDocument(
'<root attr="a\u0085b\u2028c\u2029d">a\r\u0085b\r\u2028c\r\n&#13;d\u2029</root>',
{ doctype: "allow", lineEndings: "normalize" },
);
if (!parsed.ok) throw parsed.error;
expect(parsed.value.documentElement?.getAttribute("attr")).toBe(
"a\u0085b\u2028c\u2029d",
);
expect(parsed.value.documentElement?.textContent).toBe(
"a\n\u0085b\n\u2028c\n\rd\u2029",
);
});
it("gives the source range of each picked element, the outermost, through line breaks and astral characters", () => {
const source =
'<r xmlns:n="urn:n">\r\n<!-- <x n:k="1"/> --><x n:k="1">😀<x n:k="1"/></x>\r\n<x n:k = \'1\' /><x k="1"/></r>';
const ranges = xmlElementRanges(
source,
(element) =>
element.local === "x" && element.attribute("urn:n", "k") === "1",
{ doctype: "reject" },
);
if (!ranges.ok) throw ranges.error;
expect(
ranges.value.map(({ start, end }) => source.slice(start, end)),
).toEqual(['<x n:k="1">😀<x n:k="1"/></x>', "<x n:k = '1' />"]);
expect(
xmlElementRanges("<r><x></r>", () => true, { doctype: "reject" }).ok,
).toBe(false);
});
it("selects sibling outermost matches after a deeply nested nonmatching branch", () => {
const source = `<root>${"<n>".repeat(4000)}${"</n>".repeat(4000)}<x><x/></x><x/></root>`;
const ranges = xmlElementRanges(source, ({ local }) => local === "x", {
doctype: "reject",
});
if (!ranges.ok) throw ranges.error;
expect(
ranges.value.map(({ start, end }) => source.slice(start, end)),
).toEqual(["<x><x/></x>", "<x/>"]);
});