Files
reverse-engineering-anything/tests/composition/reverseEngineeringCommandPort.test.ts
T
morluto 422cff00ab refactor(process): give POSIX PATH search order one owner (#10891)
* refactor(process): give POSIX PATH search order one owner

Program lookup, the packet-tool locator and Jadx's Java lookup each split
PATH and resolved empty and relative entries themselves. They now take the
candidates from posixPathCandidates in src/process/executableSearch and keep
only their own probe and failure reporting. A counted architecture guard
keeps new PATH splits from returning; the remaining Windows and Ghidra
searches are baselined.

* refactor(process): move the colon-split PATH searches onto the search owner

REA's launcher-alias lookup and Ghidra's macOS Java lookup split PATH on
":" and skipped relative entries themselves; they now use
posixPathCandidates with relativeEntries: "skip", and the guard counts
colon splits of PATH as well.

* fix(process): produce PATH candidates lazily so an unused entry cannot fail an earlier match

* refactor(process): give Windows PATH/PATHEXT search order the executable search owner (#10916)

* refactor(process): give Windows PATH/PATHEXT search order the executable search owner

* fix(process): keep later PATH entries searchable after a stray quote or a removed working directory

* fix(reverse): refuse a Windows objdump or Rizin command that PATH does not find (#10953)

* fix(reverse): refuse an objdump or Rizin command Windows PATH does not find instead of launching the bare name

* fix(reverse): report a Windows lookup refusal after cancellation and to capability discovery

* fix(desktop): search a Windows host's PATH as Windows does when doctor looks up a program

programOnPath sent every host through the POSIX PATH search, so on Windows a
relative PATH entry reached absoluteCallerPath with platform linux and the
Windows working directory, which resolved against itself until the stack
overflowed. programOnPath now searches PATH and PATHEXT through
windowsPathCandidates on a Windows host, and absoluteCallerPath refuses a
non-POSIX working directory with a TypeError instead of recursing.
2026-11-15 18:24:04 +08:00

389 lines
12 KiB
TypeScript

import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { ReverseEngineeringService } from "../../src/application/reverse/ReverseEngineeringService.js";
import { analysisErrorProjectionSchema } from "../../src/contracts/errorSchemas.js";
import { projectAnalysisError } from "../../src/domain/analysisErrorProjection.js";
import { ok } from "../../src/domain/result.js";
import { createReverseEngineeringCommandPort } from "../../src/composition/reverseEngineering.js";
import { execFileOutput } from "../../src/process/ExecFileOutput.js";
type ExecFileOutputOptions = NonNullable<Parameters<typeof execFileOutput>[2]>;
it("uses provider profiles supplied through the application port", async () => {
const directory = await mkdtemp(join(tmpdir(), "rea-reverse-port-"));
const path = join(directory, "fixture.bin");
await writeFile(path, Buffer.from([0x7f, 0x45, 0x4c, 0x46]));
const run = vi.fn(
async (
_command: string,
_args: readonly string[],
_options?: ExecFileOutputOptions,
) => ({ stdout: "injected", stderr: "" }),
);
const service = new ReverseEngineeringService({
environment: { REA_OBJDUMP_COMMAND: "ambient-objdump" },
commands: {
objdump: () =>
ok({
executable: "injected-objdump",
arguments: ["--injected", path],
identity: {
id: "test.objdump",
name: "Injected objdump",
version: null,
},
environmentOverrides: { INJECTED_PROFILE: "yes" },
}),
rizin: () =>
ok({
executable: "injected-rizin",
arguments: ["--injected", path],
identity: { id: "test.rizin", name: "Injected Rizin", version: null },
}),
},
run,
});
try {
const result = await service.inspectWithObjdump({
path,
operation: "file_headers",
});
expect(result.ok).toBe(true);
const call = run.mock.calls[0];
expect(call?.[0]).toBe("injected-objdump");
expect(call?.[1]).toEqual(["--injected", path]);
expect(call?.[2]?.env?.INJECTED_PROFILE).toBe("yes");
if (result.ok) expect(result.value.provider.id).toBe("test.objdump");
} finally {
await rm(directory, { recursive: true, force: true });
}
});
it("does not reintroduce ambient variables absent from the captured provider environment", async () => {
const directory = await mkdtemp(join(tmpdir(), "rea-reverse-env-"));
const path = join(directory, "fixture.bin");
const variable = "REA_TEST_LATE_PROVIDER_ENV";
const previous = process.env[variable];
await writeFile(path, Buffer.from([0x7f, 0x45, 0x4c, 0x46]));
process.env[variable] = "late-ambient-value";
const run = vi.fn(
async (
_command: string,
_args: readonly string[],
_options?: ExecFileOutputOptions,
) => ({ stdout: "captured", stderr: "" }),
);
const service = new ReverseEngineeringService({
environment: { REA_OBJDUMP_COMMAND: "captured-objdump" },
commands: {
objdump: () =>
ok({
executable: "captured-objdump",
arguments: [path],
identity: {
id: "test.objdump",
name: "Captured objdump",
version: null,
},
environmentOverrides: { PROFILE_VALUE: "explicit" },
}),
rizin: () =>
ok({
executable: "captured-rizin",
arguments: [path],
identity: { id: "test.rizin", name: "Captured Rizin", version: null },
}),
},
run,
});
try {
await service.inspectWithObjdump({ path, operation: "file_headers" });
expect(run.mock.calls[0]?.[2]?.env).toEqual({
REA_OBJDUMP_COMMAND: "captured-objdump",
PROFILE_VALUE: "explicit",
});
} finally {
if (previous === undefined) delete process.env[variable];
else process.env[variable] = previous;
await rm(directory, { recursive: true, force: true });
}
});
it("projects missing and non-file objdump targets with actionable path reasons", async () => {
const directory = await mkdtemp(
join(tmpdir(), "rea-reverse-invalid-target-"),
);
const missing = join(directory, "missing.bin");
const run = vi.fn(async () => ({ stdout: "", stderr: "" }));
const service = new ReverseEngineeringService({
commands: {
objdump: () =>
ok({
executable: "unused-objdump",
arguments: [missing],
identity: {
id: "test.objdump",
name: "Test objdump",
version: null,
},
}),
rizin: ({ path }) =>
ok({
executable: "unused-rizin",
arguments: [path],
identity: { id: "test.rizin", name: "Test Rizin", version: null },
}),
},
run,
});
try {
for (const [path, reason] of [
[missing, "not found"],
[directory, "not a regular file"],
] as const) {
const result = await service.inspectWithObjdump({
path,
operation: "symbols",
});
if (result.ok) throw new Error("Expected target validation failure");
const projection = projectAnalysisError(result.error);
expect(analysisErrorProjectionSchema.safeParse(projection).success).toBe(
true,
);
expect(projection).toMatchObject({
code: "invalid_request",
category: "invalid_input",
retryable: false,
message: expect.stringContaining(path),
remediation: {
action: expect.stringContaining("Correct the listed fields"),
},
details: {
operation: "inspect_with_objdump",
issues: [
{
path: ["path"],
reason: "invalid_value",
message: expect.stringContaining(reason),
},
],
},
});
}
expect(run).not.toHaveBeenCalled();
} finally {
await rm(directory, { recursive: true, force: true });
}
});
it("keeps target preflight I/O failures out of invalid-input classification", async () => {
const directory = await mkdtemp(
join(tmpdir(), "rea-reverse-target-io-failure-"),
);
const path = join(directory, "fixture.bin");
await writeFile(path, Buffer.from([0x7f, 0x45, 0x4c, 0x46]));
const ioFailure = Object.assign(new Error("device I/O failure"), {
code: "EIO",
});
const stat = vi.fn(async () => {
throw ioFailure;
});
const run = vi.fn(async () => ({ stdout: "", stderr: "" }));
const service = new ReverseEngineeringService({
stat,
commands: {
objdump: () =>
ok({
executable: "unused-objdump",
arguments: [path],
identity: {
id: "test.objdump",
name: "Test objdump",
version: null,
},
}),
rizin: () =>
ok({
executable: "unused-rizin",
arguments: [path],
identity: { id: "test.rizin", name: "Test Rizin", version: null },
}),
},
run,
});
try {
const result = await service.inspectWithObjdump({
path,
operation: "symbols",
});
if (result.ok) throw new Error("Expected target preflight I/O failure");
expect(result.error).toMatchObject({
_tag: "ProviderAdapterError",
cause: ioFailure,
diagnostics: { phase: "target_preflight", system_code: "EIO" },
});
expect(projectAnalysisError(result.error)).toMatchObject({
code: "execution_failure",
category: "execution_failure",
retryable: false,
details: {
provider_id: "test.objdump",
operation: "inspect_with_objdump",
diagnostics: { phase: "target_preflight", system_code: "EIO" },
},
});
expect(run).not.toHaveBeenCalled();
expect(stat).toHaveBeenCalledOnce();
} finally {
await rm(directory, { recursive: true, force: true });
}
});
it.each([
{ code: "ELOOP", reason: "symbolic-link loop" },
{ code: "ENAMETOOLONG", reason: "path is too long" },
])(
"classifies selected target $code as correctable input",
async ({ code, reason }) => {
const directory = await mkdtemp(
join(tmpdir(), "rea-reverse-target-path-error-"),
);
const path = join(directory, "fixture.bin");
const pathFailure = Object.assign(new Error(`stat failed: ${code}`), {
code,
});
const stat = vi.fn(async () => {
throw pathFailure;
});
const run = vi.fn(async () => ({ stdout: "", stderr: "" }));
const service = new ReverseEngineeringService({
stat,
commands: {
objdump: () =>
ok({
executable: "unused-objdump",
arguments: [path],
identity: {
id: "test.objdump",
name: "Test objdump",
version: null,
},
}),
rizin: () =>
ok({
executable: "unused-rizin",
arguments: [path],
identity: { id: "test.rizin", name: "Test Rizin", version: null },
}),
},
run,
});
try {
const result = await service.inspectWithObjdump({
path,
operation: "symbols",
});
if (result.ok) throw new Error(`Expected ${code} target path failure`);
expect(result.error).toMatchObject({
_tag: "AnalysisInputError",
cause: pathFailure,
});
expect(projectAnalysisError(result.error)).toMatchObject({
code: "invalid_request",
category: "invalid_input",
retryable: false,
message: expect.stringContaining(path),
details: {
operation: "inspect_with_objdump",
issues: [
{
path: ["path"],
reason: "invalid_value",
message: expect.stringContaining(reason),
},
],
},
});
expect(run).not.toHaveBeenCalled();
expect(stat).toHaveBeenCalledOnce();
} finally {
await rm(directory, { recursive: true, force: true });
}
},
);
it.each([
{
operation: "inspect_with_objdump",
environment: { PATH: "", REA_OBJDUMP_COMMAND: "rea-missing-objdump" },
run: (service: ReverseEngineeringService, signal?: AbortSignal) =>
service.inspectWithObjdump(
{ path: "artifact", operation: "file_headers" },
signal === undefined ? {} : { signal },
),
command: "rea-missing-objdump",
reason: { lookup_failure: "not_found" },
},
{
operation: "execute_rizin_command",
environment: { PATH: "C:\\tools", PATHEXT: ".EXE;C:\\bad" },
run: (service: ReverseEngineeringService, signal?: AbortSignal) =>
service.executeRizinCommand(
{ path: "artifact", command: "i" },
signal === undefined ? {} : { signal },
),
command: "rizin",
reason: { lookup_failure: "invalid_pathext", pathext_entry: "C:\\bad" },
},
])(
"refuses $operation on Windows when the bare command is not found on PATH, launching nothing",
async ({ operation, environment, run: execute, command, reason }) => {
const run = vi.fn(async () => ({ stdout: "", stderr: "" }));
const service = new ReverseEngineeringService({
environment,
commands: createReverseEngineeringCommandPort(environment, "win32"),
run,
});
const result = await execute(service);
if (result.ok) throw new Error("Expected a lookup refusal");
const projection = projectAnalysisError(result.error);
expect(analysisErrorProjectionSchema.safeParse(projection).success).toBe(
true,
);
expect(projection).toMatchObject({
code: "capability_unavailable",
details: {
operation,
diagnostics: { boundary: "provider-lookup", command, ...reason },
},
});
// Capability discovery reports the refusal and its repair, as it does
// for a launch failure.
expect(service.observedLaunchFailures()).toEqual({
[operation]: {
reason: projection.message,
remediation: projection.remediation.action,
},
});
// Cancellation is reported before the lookup refusal.
const controller = new AbortController();
const abortReason = new Error("caller stopped");
controller.abort(abortReason);
const cancelled = await execute(service, controller.signal);
expect(cancelled).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError", cause: abortReason },
});
expect(run).not.toHaveBeenCalled();
},
);