Files
rea/src/cli/firmwareCommands.ts
T
Kaoru0822andClaude Opus 5.5 fc791bb606 fix(paths): select caller paths through symlinks and .. as the filesystem does (#4067)
* fix: select caller paths through symlinks and .. as the filesystem does

CLI adapters, binary target resolution, the reverse-engineering service
and the objdump and Rizin command builders made caller-selected paths
absolute with path.resolve, which collapses `link/..` lexically. On
POSIX the filesystem follows `link` first, so these commands inspected,
hashed or launched a different file and returned internally consistent
Evidence for it.

absoluteCallerPath now owns that step: on POSIX it prefixes a relative
path with the working directory and keeps an absolute path as spelled;
on Windows, where `..` resolves lexically, it keeps win32.resolve.

Fixes #3864
Fixes #3911
Fixes #3912

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep firmware, WASM list and output-root paths on the selected file

Review follow-up. The CLI already passed firmware and WASM paths through
absoluteCallerPath, but snapshotFirmware and SafeOutputTree.create
re-resolved them lexically, and the WASM glue and candidate lists still
used resolve, so those inputs could still select a sibling file or
create output beside the intended directory.

absoluteCallerPath now also drops `.` segments and repeated or trailing
separators on POSIX, which never change the named file, so selected
paths such as `./module.wasm` keep matching their glue references
exactly. Every `..` is still left for the filesystem.

SafeOutputTree keeps host path syntax; its platform argument selects only
the Windows name policy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep directory requirements and relative cwd in caller paths

Review follow-up. absoluteCallerPath stripped a trailing `/` or `/.`, so
`file/` selected the regular file instead of failing with ENOTDIR; it
now keeps one trailing separator. A relative cwd option was appended to
the root; it is now made absolute without collapsing `..`. WASM glue
matching resolved local literals lexically, so a module selected through
`link/..` matched only by basename; the adapter now compares both sides
through absoluteCallerPath and still reports each candidate as given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 05:19:43 -07:00

89 lines
3.4 KiB
TypeScript

import { z } from "incur";
import { FirmwareAnalysisService } from "../application/firmware/FirmwareAnalysisService.js";
import { createFirmwareAnalysisProvider } from "../composition/firmware.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { logCliCommand } from "../cliLogging.js";
import { firmwareInputSchemas } from "../domain/firmware/firmwareAnalysis.js";
import { localPathStringSchema } from "../domain/localPath.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import type { FirmwareOperation } from "../domain/firmware/firmwareAnalysis.js";
import type { Logger } from "pino";
import type { CliInstance } from "./types.js";
import { withCommandCancellation } from "./commandCancellation.js";
import { absoluteCallerPath } from "../filesystem/callerPath.js";
/** One-shot firmware CLI commands backed by the same workflows as MCP. */
export const registerFirmwareCommands = (
cli: CliInstance,
logger: Logger,
environment: Readonly<Record<string, string | undefined>>,
): void => {
const service = new FirmwareAnalysisService(
createFirmwareAnalysisProvider(environment),
);
const extractInput = firmwareInputSchemas.extract_firmware.shape;
const range = extractInput.range.unwrap().shape;
const execute = (
name: string,
operation: FirmwareOperation,
input: unknown,
) =>
withCommandCancellation((signal) =>
logCliCommand(logger, name, async () => {
try {
const result = await service.execute(operation, input, { signal });
return result.ok ? result.value : projectAnalysisError(result.error);
} finally {
await service.close();
}
}),
);
const path = localPathStringSchema.describe(
"Local firmware path; never executed",
);
cli.command(CLI_COMMANDS.inspectFirmwareRegions, {
description:
"Inspect firmware signature candidates without extracting them",
args: z.object({ path }),
run: ({ args }) =>
execute(
CLI_COMMANDS.inspectFirmwareRegions,
"inspect_firmware_regions",
// CLI paths are operator-relative; resolve before shared validation.
{ path: absoluteCallerPath(args.path) },
),
});
cli.command(CLI_COMMANDS.extractFirmware, {
description:
"Extract selected firmware bytes and return verified files and provenance",
args: z.object({
path,
outputDirectory: localPathStringSchema.describe(
"Absent output directory; parent must exist",
),
}),
options: z.object({
offset: range.offset
.optional()
.describe("Selected byte offset; requires length"),
length: range.length
.optional()
.describe("Selected byte length; requires offset"),
maxDepth: extractInput.max_depth,
maxOutputBytes: extractInput.max_output_bytes,
maxOutputFiles: extractInput.max_output_files,
}),
run: ({ args, options }) =>
execute(CLI_COMMANDS.extractFirmware, "extract_firmware", {
path: absoluteCallerPath(args.path),
output_directory: absoluteCallerPath(args.outputDirectory),
...(options.offset === undefined && options.length === undefined
? {}
: { range: { offset: options.offset, length: options.length } }),
max_depth: options.maxDepth,
max_output_bytes: options.maxOutputBytes,
max_output_files: options.maxOutputFiles,
}),
});
};