Skip to content

Commit a8b8288

Browse files
felipeatpN0zoM1z0
authored andcommitted
fix: keep the Ghidra session root free of dot-prefixed path elements
Ghidra validates every project path element with NamingUtilities.checkName and aborts the headless analyzer on any element starting with '.', so an inherited TMPDIR such as ~/.cache/<tool> failed every Ghidra operation with a generic analysis failure. Select the session root through a helper that prefers the inherited base, then a dot-free platform temp directory, then the nearest dot-free ancestor.
1 parent 5486860 commit a8b8288

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

‎src/ghidra/GhidraClient.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { randomBytes, randomUUID } from "node:crypto";
22
import type { Socket } from "node:net";
3-
import { tmpdir } from "node:os";
43

54
import { AnalysisError } from "../domain/analysisErrorBase.js";
65
import type { JsonValue } from "../domain/jsonValue.js";
@@ -47,9 +46,10 @@ import {
4746
} from "./GhidraTransport.js";
4847
import { GhidraWire } from "./GhidraClientWire.js";
4948
import { completeGhidraStartupHandshake } from "./GhidraClientStartup.js";
49+
import { ghidraSessionRoot } from "./GhidraSessionRoot.js";
5050

5151
const SHUTDOWN_TIMEOUT_MS = 10_000;
52-
const SESSION_ROOT = tmpdir();
52+
const SESSION_ROOT = ghidraSessionRoot();
5353

5454
export type {
5555
GhidraClientOptions,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { join, sep } from "node:path";
2+
import { describe, expect, it } from "vitest";
3+
4+
import { ghidraSessionRoot } from "./GhidraSessionRoot.js";
5+
6+
const hasDotPrefixElement = (directory: string): boolean =>
7+
directory.split(sep).some((element) => element.startsWith("."));
8+
9+
const POSIX = process.platform !== "win32";
10+
const posixFallback = join(sep, "tmp");
11+
const posixBase = (...segments: string[]) => join(sep, ...segments);
12+
13+
describe("ghidraSessionRoot", () => {
14+
it("keeps an inherited base that Ghidra accepts", () => {
15+
const base = posixBase("var", "tmp", "rea-run");
16+
expect(ghidraSessionRoot({ base, fallback: posixFallback })).toBe(base);
17+
});
18+
19+
it("keeps a base whose elements only contain an interior dot", () => {
20+
const base = posixBase("opt", "rea.run", "session");
21+
expect(ghidraSessionRoot({ base, fallback: posixFallback })).toBe(base);
22+
});
23+
24+
it("relocates a dot-prefixed base to the supplied dot-free fallback", () => {
25+
const base = posixBase("home", "operator", ".cache", "scratch");
26+
expect(ghidraSessionRoot({ base, fallback: posixFallback })).toBe(
27+
posixFallback,
28+
);
29+
});
30+
31+
it("relocates a base that is itself a hidden directory", () => {
32+
const base = posixBase("home", "operator", ".tmp");
33+
expect(ghidraSessionRoot({ base, fallback: posixBase("var", "tmp") })).toBe(
34+
posixBase("var", "tmp"),
35+
);
36+
});
37+
38+
it("ignores a fallback that is itself dotted", () => {
39+
const base = posixBase("home", "operator", ".cache", "scratch");
40+
expect(
41+
ghidraSessionRoot({
42+
base,
43+
fallback: posixBase("home", "operator", ".tmp"),
44+
}),
45+
).toBe(posixBase("home", "operator"));
46+
});
47+
48+
it("never returns a path with a dot-prefixed element", () => {
49+
const directory = ghidraSessionRoot({
50+
base: posixBase("home", "operator", ".cache", "scratch"),
51+
fallback: posixBase("home", "operator", ".cache"),
52+
});
53+
expect(hasDotPrefixElement(directory)).toBe(false);
54+
});
55+
56+
it.skipIf(!POSIX)("prefers the platform temp directory by default", () => {
57+
expect(
58+
ghidraSessionRoot({ base: posixBase("home", "operator", ".cache") }),
59+
).toBe(posixBase("tmp"));
60+
});
61+
});

‎src/ghidra/GhidraSessionRoot.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { dirname, isAbsolute, resolve, sep } from "node:path";
2+
import { tmpdir } from "node:os";
3+
4+
/** Session roots may not contain a dot-prefixed path element. */
5+
const hasDotPrefixElement = (directory: string): boolean =>
6+
resolve(directory)
7+
.split(sep)
8+
.some((element) => element.startsWith("."));
9+
10+
/** Nearest ancestor of `directory`, inclusive, that has no dot-prefixed element. */
11+
const nearestSafeAncestor = (directory: string): string | undefined => {
12+
let current = resolve(directory);
13+
for (;;) {
14+
if (!hasDotPrefixElement(current)) return current;
15+
const parent = dirname(current);
16+
if (parent === current) return undefined;
17+
current = parent;
18+
}
19+
};
20+
21+
/** Coordinates accepted by {@link ghidraSessionRoot}. */
22+
export interface GhidraSessionRootOptions {
23+
/** Inherited base directory, normally `os.tmpdir()`. */
24+
readonly base?: string;
25+
/** Platform-specific dot-free fallback directory passed to `mkdtemp`. */
26+
readonly fallback?: string;
27+
/** Platform override used by deterministic boundary tests. */
28+
readonly platform?: NodeJS.Platform;
29+
}
30+
31+
/**
32+
* Select a session root usable as the parent of a Ghidra project path.
33+
*
34+
* Ghidra validates every project path element with `NamingUtilities.checkName`
35+
* and aborts the headless analyzer on any element that starts with `.`, so an
36+
* inherited `TMPDIR` such as `~/.cache/<tool>` fails every Ghidra operation
37+
* with a generic analysis failure. Prefer the inherited base when it is usable,
38+
* then a dot-free platform temp directory, then the nearest dot-free ancestor.
39+
*/
40+
export const ghidraSessionRoot = (
41+
options: GhidraSessionRootOptions = {},
42+
): string => {
43+
const base = resolve(options.base ?? tmpdir());
44+
if (!hasDotPrefixElement(base)) return base;
45+
const platform = options.platform ?? process.platform;
46+
const fallback =
47+
options.fallback ?? (platform === "win32" ? undefined : "/tmp");
48+
if (
49+
fallback !== undefined &&
50+
isAbsolute(fallback) &&
51+
!hasDotPrefixElement(fallback)
52+
)
53+
return resolve(fallback);
54+
return nearestSafeAncestor(base) ?? base;
55+
};

0 commit comments

Comments
 (0)