Skip to content

Commit 7a38f14

Browse files
authored
fix(node): approved local commands no longer fail identity validation (#103886)
* fix(node): preserve approval replay identity * docs(node): explain replay identity boundary * fix(node): reuse persisted replay identity * chore(release): leave changelog to release automation
1 parent b56bad3 commit 7a38f14

2 files changed

Lines changed: 135 additions & 6 deletions

File tree

‎src/agents/tools/gateway.test.ts‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,107 @@ describe("gateway tool defaults", () => {
319319
expect(call.deviceIdentity).toEqual(mocks.deviceIdentity);
320320
});
321321

322+
it.each([
323+
{
324+
name: "approved flag",
325+
params: { approved: true, runId: "approval-inline" },
326+
},
327+
{
328+
name: "allow-once decision",
329+
params: { approvalDecision: "allow-once", runId: "approval-async" },
330+
},
331+
{
332+
name: "allow-always decision",
333+
params: { approvalDecision: "allow-always", runId: "approval-always" },
334+
},
335+
])("binds persisted device identity to node system.run with $name", async ({ params }) => {
336+
mocks.callGateway.mockResolvedValueOnce({ ok: true });
337+
338+
await callGatewayTool(
339+
"node.invoke",
340+
{},
341+
{ nodeId: "node-1", command: "system.run", params, idempotencyKey: "invoke-1" },
342+
{ scopes: ["operator.write", "operator.approvals"] },
343+
);
344+
345+
const call = capturedGatewayCall();
346+
expect(call.deviceIdentity).toEqual(mocks.deviceIdentity);
347+
expect(call).not.toHaveProperty("approvalRuntimeToken");
348+
});
349+
350+
it.each([
351+
{
352+
name: "unapproved system.run",
353+
command: "system.run",
354+
params: { approved: false },
355+
},
356+
{
357+
name: "system.run.prepare",
358+
command: "system.run.prepare",
359+
params: { approved: true, approvalDecision: "allow-once" },
360+
},
361+
{
362+
name: "unrelated node command",
363+
command: "system.info",
364+
params: { approved: true, approvalDecision: "allow-always" },
365+
},
366+
])("keeps ordinary node.invoke device-less for $name", async ({ command, params }) => {
367+
mocks.callGateway.mockResolvedValueOnce({ ok: true });
368+
369+
await callGatewayTool(
370+
"node.invoke",
371+
{},
372+
{
373+
nodeId: "node-1",
374+
command,
375+
params,
376+
idempotencyKey: "invoke-1",
377+
},
378+
);
379+
380+
const call = capturedGatewayCall();
381+
expect(call).not.toHaveProperty("deviceIdentity");
382+
expect(call).not.toHaveProperty("approvalRuntimeToken");
383+
});
384+
385+
it("fails approved node system.run closed without a persisted identity", async () => {
386+
mocks.persistedDeviceIdentity = null;
387+
388+
await expect(
389+
callGatewayTool(
390+
"node.invoke",
391+
{},
392+
{
393+
nodeId: "node-1",
394+
command: "system.run",
395+
params: { approved: true, runId: "approval-id" },
396+
idempotencyKey: "invoke-1",
397+
},
398+
{ scopes: ["operator.write", "operator.approvals"] },
399+
),
400+
).rejects.toThrow("approved node gateway calls require a stable device identity");
401+
expect(mocks.callGateway).not.toHaveBeenCalled();
402+
});
403+
404+
it("reuses an existing replay identity without trying to create one", async () => {
405+
mocks.deviceIdentityError = new Error("must not create identity during replay");
406+
mocks.callGateway.mockResolvedValueOnce({ ok: true });
407+
408+
await callGatewayTool(
409+
"node.invoke",
410+
{},
411+
{
412+
nodeId: "node-1",
413+
command: "system.run",
414+
params: { approved: true, runId: "approval-id" },
415+
idempotencyKey: "invoke-1",
416+
},
417+
{ scopes: ["operator.write", "operator.approvals"] },
418+
);
419+
420+
expect(capturedGatewayCall().deviceIdentity).toEqual(mocks.deviceIdentity);
421+
});
422+
322423
it("does not mark direct cron helper calls with agent runtime identity", async () => {
323424
mocks.callGateway.mockResolvedValueOnce({ id: "job-1" });
324425

‎src/agents/tools/gateway.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*
44
* Resolves gateway URL/token overrides, local credentials, and least-privilege operator scopes.
55
*/
6+
import { asNullableRecord } from "@openclaw/normalization-core/record-coerce";
67
import {
78
normalizeLowercaseStringOrEmpty,
89
normalizeOptionalString,
@@ -240,29 +241,55 @@ function resolveApprovalRuntimeTokenForGatewayTool(params: {
240241
return getOperatorApprovalRuntimeToken();
241242
}
242243

244+
function isApprovalReplayNodeSystemRun(method: string, callParams: unknown): boolean {
245+
const invoke = method === "node.invoke" ? asNullableRecord(callParams) : null;
246+
const run = invoke?.command === "system.run" ? asNullableRecord(invoke.params) : null;
247+
const decision = normalizeOptionalString(run?.approvalDecision);
248+
return run?.approved === true || decision === "allow-once" || decision === "allow-always";
249+
}
250+
243251
function resolveApprovalRequesterDeviceIdentityForGatewayTool(params: {
244252
method: string;
253+
callParams: unknown;
245254
opts: GatewayCallOptions;
246255
target: GatewayOverrideTarget;
247256
}): DeviceIdentity | undefined {
248-
if (!APPROVAL_RUNTIME_METHODS.has(params.method)) {
257+
const isApprovalRuntimeMethod = APPROVAL_RUNTIME_METHODS.has(params.method);
258+
const isNodeApprovalReplay = isApprovalReplayNodeSystemRun(params.method, params.callParams);
259+
if (!isApprovalRuntimeMethod && !isNodeApprovalReplay) {
249260
return undefined;
250261
}
251-
if (trimToUndefined(params.opts.gatewayUrl) !== undefined) {
262+
if (isApprovalRuntimeMethod && trimToUndefined(params.opts.gatewayUrl) !== undefined) {
252263
return undefined;
253264
}
254265
try {
266+
if (isNodeApprovalReplay) {
267+
// Replay must reuse the identity present when the approval was registered.
268+
// Creating one here could turn a device-less record into a different identity.
269+
const identity = loadDeviceIdentityIfPresent();
270+
if (!identity) {
271+
throw new Error("device identity is not persisted");
272+
}
273+
return identity;
274+
}
255275
const identity = loadOrCreateDeviceIdentity();
256-
// Approval request/wait calls may cross backend processes. Bind them to the
257-
// persisted device id so a process-local approval token mismatch cannot hide
258-
// the pending record from the matching wait call.
259-
// Reject loadOrCreate's unpersisted fallback so another process can see the same id.
276+
// Approval registration and wait can use separate gateway connections.
277+
// Reject loadOrCreate's unpersisted fallback so both sides bind the same id.
260278
const persistedIdentity = loadDeviceIdentityIfPresent();
261279
if (persistedIdentity?.deviceId !== identity.deviceId) {
262280
throw new Error("device identity is not persisted");
263281
}
264282
return identity;
265283
} catch (error) {
284+
if (isNodeApprovalReplay) {
285+
throw new Error(
286+
[
287+
"approved node gateway calls require a stable device identity.",
288+
"Fix the OpenClaw state directory permissions and retry the approval.",
289+
].join(" "),
290+
{ cause: error },
291+
);
292+
}
266293
if (params.target === "local") {
267294
return undefined;
268295
}
@@ -347,6 +374,7 @@ export async function callGatewayTool<T = Record<string, unknown>>(
347374
});
348375
const deviceIdentity = resolveApprovalRequesterDeviceIdentityForGatewayTool({
349376
method,
377+
callParams: params,
350378
opts,
351379
target: gateway.target,
352380
});

0 commit comments

Comments
 (0)