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
This commit is contained in:
morluto authored and GitHub committed 2026-11-15 19:54:39 +08:00
1 parent abd36dd250
commit cabd56ea71
103 files changed
+322 -251

No files matched your search

+2 -4
View File
@@ -1,3 +1,4 @@
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
import { WabtAdmissionError } from "./WabtAdmissionError.js";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import { randomUUID, createHash } from "node:crypto";
@@ -494,10 +495,7 @@ export class WabtArtifactProvider implements WasmArtifactPort {
};
try {
// Keep source characters intact so locations match the hashed artifact.
const source = new TextDecoder("utf-8", {
fatal: true,
ignoreBOM: true,
}).decode(bytes.bytes);
const source = decodeStrictUtf8(bytes.bytes, "preserve");
glue[glueIndex] = {
artifact: glueArtifact,
...associateWasmGlue(
+2 -3
View File
@@ -1,3 +1,4 @@
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
import { WabtAdmissionError } from "./WabtAdmissionError.js";
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
import { wasmInterfaceEntryRepresentationBytes } from "../domain/wasm/wasmInterface.js";
@@ -57,9 +58,7 @@ const splitParts = (form: string, maximumBytes?: number): string[] => {
const name = (text: string | undefined): string => {
if (text?.startsWith('"') !== true)
throw new WabtAdmissionError("name_missing");
return new TextDecoder("utf8", { fatal: true, ignoreBOM: true }).decode(
watString(text, 1).bytes,
);
return decodeStrictUtf8(watString(text, 1).bytes, "preserve");
};
const integer = (text: string): string | null => {
if (!/^(?:\d+|0x[\da-f]+)$/iu.test(text)) return null;
+2 -8
View File
@@ -1,5 +1,6 @@
import { WabtAdmissionError } from "./WabtAdmissionError.js";
import { readLeb128, ULEB32 } from "../domain/leb128.js";
import { isStrictUtf8 } from "../domain/strictUtf8.js";
/** WABT prints names with %.*s, so an embedded NUL ends their display. */
const wabtDisplayedName = (name: string | undefined): string | undefined =>
@@ -104,12 +105,5 @@ const invalidCustomNameEnd = (
if ("failure" in read) return undefined;
const nameEnd = read.next + Number(read.value);
if (nameEnd > end) return undefined;
try {
new TextDecoder("utf-8", { fatal: true }).decode(
bytes.subarray(read.next, nameEnd),
);
return undefined;
} catch {
return nameEnd;
}
return isStrictUtf8(bytes.subarray(read.next, nameEnd)) ? undefined : nameEnd;
};
+3 -7
View File
@@ -1,3 +1,4 @@
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
import { WabtAdmissionError } from "./WabtAdmissionError.js";
import { parseWabtInterface } from "./WabtInterface.js";
import { watString, watStringEnd } from "./WabtString.js";
@@ -45,9 +46,7 @@ const moduleNames = (
if (nameEnd > end)
throw new WabtAdmissionError("incomplete_module_name");
names.push(
new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
bytes.subarray(offset, nameEnd),
),
decodeStrictUtf8(bytes.subarray(offset, nameEnd), "preserve"),
);
// WABT calls OnModuleName before rejecting trailing subsection bytes.
if (nameEnd !== end)
@@ -121,10 +120,7 @@ export const parseWabtWat = (text: string, maximumInterfaceBytes?: number) => {
throw new WabtAdmissionError("unsupported_custom_name");
const nameString = watString(form, prefix[0].length);
// A WASM name is length-delimited UTF-8; leading U+FEFF is name data.
const name = new TextDecoder("utf-8", {
fatal: true,
ignoreBOM: true,
}).decode(nameString.bytes);
const name = decodeStrictUtf8(nameString.bytes, "preserve");
customSectionNames.push(name);
if (name === "name") {
const payloadPrefix = /^\s+"/u.exec(form.slice(nameString.end));