Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(ghidra): preserve valid Unicode language metadata
morluto3cfa00a29c1e
diff --git a/docs/installation.md b/docs/installation.mdindex 7dca3450b..0bfd00063 100644--- a/docs/installation.md+++ b/docs/installation.md@@ -1024,6 +1024,8 @@ export REA_GHIDRA_COMPILER_SPEC_ID="borlandcpp" # optional; requires a languagREA passes `-processor`/`-cspec`, checks both against the installation's `.ldefs`, and records them in the analysis profile. DOS imports reject an override.+Language definitions must be well-formed XML encoded as UTF-8 or BOM-marked UTF-16, with any encoding declaration matching the bytes. Literal Unicode replacement characters (U+FFFD) are accepted; malformed source bytes are rejected.+To seed code that auto-analysis cannot reach (computed jumps, pointers in data):```bashdiff --git a/src/ghidra/GhidraLanguageCatalog.ts b/src/ghidra/GhidraLanguageCatalog.tsindex 1cde8769f..1cd939af2 100644--- a/src/ghidra/GhidraLanguageCatalog.ts+++ b/src/ghidra/GhidraLanguageCatalog.ts@@ -1,7 +1,8 @@import { open, readdir } from "node:fs/promises";import type { FileHandle } from "node:fs/promises";import { join } from "node:path";-import { DOMParser, onWarningStopParsing } from "@xmldom/xmldom";+import { parseXmlDocument } from "../domain/xmlDocument.js";+import { decodeXmlPlistText } from "../domain/propertyListXmlText.js";import type { GhidraLanguageOverride } from "../config/ghidraLanguageOverride.js";import { ConfigurationError } from "../domain/configurationErrors.js";@@ -80,9 +81,12 @@ export const parseGhidraLanguageDefinitions = (text: string,): readonly GhidraLanguageDefinition[] => {const definitions: GhidraLanguageDefinition[] = [];- const document = new DOMParser({- onError: onWarningStopParsing,- }).parseFromString(text, "application/xml");+ const parsed = parseXmlDocument(text, {+ doctype: "allow",+ lineEndings: "normalize",+ });+ if (!parsed.ok) throw parsed.error;+ const document = parsed.value;const root = document.documentElement;if (root?.tagName !== "language_definitions")throw new SyntaxError("Expected Ghidra language_definitions XML root");@@ -164,10 +168,10 @@ export const readGhidraLanguageCatalog = async (if (!entry.endsWith(".ldefs")) continue;const bytes = await readLanguageDefinitionFile(join(directory, entry));if (bytes === undefined) continue;- // XML 1.0 section 4.3.3: a leading UTF-8 BOM is an encoding signature,- // not document content; TextDecoder consumes it, Buffer#toString does not.+ // The XML byte owner validates encoding and declaration before syntax+ // parsing; a literal U+FFFD cannot diagnose invalid source bytes.definitions.push(- ...parseGhidraLanguageDefinitions(new TextDecoder().decode(bytes)),+ ...parseGhidraLanguageDefinitions(decodeXmlPlistText(bytes)),);}}diff --git a/tests/boundary/filesystem/ghidraLanguageCatalogEncoding.test.ts b/tests/boundary/filesystem/ghidraLanguageCatalogEncoding.test.tsnew file mode 100644index 000000000..647e5c226--- /dev/null+++ b/tests/boundary/filesystem/ghidraLanguageCatalogEncoding.test.ts@@ -0,0 +1,68 @@+import { mkdir, writeFile } from "node:fs/promises";+import { join } from "node:path";+import { expect, it } from "vitest";+import { readGhidraLanguageCatalog } from "../../../src/ghidra/GhidraLanguageCatalog.js";+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";++const xml =+ '<language_definitions><language id="toy:LE:32:default" description="�"><compiler id="default"/></language></language_definitions>';+it.each([+ { name: "literal U+FFFD", bytes: Buffer.from(xml), expectedError: null },+ {+ name: "UTF-8 BOM",+ bytes: Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from(xml)]),+ expectedError: null,+ },+ {+ name: "UTF-16 BOM/declaration",+ bytes: Buffer.concat([+ Buffer.from([0xff, 0xfe]),+ Buffer.from('<?xml version="1.0" encoding="UTF-16"?>' + xml, "utf16le"),+ ]),+ expectedError: null,+ },+ {+ name: "malformed UTF-8",+ bytes: Buffer.concat([+ Buffer.from('<language_definitions description="'),+ Buffer.from([0xff]),+ Buffer.from(+ '"><language id="toy:LE:32:default"/></language_definitions>',+ ),+ ]),+ expectedError: "invalid-bytes",+ },+ {+ name: "encoding mismatch",+ bytes: Buffer.from('<?xml version="1.0" encoding="UTF-16"?>' + xml),+ expectedError: "encoding-mismatch",+ },+ {+ name: "malformed XML alongside U+FFFD",+ bytes: Buffer.from(xml.replace('id="toy:LE:32:default"', "id=unquoted")),+ expectedError: "malformed",+ },+])(+ "reads $name at the language-file byte boundary",+ async ({ bytes, expectedError }) => {+ const root = await createTestTempDirectory("rea-ghidra-xml-encoding-");+ const directory = join(+ root,+ "Ghidra",+ "Processors",+ "toy",+ "data",+ "languages",+ );+ await mkdir(directory, { recursive: true });+ await writeFile(join(directory, "toy.ldefs"), bytes);+ if (expectedError === null)+ expect(await readGhidraLanguageCatalog(root)).toEqual([+ { id: "toy:LE:32:default", compilerSpecIds: ["default"] },+ ]);+ else+ await expect(readGhidraLanguageCatalog(root)).rejects.toMatchObject({+ reason: expectedError,+ });+ },+);