Skip to content

Commit ebd6139

Browse files
committed
fix(cli): validate node options before Gateway lookup
1 parent 4eea0d3 commit ebd6139

8 files changed

Lines changed: 103 additions & 67 deletions

‎src/cli/nodes-cli.coverage.test.ts‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe("nodes-cli coverage", () => {
9898
return;
9999
}
100100
sharedProgram.exitOverride();
101-
await registerNodesCli(sharedProgram);
101+
await registerNodesCli(sharedProgram, ["node", "openclaw", "nodes", "status"]);
102102
});
103103

104104
beforeEach(() => {
@@ -292,6 +292,42 @@ describe("nodes-cli coverage", () => {
292292
args: ["nodes", "rename", "--node", "mac-1", "--name", " "],
293293
message: "--name must not be empty",
294294
},
295+
{
296+
label: "push with an invalid environment",
297+
command: "push",
298+
args: ["nodes", "push", "--node", "mac-1", "--environment", "staging"],
299+
message: "invalid --environment (use sandbox|production)",
300+
},
301+
{
302+
label: "notify without a title or body",
303+
command: "notify",
304+
args: ["nodes", "notify", "--node", "mac-1", "--title", " ", "--body", " "],
305+
message: "missing --title or --body",
306+
},
307+
{
308+
label: "camera snap with an invalid facing",
309+
command: "camera snap",
310+
args: ["nodes", "camera", "snap", "--node", "mac-1", "--facing", "side"],
311+
message: "invalid facing: side (expected front|back|both)",
312+
},
313+
{
314+
label: "camera clip with an invalid facing",
315+
command: "camera clip",
316+
args: ["nodes", "camera", "clip", "--node", "mac-1", "--facing", "both"],
317+
message: "invalid facing: both (expected front|back)",
318+
},
319+
{
320+
label: "camera clip with an invalid duration",
321+
command: "camera clip",
322+
args: ["nodes", "camera", "clip", "--node", "mac-1", "--duration", "later"],
323+
message: "Invalid duration",
324+
},
325+
{
326+
label: "screen record with an invalid duration",
327+
command: "screen record",
328+
args: ["nodes", "screen", "record", "--node", "mac-1", "--duration", "later"],
329+
message: "Invalid duration",
330+
},
295331
])("reports $label once before calling the gateway", async ({ command, args, message }) => {
296332
await expect(sharedProgram.parseAsync(args, { from: "user" })).rejects.toThrow("__exit__:1");
297333

@@ -605,9 +641,13 @@ describe("nodes-cli coverage", () => {
605641
],
606642
flag: "--invoke-timeout",
607643
},
608-
])("rejects partial numeric option for $args", async ({ args, flag }) => {
609-
await expect(sharedProgram.parseAsync(args, { from: "user" })).rejects.toThrow("__exit__:1");
610-
expect(runtimeErrors.at(-1)).toContain(`${flag} must be`);
611-
expect(lastNodeInvokeCall).toBeNull();
612-
});
644+
])(
645+
"rejects invalid numeric option before calling the gateway for $args",
646+
async ({ args, flag }) => {
647+
await expect(sharedProgram.parseAsync(args, { from: "user" })).rejects.toThrow("__exit__:1");
648+
expect(runtimeErrors.at(-1)).toContain(`${flag} must be`);
649+
expect(callGateway).not.toHaveBeenCalled();
650+
expect(lastNodeInvokeCall).toBeNull();
651+
},
652+
);
613653
});

