Skip to content

Commit fc88f5f

Browse files
committed
fix(gateway): fence prepared device auth rotations
1 parent 016237e commit fc88f5f

4 files changed

Lines changed: 267 additions & 63 deletions

File tree

‎src/gateway/client.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,73 @@ describe("GatewayClient connect auth payload", () => {
19881988
client.stop();
19891989
});
19901990

1991+
it.each([
1992+
{ completion: "clear", deviceAuthScope: undefined, store: "device" },
1993+
{ completion: "overwrite", deviceAuthScope: undefined, store: "device" },
1994+
{ completion: "clear", deviceAuthScope: "wss://one.example/rpc", store: "origin" },
1995+
{ completion: "overwrite", deviceAuthScope: "wss://one.example/rpc", store: "origin" },
1996+
] as const)(
1997+
"does not $completion rotated $store auth after the prepared snapshot",
1998+
async ({ completion, deviceAuthScope }) => {
1999+
const preparedDeviceAuth = {
2000+
token: "prepared-device-token",
2001+
role: "operator",
2002+
scopes: ["operator.read"],
2003+
updatedAtMs: 123,
2004+
};
2005+
let durableToken: string | undefined = preparedDeviceAuth.token;
2006+
const clearTokenMock = deviceAuthScope
2007+
? clearOriginDeviceTokenMock
2008+
: clearDeviceAuthTokenMock;
2009+
const storeTokenMock = deviceAuthScope
2010+
? storeOriginDeviceTokenMock
2011+
: storeDeviceAuthTokenMock;
2012+
clearTokenMock.mockImplementation(({ expectedToken }: { expectedToken?: string }) => {
2013+
if (durableToken === expectedToken) {
2014+
durableToken = undefined;
2015+
}
2016+
});
2017+
storeTokenMock.mockImplementation(
2018+
({ expectedToken, token }: { expectedToken?: string; token: string }) => {
2019+
if (durableToken === expectedToken) {
2020+
durableToken = token;
2021+
}
2022+
},
2023+
);
2024+
const client = createClientWithIdentity("device-1", () => {}, {
2025+
preparedDeviceAuth,
2026+
...(deviceAuthScope ? { deviceAuthScope } : {}),
2027+
});
2028+
2029+
const { ws, connect } = startClientAndConnect({ client });
2030+
durableToken = "rotated-device-token";
2031+
if (completion === "clear") {
2032+
emitConnectFailure(ws, connect.id, { code: "AUTH_DEVICE_TOKEN_MISMATCH" });
2033+
await waitForFast(() => expect(clearTokenMock).toHaveBeenCalledOnce());
2034+
} else {
2035+
ws.emitMessage(
2036+
JSON.stringify({
2037+
type: "res",
2038+
id: connect.id,
2039+
ok: true,
2040+
payload: {
2041+
type: "hello-ok",
2042+
auth: {
2043+
role: "operator",
2044+
scopes: ["operator.write"],
2045+
deviceToken: "stale-request-replacement",
2046+
},
2047+
},
2048+
}),
2049+
);
2050+
await waitForFast(() => expect(storeTokenMock).toHaveBeenCalledOnce());
2051+
}
2052+
2053+
expect(durableToken).toBe("rotated-device-token");
2054+
client.stop();
2055+
},
2056+
);
2057+
19912058
it("keeps read-only origin auth loads and hello tokens off writable host callbacks", async () => {
19922059
loadOriginDeviceTokenReadOnlyMock.mockReturnValue({
19932060
token: "stored-origin-token",

‎src/gateway/client.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ function createOpenClawGatewayClientHostDeps(
6161
preparedDeviceAuth?: DeviceAuthEntry,
6262
): GatewayClientHostDeps {
6363
const readOnly = sharedStateMode === "read-only";
64+
// Prepared auth is immutable request input. Any later durable mutation must
65+
// still match this token so a stale request cannot undo a concurrent rotation.
66+
const rotationFence = preparedDeviceAuth
67+
? { expectedToken: preparedDeviceAuth.token }
68+
: undefined;
6469
const deviceAuthDeps: Pick<
6570
GatewayClientHostDeps,
6671
"loadDeviceAuthToken" | "storeDeviceAuthToken" | "clearDeviceAuthToken"
@@ -74,18 +79,32 @@ function createOpenClawGatewayClientHostDeps(
7479
: loadOriginDeviceToken({ ...params, gatewayScope: deviceAuthScope }),
7580
storeDeviceAuthToken: readOnly
7681
? () => {}
77-
: (params) => storeOriginDeviceToken({ ...params, gatewayScope: deviceAuthScope }),
82+
: (params) =>
83+
storeOriginDeviceToken({
84+
...params,
85+
gatewayScope: deviceAuthScope,
86+
...rotationFence,
87+
}),
7888
clearDeviceAuthToken: readOnly
7989
? () => {}
80-
: (params) => clearOriginDeviceToken({ ...params, gatewayScope: deviceAuthScope }),
90+
: (params) =>
91+
clearOriginDeviceToken({
92+
...params,
93+
gatewayScope: deviceAuthScope,
94+
...rotationFence,
95+
}),
8196
}
8297
: readOnly
8398
? {
8499
loadDeviceAuthToken: loadDeviceAuthTokenReadOnly,
85100
storeDeviceAuthToken: () => {},
86101
clearDeviceAuthToken: () => {},
87102
}
88-
: { loadDeviceAuthToken, storeDeviceAuthToken, clearDeviceAuthToken };
103+
: {
104+
loadDeviceAuthToken,
105+
storeDeviceAuthToken: (params) => storeDeviceAuthToken({ ...params, ...rotationFence }),
106+
clearDeviceAuthToken: (params) => clearDeviceAuthToken({ ...params, ...rotationFence }),
107+
};
89108
const preparedDeviceAuthDeps = preparedDeviceAuth
90109
? { ...deviceAuthDeps, loadDeviceAuthToken: () => preparedDeviceAuth }
91110
: deviceAuthDeps;

‎src/infra/device-auth-store.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,71 @@ describe("infra/device-auth-store", () => {
376376
);
377377
});
378378
});
379+
380+
it("keeps credentials rotated after a stale request snapshot", async () => {
381+
await withTempDir("openclaw-device-auth-rotation-", async (stateDir) => {
382+
const env = createEnv(stateDir);
383+
const targets = [
384+
{
385+
name: "device",
386+
load: () => loadDeviceAuthToken({ deviceId: "device-1", role: "operator", env }),
387+
store: (token: string, expectedToken?: string) =>
388+
storeDeviceAuthToken({
389+
deviceId: "device-1",
390+
role: "operator",
391+
token,
392+
scopes: ["operator.read"],
393+
env,
394+
...(expectedToken === undefined ? {} : { expectedToken }),
395+
}),
396+
clear: (expectedToken: string) =>
397+
clearDeviceAuthToken({
398+
deviceId: "device-1",
399+
role: "operator",
400+
env,
401+
expectedToken,
402+
}),
403+
},
404+
{
405+
name: "origin",
406+
load: () =>
407+
loadOriginDeviceToken({
408+
gatewayScope: "wss://one.example",
409+
deviceId: "device-1",
410+
role: "operator",
411+
env,
412+
}),
413+
store: (token: string, expectedToken?: string) =>
414+
storeOriginDeviceToken({
415+
gatewayScope: "wss://one.example",
416+
deviceId: "device-1",
417+
role: "operator",
418+
token,
419+
scopes: ["operator.read"],
420+
env,
421+
...(expectedToken === undefined ? {} : { expectedToken }),
422+
}),
423+
clear: (expectedToken: string) =>
424+
clearOriginDeviceToken({
425+
gatewayScope: "wss://one.example",
426+
deviceId: "device-1",
427+
role: "operator",
428+
env,
429+
expectedToken,
430+
}),
431+
},
432+
];
433+
434+
for (const target of targets) {
435+
const prepared = target.store(`${target.name}-prepared`);
436+
const rotated = target.store(`${target.name}-rotated`);
437+
expect(prepared).not.toBeNull();
438+
expect(rotated).not.toBeNull();
439+
440+
expect(target.store(`${target.name}-stale-replacement`, prepared!.token)).toBeNull();
441+
expect(target.clear(prepared!.token)).toBe(false);
442+
expect(target.load()).toEqual(rotated);
443+
}
444+
});
445+
});
379446
});

0 commit comments

Comments
 (0)