Files
reverse-engineering-anything/src/cli/cutterCommands.ts
T
morlutoandArjun Prabhulal 8d8257928b fix(cli): validate integer tokens before coercion
Validate decimal tokens at the CLI owner before safe-number parsing. Apply the same rule to positional indexes/generations and repeated record selectors, retaining leading-zero decimals and canonical MCP inputs.

Refs #4182

Co-authored-by: Arjun Prabhulal <arjun.prabhulal@gmail.com>
2026-10-16 03:13:06 +08:00

77 lines
2.7 KiB
TypeScript

import { z } from "incur";
import type { Logger } from "pino";
import { CutterBridgeClient } from "../cutter/CutterBridgeClient.js";
import { CutterBridgeService } from "../cutter/CutterBridgeService.js";
import { CLI_COMMANDS } from "../cliCommandNames.js";
import { logCliCommand } from "../cliLogging.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { withCommandCancellation } from "./commandCancellation.js";
import type { CliInstance } from "./types.js";
import { cliIntegerArgument } from "./options.js";
/** Register commands that control an already-running Cutter instance. */
export const registerCutterCommands = (
cli: CliInstance,
logger: Logger,
environment: Readonly<NodeJS.ProcessEnv>,
): void => {
const cutter = new CutterBridgeService(new CutterBridgeClient(environment));
cli.command(CLI_COMMANDS.listCutterSessions, {
description:
"List live Cutter instances connected through the REA Python plugin bridge",
args: z.object({}),
run: () =>
logCliCommand(logger, CLI_COMMANDS.listCutterSessions, () =>
cutter.listSessions(),
),
});
cli.command(CLI_COMMANDS.cutterCommand, {
description: "Execute one Rizin command in a selected live Cutter instance",
args: z.object({
sessionId: z
.string()
.uuid()
.describe("Session ID copied from list-cutter-sessions."),
expectedGeneration: cliIntegerArgument(0).describe(
"Session generation returned by list-cutter-sessions.",
),
command: z
.string()
.min(1)
.describe("One unrestricted Rizin command to run in Cutter.")
.refine((value) => !/[\r\n]/u.test(value)),
}),
options: z.object({
jsonOutput: z
.boolean()
.optional()
.describe(
"Return command-specific JSON output when available (independent of CLI output formatting).",
),
json: z
.boolean()
.optional()
.describe(
"Deprecated compatibility input: use --json-output; pass --json=true or --json=false (bare --json formats CLI output).",
),
}),
run: ({ args, options }) =>
withCommandCancellation((signal) =>
logCliCommand(logger, CLI_COMMANDS.cutterCommand, async () => {
const result = await cutter.execute(
{
session_id: args.sessionId,
expected_generation: args.expectedGeneration,
command: args.command,
json: options.jsonOutput ?? options.json ?? false,
},
{ signal },
);
return result.ok ? result.value : projectAnalysisError(result.error);
}),
),
});
};