From 1d85f680f9aeee634ff3da8cd20c9209c6275b9e Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 06:46:19 -0500 Subject: [PATCH] fix(ghidra): name invalid installed language definition files (#13401) --- src/ghidra/GhidraAdmissionError.ts | 1 + src/ghidra/GhidraLanguageCatalog.ts | 23 +++++-- src/ghidra/GhidraLanguageOverride.test.ts | 68 ++++++++----------- .../ghidraLanguageCatalogEncoding.test.ts | 31 +++++++-- 4 files changed, 73 insertions(+), 50 deletions(-) diff --git a/src/ghidra/GhidraAdmissionError.ts b/src/ghidra/GhidraAdmissionError.ts index 490a594125..45e809a962 100644 --- a/src/ghidra/GhidraAdmissionError.ts +++ b/src/ghidra/GhidraAdmissionError.ts @@ -18,6 +18,7 @@ const GHIDRA_ADMISSION_CODES = [ "elf_image_base_format", "language_override_dos", "language_override_raw_binary", + "language_definition_invalid", "raw_binary_dos", "invalid_environment_entry", "invalid_jvm_argument", diff --git a/src/ghidra/GhidraLanguageCatalog.ts b/src/ghidra/GhidraLanguageCatalog.ts index d555439b82..a40b187413 100644 --- a/src/ghidra/GhidraLanguageCatalog.ts +++ b/src/ghidra/GhidraLanguageCatalog.ts @@ -1,5 +1,7 @@ import { readdir } from "node:fs/promises"; import { join } from "node:path"; +import { malformedInput, ParseFailureError } from "../domain/parseFailure.js"; +import { GhidraAdmissionError } from "./GhidraAdmissionError.js"; import { parseXmlDocument } from "../domain/xmlDocument.js"; import { decodeXmlPlistText } from "../domain/propertyListXmlText.js"; @@ -29,7 +31,7 @@ const LANGUAGE_ROOTS = ["Ghidra/Processors", "Ghidra/Extensions"] as const; const CATALOG_CONCURRENCY = 8; /** Parse the `` elements of one `.ldefs` document. */ -export const parseGhidraLanguageDefinitions = ( +const parseGhidraLanguageDefinitions = ( text: string, ): readonly GhidraLanguageDefinition[] => { const definitions: GhidraLanguageDefinition[] = []; @@ -41,7 +43,10 @@ export const parseGhidraLanguageDefinitions = ( const document = parsed.value; const root = document.documentElement; if (root?.tagName !== "language_definitions") - throw new SyntaxError("Expected Ghidra language_definitions XML root"); + throw malformedInput( + "ghidra-language-definition", + "Expected Ghidra language_definitions XML root", + ); for (const language of Array.from(root.getElementsByTagName("language"))) { if (language.parentNode !== root) continue; const id = language.getAttribute("id"); @@ -101,9 +106,17 @@ const readLanguageDefinitionFile = ( }); // The XML byte owner validates encoding and declaration before syntax // parsing; a literal U+FFFD cannot diagnose invalid source bytes. - return bytes === undefined - ? [] - : parseGhidraLanguageDefinitions(decodeXmlPlistText(bytes)); + if (bytes === undefined) return []; + try { + return parseGhidraLanguageDefinitions(decodeXmlPlistText(bytes)); + } catch (cause: unknown) { + if (!(cause instanceof ParseFailureError)) throw cause; + throw new GhidraAdmissionError( + "language_definition_invalid", + { path, parse_kind: cause.kind, parse_message: cause.message }, + { cause }, + ); + } }, "follow", ); diff --git a/src/ghidra/GhidraLanguageOverride.test.ts b/src/ghidra/GhidraLanguageOverride.test.ts index 496a267382..94dec60fcf 100644 --- a/src/ghidra/GhidraLanguageOverride.test.ts +++ b/src/ghidra/GhidraLanguageOverride.test.ts @@ -16,10 +16,7 @@ import { ghidraProfileInstallationHost as host, } from "./GhidraInstallation.fixture.js"; import { SUPPORTED_GHIDRA_VERSION } from "./GhidraReleasePolicy.js"; -import { - parseGhidraLanguageDefinitions, - readGhidraLanguageCatalog, -} from "./GhidraLanguageCatalog.js"; +import { readGhidraLanguageCatalog } from "./GhidraLanguageCatalog.js"; import { GhidraProvider } from "./GhidraProvider.js"; import { ArtifactResourceScope } from "../resources/ArtifactResourceScope.js"; @@ -214,39 +211,16 @@ describe("Ghidra language override configuration", () => { }); describe("Ghidra language catalog", () => { - it("reads quoted and escaped XML attributes without admitting comments or CDATA", () => { - const catalog = parseGhidraLanguageDefinitions(` - - - ]]> - - - - `); - expect(catalog).toEqual([ - { id: "x86:LE:32:default", compilerSpecIds: ["borlandcpp"] }, - ]); - }); - - it.each([ - '', - "", - '', - ])("rejects malformed language-definition documents", (text) => { - expect(() => parseGhidraLanguageDefinitions(text)).toThrow(); - }); - it("parses language IDs and their compiler specs", async () => { - expect(parseGhidraLanguageDefinitions(X86_LDEFS)).toEqual([ + expect( + await readGhidraLanguageCatalog(installDir, new ArtifactResourceScope()), + ).toEqual([ { id: "x86:LE:32:default", compilerSpecIds: ["windows", "gcc", "borlandcpp"], }, { id: "x86:LE:16:Real Mode", compilerSpecIds: ["default"] }, ]); - expect( - await readGhidraLanguageCatalog(installDir, new ArtifactResourceScope()), - ).toHaveLength(2); expect( await readGhidraLanguageCatalog( join(installDir, "absent"), @@ -254,32 +228,48 @@ describe("Ghidra language catalog", () => { ), ).toEqual([]); }); -}); -describe("Ghidra language override profile", () => { - it("returns a provider failure for a malformed installed language definition", async () => { + it("identifies the malformed installed definition beside a valid selected language", async () => { const path = join( installDir, "Ghidra/Processors/x86/data/languages/broken.ldefs", ); + const configured = provider({ + REA_GHIDRA_LANGUAGE_ID: "x86:LE:32:default", + }); await writeFile( path, "", ); try { - const resolved = await provider({ - REA_GHIDRA_LANGUAGE_ID: "x86:LE:32:default", - }).resolveAnalysisProfile(pe); + const resolved = await configured.resolveAnalysisProfile(pe); if (resolved.ok) throw new Error("Expected a language catalog failure"); - expect(resolved.error).toMatchObject({ - providerId: "ghidra", - operation: "resolve_analysis_profile", + expect(projectAnalysisError(resolved.error)).toMatchObject({ + details: { + provider_id: "ghidra", + operation: "resolve_analysis_profile", + diagnostics: { + setting: "REA_GHIDRA_LANGUAGE_ID", + code: "language_definition_invalid", + path, + parse_kind: "malformed", + parse_message: expect.stringContaining("XML is not well-formed"), + }, + }, }); + await writeFile(path, ""); + const repaired = await configured.resolveAnalysisProfile(pe); + if (!repaired.ok) throw repaired.error; + expect(repaired.value.profile?.parameters.language_id).toBe( + "x86:LE:32:default", + ); } finally { await rm(path); } }); +}); +describe("Ghidra language override profile", () => { it("accepts the caller-selected language on the legacy MIPS lane", async () => { const mipsLanguages = join( installDir, diff --git a/tests/boundary/filesystem/ghidraLanguageCatalogEncoding.test.ts b/tests/boundary/filesystem/ghidraLanguageCatalogEncoding.test.ts index 377b746348..79b1cfe4c1 100644 --- a/tests/boundary/filesystem/ghidraLanguageCatalogEncoding.test.ts +++ b/tests/boundary/filesystem/ghidraLanguageCatalogEncoding.test.ts @@ -5,8 +5,14 @@ import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceSc import { readGhidraLanguageCatalog } from "../../../src/ghidra/GhidraLanguageCatalog.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -const xml = - ''; +const xml = ` + + + ]]> + + + +`; const languageDirectory = async () => { const root = await createTestTempDirectory("rea-ghidra-xml-encoding-"); const directory = join(root, "Ghidra/Processors/toy/data/languages"); @@ -19,7 +25,7 @@ it("reads independent unsigned, UTF-8 BOM and UTF-16 BOM definitions together", const named = (id: string) => xml.replace("toy:LE:32:default", `${id}:LE:32:default`); await writeFile( - join(directory, "ascii.ldefs"), + join(directory, "plain.ldefs"), Buffer.from(named("unsigned")), ); await writeFile( @@ -67,16 +73,29 @@ it.each([ }, { name: "malformed XML alongside U+FFFD", - bytes: Buffer.from(xml.replace('id="toy:LE:32:default"', "id=unquoted")), + bytes: Buffer.from(xml.replace("id='toy:LE:32:default'", "id=unquoted")), expectedError: { _tag: "ParseFailureError", kind: "malformed" }, }, + { + name: "a valid XML document with the wrong root", + bytes: Buffer.from(''), + expectedError: { + kind: "malformed", + message: "Expected Ghidra language_definitions XML root", + }, + }, ])( "preserves the failure for $name at the language-file byte boundary", async ({ bytes, expectedError }) => { const { root, directory } = await languageDirectory(); - await writeFile(join(directory, "toy.ldefs"), bytes); + const path = join(directory, "toy.ldefs"); + await writeFile(path, bytes); await expect( readGhidraLanguageCatalog(root, new ArtifactResourceScope()), - ).rejects.toMatchObject(expectedError); + ).rejects.toMatchObject({ + code: "language_definition_invalid", + detail: { path }, + cause: expectedError, + }); }, );