Files
reverse-engineering-anything/src/domain/propertyListXmlText.ts
T
morluto cabd56ea71 refactor(domain): give strict UTF-8 decoding one owner with an explicit BOM policy (#10728)
* refactor(domain): give strict UTF-8 decoding one owner with an explicit BOM policy

* fix(domain): keep the leading-BOM behaviour every migrated UTF-8 decoder had

* refactor(domain): read the domain layer's strict UTF-8 through the BOM-policy owner (#10803)

Twelve domain decoders built their own fatal TextDecoder with ignoreBOM: true. They now use strictUtf8Decoder("preserve"), which keeps that behaviour: each reads bytes where a leading U+FEFF is content (binary-format names and fields, JSON and XML read byte-for-byte, recorded bodies, registry seeds whose kept bytes are re-emitted). The counted baseline drops from 74 to 62.

* refactor(artifacts): read artifact readers' strict UTF-8 through the BOM-policy owner (#10871)

Fourteen artifact reader modules (17 sites) built their own fatal TextDecoder. They now take it from strictUtf8Decoder with the policy each had: every one kept a leading BOM (ignoreBOM: true), so they use "preserve"; JavaScriptArtifactFiles keeps it for JavaScript and HTML entries and strips it otherwise, as before. The strict-utf8-decoder baseline drops those 17 sites.

* refactor(domain): drop TextDecoder imports left by the strict UTF-8 migration

* refactor(application): read MCP registration metadata through the strict UTF-8 owner

* refactor(formats): read binary-format readers' strict UTF-8 through the BOM-policy owner (#10903)

* refactor(formats): read binary-format readers' strict UTF-8 through the BOM-policy owner

* refactor(formats): drop the model readers' unused TextDecoder imports

* refactor(browser): read browser capture and fetch strict UTF-8 through the BOM-policy owner (#10935)

* refactor(browser): read browser capture and fetch strict UTF-8 through the BOM-policy owner

* refactor(native): read LLDB and Mach-O name strict UTF-8 through the BOM-policy owner (#10936)

* refactor(native): read LLDB and Mach-O name strict UTF-8 through the BOM-policy owner

* refactor(process): read MCP and NDJSON transport frames' strict UTF-8 through the BOM-policy owner (#10937)

* refactor(process): read MCP and NDJSON transport frames' strict UTF-8 through the BOM-policy owner

* refactor(application): read JSON and file inputs' strict UTF-8 through the BOM-policy owner (#10938)

* refactor(application): read JSON and file inputs' strict UTF-8 through the BOM-policy owner

* refactor(providers): read IDA, Electron, Ghidra and reference strict UTF-8 through the BOM-policy owner (#10939)

* refactor(providers): read IDA, Electron, Ghidra and reference strict UTF-8 through the BOM-policy owner

* refactor(evm): read the hex bytecode carrier's strict UTF-8 through the owner and refuse new local decoders (#10940)

* refactor(evm): read the hex bytecode carrier's strict UTF-8 through the BOM-policy owner

* chore(architecture): match local strict UTF-8 decoders in any option order
2026-11-15 19:54:39 +08:00

122 lines
3.9 KiB
TypeScript

import { ParseFailureError } from "./parseFailure.js";
import { byteOrderMarkEncoding } from "./textByteOrderMark.js";
import { decodeStrictUtf8 } from "./strictUtf8.js";
const XML_PLIST_FORMAT = "xml-plist";
/**
* XML plist text that cannot be decoded. An unsupported encoding holds text
* this decoder cannot read; invalid bytes and a contradicted declaration are
* malformed.
*/
export class XmlPlistTextDecodeError extends ParseFailureError<
"malformed" | "unsupported"
> {
constructor(
readonly reason:
| "invalid-bytes"
| "unsupported-encoding"
| "encoding-mismatch",
message: string,
readonly declaredEncoding?: string,
options?: ErrorOptions,
) {
super(
reason === "unsupported-encoding"
? {
kind: "unsupported",
format: XML_PLIST_FORMAT,
detail: message,
undecodedContent: true,
}
: { kind: "malformed", format: XML_PLIST_FORMAT, detail: message },
options,
);
this.name = "XmlPlistTextDecodeError";
}
}
/** Decode BOM-aware XML plist text and verify any declaration against its bytes. */
export const decodeXmlPlistText = (bytes: Uint8Array): string => {
const marked = byteOrderMarkEncoding(bytes);
if (marked === "utf-32")
throw new XmlPlistTextDecodeError(
"unsupported-encoding",
"Unsupported XML encoding: UTF-32. Supported encodings are UTF-8 and BOM-marked UTF-16 in either byte order.",
"UTF-32",
);
const encoding = marked ?? "utf-8";
// An ASCII declaration can identify an unsupported legacy encoding before
// its body is incorrectly subjected to UTF-8 validation.
const prolog = encoding === "utf-8" ? asciiXmlDeclaration(bytes) : undefined;
const declaration =
prolog === undefined ? undefined : declaredEncoding(prolog);
if (prolog !== undefined) verifyEncodingDeclaration(declaration, encoding);
let text: string;
try {
text = new TextDecoder(encoding, { fatal: true }).decode(bytes);
} catch (cause: unknown) {
throw new XmlPlistTextDecodeError(
"invalid-bytes",
"XML text bytes are invalid for the detected encoding",
undefined,
{ cause },
);
}
if (prolog === undefined)
verifyEncodingDeclaration(declaredEncoding(text), encoding);
return text;
};
const declaredEncoding = (text: string): string | undefined =>
/^<\?xml\s[^?]*\bencoding\s*=\s*["']([^"']+)["']/u.exec(text)?.[1];
/** Only a complete ASCII XML declaration is interpretable before body decoding. */
const asciiXmlDeclaration = (bytes: Uint8Array): string | undefined => {
const start =
bytes[0] === 0xef && bytes[1] === 0xbb && bytes[2] === 0xbf ? 3 : 0;
if (
bytes[start] !== 0x3c ||
bytes[start + 1] !== 0x3f ||
bytes[start + 2] !== 0x78 ||
bytes[start + 3] !== 0x6d ||
bytes[start + 4] !== 0x6c
)
return undefined;
for (let index = start + 5; index < bytes.length; index += 1) {
if (bytes[index]! > 0x7f) return undefined;
if (bytes[index] === 0x3f && bytes[index + 1] === 0x3e)
return decodeStrictUtf8(
bytes.subarray(start, index + 2),
"strip-signature",
);
}
return undefined;
};
const verifyEncodingDeclaration = (
declaration: string | undefined,
encoding: string,
): void => {
const declared = declaration?.toLowerCase();
if (
declared !== undefined &&
!["utf-8", "utf-16", "utf-16le", "utf-16be"].includes(declared)
)
throw new XmlPlistTextDecodeError(
"unsupported-encoding",
`Unsupported XML encoding declaration: ${declaration}. Supported encodings are UTF-8 and BOM-marked UTF-16 in either byte order.`,
declaration,
);
if (
declared !== undefined &&
declared !== encoding &&
!(encoding !== "utf-8" && declared === "utf-16")
)
throw new XmlPlistTextDecodeError(
"encoding-mismatch",
"XML encoding declaration disagrees with its bytes",
declaration,
);
};