‎src/cli/nodes-cli/register.camera.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ export function registerNodesCameraCommands(nodes: Command) {
128128
.option("--invoke-timeout <ms>", "Node invoke timeout in ms (default 20000)", "20000")
129129
.action(async (opts: NodesRpcOpts) => {
130130
await runNodesCommand("camera snap", async () => {
131-
const node = await resolveCliNode(opts, normalizeOptionalString(opts.node) ?? "");
132-
const nodeId = node.nodeId;
133131
const facingOpt = normalizeLowercaseStringOrEmpty(
134132
normalizeOptionalString(opts.facing) ?? "",
135133
);
@@ -151,6 +149,12 @@ export function registerNodesCameraCommands(nodes: Command) {
151149
});
152150
const delayMs = parseOptionalNodeNonNegativeInteger(opts.delayMs, "--delay-ms");
153151
const deviceId = normalizeOptionalString(opts.deviceId);
152+
const timeoutMs = parseOptionalNodePositiveInteger(
153+
opts.invokeTimeout,
154+
"--invoke-timeout",
155+
);
156+
const node = await resolveCliNode(opts, normalizeOptionalString(opts.node) ?? "");
157+
const nodeId = node.nodeId;
154158
if (deviceId && facing === "both" && node.platform?.toLowerCase() !== "linux") {
155159
throw new Error("facing=both is not allowed when --device-id is set");
156160
}
@@ -159,11 +163,6 @@ export function registerNodesCameraCommands(nodes: Command) {
159163
platform: node.platform,
160164
deviceId,
161165
});
162-
const timeoutMs = parseOptionalNodePositiveInteger(
163-
opts.invokeTimeout,
164-
"--invoke-timeout",
165-
);
166-
167166
const results: Array<{
168167
facing: CameraArtifactFacing;
169168
path: string;
@@ -235,17 +234,17 @@ export function registerNodesCameraCommands(nodes: Command) {
235234
.option("--invoke-timeout <ms>", "Node invoke timeout in ms (default 90000)", "90000")
236235
.action(async (opts: NodesRpcOpts & { audio?: boolean }) => {
237236
await runNodesCommand("camera clip", async () => {
238-
const node = await resolveCliNode(opts, normalizeOptionalString(opts.node) ?? "");
239-
const nodeId = node.nodeId;
240237
const facing = parseFacing(opts.facing ?? "front");
241-
const target = resolveCameraClipTarget({ facing, platform: node.platform });
242238
const durationMs = parseDurationMs(opts.duration ?? "3000");
243239
const includeAudio = opts.audio !== false;
244240
const timeoutMs = parseOptionalNodePositiveInteger(
245241
opts.invokeTimeout,
246242
"--invoke-timeout",
247243
);
248244
const deviceId = normalizeOptionalString(opts.deviceId);
245+
const node = await resolveCliNode(opts, normalizeOptionalString(opts.node) ?? "");
246+
const nodeId = node.nodeId;
247+
const target = resolveCameraClipTarget({ facing, platform: node.platform });
249248

250249
const invokeParams = buildNodeInvokeParams({
251250
nodeId,

‎src/cli/nodes-cli/register.invoke.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export function registerNodesInvokeCommands(nodes: Command) {
4949
);
5050
}
5151
const params = parseNodeInvokeParams(opts.params);
52-
const nodeId = await resolveCliNodeId(opts, nodeQuery);
5352
const timeoutMs = parseOptionalNodePositiveInteger(
5453
opts.invokeTimeout,
5554
"--invoke-timeout",
5655
);
56+
const nodeId = await resolveCliNodeId(opts, nodeQuery);
5757

5858
const invokeParams: Record<string, unknown> = {
5959
nodeId,

‎src/cli/nodes-cli/register.location.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export function registerNodesLocationCommands(nodes: Command) {
4141
if (opts.accuracy !== undefined && desiredAccuracy === undefined) {
4242
throw new Error("invalid --accuracy (use coarse|balanced|precise)");
4343
}
44-
const nodeId = await resolveCliNodeId(opts, opts.node ?? "");
4544
const maxAgeMs = parseOptionalNodeNonNegativeInteger(opts.maxAge, "--max-age");
4645
const timeoutMs = parseOptionalNodePositiveInteger(
4746
opts.locationTimeout,
@@ -51,6 +50,7 @@ export function registerNodesLocationCommands(nodes: Command) {
5150
opts.invokeTimeout,
5251
"--invoke-timeout",
5352
);
53+
const nodeId = await resolveCliNodeId(opts, opts.node ?? "");
5454

5555
const invokeParams: Record<string, unknown> = {
5656
nodeId,

‎src/cli/nodes-cli/register.notify.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export function registerNodesNotifyCommand(nodes: Command) {
3737
.option("--invoke-timeout <ms>", "Node invoke timeout in ms (default 15000)", "15000")
3838
.action(async (opts: NodesRpcOpts) => {
3939
await runNodesCommand("notify", async () => {
40-
const nodeId = await resolveCliNodeId(opts, normalizeOptionalString(opts.node) ?? "");
4140
const title = normalizeOptionalString(opts.title) ?? "";
4241
const body = normalizeOptionalString(opts.body) ?? "";
4342
if (!title && !body) {
@@ -47,6 +46,7 @@ export function registerNodesNotifyCommand(nodes: Command) {
4746
opts.invokeTimeout,
4847
"--invoke-timeout",
4948
);
49+
const nodeId = await resolveCliNodeId(opts, normalizeOptionalString(opts.node) ?? "");
5050
const invokeParams: Record<string, unknown> = {
5151
nodeId,
5252
command: "system.notify",

‎src/cli/nodes-cli/register.push.ts‎

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,6 @@ import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
1010
import { callNodesGatewayCli, nodesCallOpts, resolveCliNodeId } from "./rpc.js";
1111
import type { NodesRpcOpts } from "./types.js";
1212

13-
type NodesPushOpts = NodesRpcOpts & {
14-
node?: string;
15-
title?: string;
16-
body?: string;
17-
environment?: string;
18-
};
19-
20-
function normalizeEnvironment(value: unknown): "sandbox" | "production" | null {
21-
if (typeof value !== "string") {
22-
return null;
23-
}
24-
const normalized = normalizeOptionalLowercaseString(value);
25-
if (normalized === "sandbox" || normalized === "production") {
26-
return normalized;
27-
}
28-
return null;
29-
}
30-
3113
/** Register the node push-test command. */
3214
export function registerNodesPushCommand(nodes: Command) {
3315
nodesCallOpts(
@@ -38,15 +20,15 @@ export function registerNodesPushCommand(nodes: Command) {
3820
.option("--title <text>", "Push title", "OpenClaw")
3921
.option("--body <text>", "Push body")
4022
.option("--environment <sandbox|production>", "Override APNs environment")
41-
.action(async (opts: NodesPushOpts) => {
23+
.action(async (opts: NodesRpcOpts & { environment?: string }) => {
4224
await runNodesCommand("push", async () => {
25+
const environment = normalizeOptionalLowercaseString(opts.environment);
26+
if (opts.environment && environment !== "sandbox" && environment !== "production") {
27+
throw new Error("invalid --environment (use sandbox|production)");
28+
}
4329
const nodeId = await resolveCliNodeId(opts, normalizeOptionalString(opts.node) ?? "");
4430
const title = normalizeOptionalString(opts.title) || "OpenClaw";
4531
const body = normalizeOptionalString(opts.body) || `Push test for node ${nodeId}`;
46-
const environment = normalizeEnvironment(opts.environment);
47-
if (opts.environment && !environment) {
48-
throw new Error("invalid --environment (use sandbox|production)");
49-
}
5032

5133
const params: Record<string, unknown> = {
5234
nodeId,

‎src/cli/nodes-cli/register.screen.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export function registerNodesScreenCommands(nodes: Command) {
3939
.option("--invoke-timeout <ms>", "Node invoke timeout in ms (default 120000)", "120000")
4040
.action(async (opts: NodesRpcOpts & { out?: string }) => {
4141
await runNodesCommand("screen record", async () => {
42-
const nodeId = await resolveCliNodeId(opts, opts.node ?? "");
4342
const durationMs = parseDurationMs(opts.duration ?? "");
4443
const screenIndex = parseOptionalNodeNonNegativeInteger(opts.screen ?? "0", "--screen");
4544
const fps = parseOptionalNodeFiniteNumber(opts.fps ?? "10", "--fps", {
@@ -49,6 +48,7 @@ export function registerNodesScreenCommands(nodes: Command) {
4948
opts.invokeTimeout,
5049
"--invoke-timeout",
5150
);
51+
const nodeId = await resolveCliNodeId(opts, opts.node ?? "");
5252

5353
const invokeParams = buildNodeInvokeParams({
5454
nodeId,

‎src/cli/program.nodes-push.e2e.test.ts‎

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,43 @@ describe("cli program (nodes push)", () => {
9090
});
9191

9292
it.each([
93-
["text", []],
94-
["JSON", ["--json"]],
95-
])("keeps successful APNs push %s output at exit zero", async (_label, extraArgs) => {
96-
const result: PushTestResult = {
97-
ok: true,
98-
status: 200,
99-
apnsId: "apns-id",
100-
tokenSuffix: "1234abcd",
101-
topic: "ai.openclaw.ios",
102-
environment: "sandbox",
103-
transport: "direct",
104-
};
105-
mockPushResult(result);
106-
107-
await runPush(extraArgs);
108-
109-
expect(process.exitCode).toBeUndefined();
110-
if (extraArgs.length > 0) {
111-
expect(JSON.parse(runtimeOutput())).toEqual(result);
112-
} else {
113-
expect(runtimeOutput()).toContain("push.test status=200 ok=true env=sandbox");
114-
}
115-
expect(runtime.exit).not.toHaveBeenCalled();
116-
});
93+
{ label: "text", extraArgs: [], environment: undefined },
94+
{ label: "JSON", extraArgs: ["--json"], environment: undefined },
95+
{ label: "empty environment", extraArgs: ["--environment", ""], environment: undefined },
96+
{ label: "sandbox", extraArgs: ["--environment", " SaNdBoX "], environment: "sandbox" },
97+
{ label: "production", extraArgs: ["--environment", "PrOdUcTiOn"], environment: "production" },
98+
])(
99+
"keeps successful APNs push $label output at exit zero",
100+
async ({ extraArgs, environment }) => {
101+
const result: PushTestResult = {
102+
ok: true,
103+
status: 200,
104+
apnsId: "apns-id",
105+
tokenSuffix: "1234abcd",
106+
topic: "ai.openclaw.ios",
107+
environment: "sandbox",
108+
transport: "direct",
109+
};
110+
mockPushResult(result);
111+
112+
await runPush(extraArgs);
113+
114+
expect(process.exitCode).toBeUndefined();
115+
const pushRequest = programGatewayCallMock.mock.calls
116+
.map(([request]) => request as { method?: string; params?: unknown })
117+
.find(({ method }) => method === "push.test");
118+
expect(pushRequest?.params).toEqual({
119+
nodeId: "ios-node",
120+
title: "OpenClaw",
121+
body: "Push test for node ios-node",
122+
...(environment ? { environment } : {}),
123+
});
124+
if (extraArgs.includes("--json")) {
125+
expect(JSON.parse(runtimeOutput())).toEqual(result);
126+
} else {
127+
expect(runtimeOutput()).toContain("push.test status=200 ok=true env=sandbox");
128+
}
129+
expect(runtime.exit).not.toHaveBeenCalled();
130+
},
131+
);
117132
});

0 commit comments

Comments
 (0)