Skip to content

Commit 8f5f01e

Browse files
committed
test(gateway): rewrite veto-era pairing locks for silent local widening
Five control-ui pairing suite cases and the silent-scope-upgrade poc locked the removed non-silent veto. The suite cases now assert the new invariant (local shared-auth upgrades widen silently, malformed and legacy-shaped rows are repaired by the fresh approval, node-then-operator grants complete without a stranded prompt), and the poc case now exercises the surviving manual-approval gate by disabling autoApproveLocal after its watcher connects, keeping the pairing-request broadcast and remediation-hint assertions alive on a real remaining path. The voice-node bootstrap failure was leakage from the aborted sibling tests, and passes again once they complete their flows.
1 parent 5301f34 commit 8f5f01e

2 files changed

Lines changed: 40 additions & 82 deletions

File tree

‎src/gateway/server.auth.control-ui.pairing.suite.ts‎

Lines changed: 30 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function registerControlUiPairingSuite(): void {
5959
metadata.approvedScopes = ["operator.read", null, 42, ""];
6060
});
6161
};
62-
test("auto-approves local-direct operator pairing despite a remote-looking host header", async () => {
62+
test("auto-approves local-direct operator pairing and scope upgrades despite a remote-looking host header", async () => {
6363
const { getPairedDevice, listDevicePairing } = await import("../infra/device-pairing.js");
6464
const { server, port, prevToken, identityPath, identity, client } =
6565
await startControlUiServerWithOperatorIdentity();
@@ -104,23 +104,22 @@ export function registerControlUiPairingSuite(): void {
104104
nonce: nonce2,
105105
}),
106106
});
107-
expect(res.ok).toBe(false);
108-
expect(res.error?.message ?? "").toContain("pairing required");
107+
// A local shared-auth connect could pair a fresh identity at admin, so the
108+
// widening self-approves silently instead of queueing an unanswerable prompt.
109+
expect(res.ok).toBe(true);
109110
pairing = await listDevicePairing();
110111
const pendingAfterAdmin = pairing.pending.filter(
111112
(entry) => entry.deviceId === identity.deviceId,
112113
);
113-
expect(pendingAfterAdmin).toHaveLength(1);
114-
expectArrayIncludes(pendingAfterAdmin[0]?.scopes, ["operator.admin"]);
115-
if (!(await getPairedDevice(identity.deviceId))) {
116-
throw new Error(`expected paired device ${identity.deviceId}`);
117-
}
114+
expect(pendingAfterAdmin).toHaveLength(0);
115+
const widened = await getPairedDevice(identity.deviceId);
116+
expectArrayIncludes(widened?.approvedScopes, ["operator.admin", "operator.read"]);
118117
ws2.close();
119118
await server.close();
120119
restoreGatewayToken(prevToken);
121120
});
122121

123-
test("requires approval for loopback scope upgrades for control ui clients", async () => {
122+
test("silently widens loopback control ui scope upgrades under shared auth", async () => {
124123
const { getPairedDevice, listDevicePairing } = await import("../infra/device-pairing.js");
125124
const { server, port, prevToken } = await startControlUiServer("secret");
126125
const { identity, identityPath } = await seedApprovedOperatorReadPairing({
@@ -144,21 +143,22 @@ export function registerControlUiPairingSuite(): void {
144143
nonce: nonce2,
145144
}),
146145
});
147-
expect(upgraded.ok).toBe(false);
148-
expect(upgraded.error?.message ?? "").toContain("pairing required");
146+
// A fresh Control UI browser identity holding the shared secret could pair
147+
// at admin silently, so an existing row widens the same way.
148+
expect(upgraded.ok).toBe(true);
149149
const pending = await listDevicePairing();
150150
const pendingUpgrade = pending.pending.filter((entry) => entry.deviceId === identity.deviceId);
151-
expect(pendingUpgrade).toHaveLength(1);
152-
expectArrayIncludes(pendingUpgrade[0]?.scopes, ["operator.admin"]);
151+
expect(pendingUpgrade).toHaveLength(0);
153152
const updated = await getPairedDevice(identity.deviceId);
154-
expect(updated?.tokens?.operator?.scopes ?? []).not.toContain("operator.admin");
153+
expect(updated?.tokens?.operator?.scopes ?? []).toContain("operator.admin");
155154

156155
ws2.close();
157156
await server.close();
158157
restoreGatewayToken(prevToken);
159158
});
160159

161-
test("returns pairing-required for malformed persisted access lists", async () => {
160+
test("silently repairs malformed persisted access lists on local re-approval", async () => {
161+
const { getPairedDevice } = await import("../infra/device-pairing.js");
162162
const { identity, identityPath } = await seedApprovedOperatorReadPairing({
163163
identityPrefix: "openclaw-device-malformed-access-",
164164
clientId: TEST_OPERATOR_CLIENT.id,
@@ -185,11 +185,12 @@ export function registerControlUiPairingSuite(): void {
185185
}),
186186
});
187187

188-
expect(result.ok).toBe(false);
189-
expect(result.error?.message ?? "").toContain("pairing required");
190-
expect((result.error?.details as { reason?: string } | undefined)?.reason).toBe(
191-
"scope-upgrade",
192-
);
188+
// Malformed persisted access lists never grant access by themselves: the
189+
// connect is re-authorized by a fresh silent local approval, which also
190+
// rewrites the row with a clean scope list.
191+
expect(result.ok).toBe(true);
192+
const repaired = await getPairedDevice(identity.deviceId);
193+
expect(repaired?.approvedScopes ?? []).toContain("operator.admin");
193194
} finally {
194195
ws?.close();
195196
await server.close();
@@ -284,7 +285,7 @@ export function registerControlUiPairingSuite(): void {
284285
}
285286
});
286287

287-
test("auto-approves local-direct node pairing, then queues operator scope approval", async () => {
288+
test("auto-approves local-direct node pairing, then silently grants operator scopes", async () => {
288289
const { getPairedDevice, listDevicePairing } = await import("../infra/device-pairing.js");
289290
const { identityPath, identity, client } =
290291
await createOperatorIdentityFixture("openclaw-device-scope-");
@@ -318,15 +319,13 @@ export function registerControlUiPairingSuite(): void {
318319
"operator.read",
319320
"operator.write",
320321
]);
321-
expect(operatorConnect.ok).toBe(false);
322-
expect(operatorConnect.error?.message ?? "").toContain("pairing required");
322+
expect(operatorConnect.ok).toBe(true);
323323

324324
const pending = await listDevicePairing();
325325
const pendingForTestDevice = pending.pending.filter(
326326
(entry) => entry.deviceId === identity.deviceId,
327327
);
328-
expect(pendingForTestDevice).toHaveLength(1);
329-
expectArrayIncludes(pendingForTestDevice[0]?.scopes, ["operator.read", "operator.write"]);
328+
expect(pendingForTestDevice).toHaveLength(0);
330329

331330
const paired = await getPairedDevice(identity.deviceId);
332331
expectArrayIncludes(paired?.roles, ["node", "operator"]);
@@ -430,7 +429,7 @@ export function registerControlUiPairingSuite(): void {
430429
}
431430
});
432431

433-
test("requires approval for local scope upgrades even when paired metadata is legacy-shaped", async () => {
432+
test("silently widens local scope upgrades even when paired metadata is legacy-shaped", async () => {
434433
const { getPairedDevice, listDevicePairing } = await import("../infra/device-pairing.js");
435434
const { identity, identityPath } = await seedApprovedOperatorReadPairing({
436435
identityPrefix: "openclaw-device-legacy-",
@@ -461,68 +460,18 @@ export function registerControlUiPairingSuite(): void {
461460
nonce: upgradeNonce,
462461
}),
463462
});
464-
expect(upgraded.ok).toBe(false);
465-
expect(upgraded.error?.message ?? "").toContain("pairing required");
466-
expect(
467-
(
468-
upgraded.error?.details as
469-
| {
470-
reason?: string;
471-
requestedRole?: string;
472-
requestedScopes?: string[];
473-
approvedScopes?: string[];
474-
}
475-
| undefined
476-
)?.reason,
477-
).toBe("scope-upgrade");
478-
expect(
479-
(
480-
upgraded.error?.details as
481-
| {
482-
reason?: string;
483-
requestedRole?: string;
484-
requestedScopes?: string[];
485-
approvedScopes?: string[];
486-
}
487-
| undefined
488-
)?.requestedRole,
489-
).toBe("operator");
490-
expect(
491-
(
492-
upgraded.error?.details as
493-
| {
494-
reason?: string;
495-
requestedRole?: string;
496-
requestedScopes?: string[];
497-
approvedScopes?: string[];
498-
}
499-
| undefined
500-
)?.requestedScopes,
501-
).toEqual(["operator.admin"]);
502-
expect(
503-
(
504-
upgraded.error?.details as
505-
| {
506-
reason?: string;
507-
requestedRole?: string;
508-
requestedScopes?: string[];
509-
approvedScopes?: string[];
510-
}
511-
| undefined
512-
)?.approvedScopes,
513-
).toEqual(["operator.read"]);
463+
// Legacy-shaped rows must not break the upgrade flow: the silent local
464+
// approval rewrites the row with the widened, normalized scope list.
465+
expect(upgraded.ok).toBe(true);
514466
wsUpgrade.close();
515467

516468
const pendingUpgrade = (await listDevicePairing()).pending.find(
517469
(entry) => entry.deviceId === identity.deviceId,
518470
);
519-
if (!pendingUpgrade) {
520-
throw new Error(`expected pending upgrade for device ${identity.deviceId}`);
521-
}
522-
expectArrayIncludes(pendingUpgrade.scopes, ["operator.admin"]);
471+
expect(pendingUpgrade).toBeUndefined();
523472
const repaired = await getPairedDevice(identity.deviceId);
524473
expect(repaired?.role).toBe("operator");
525-
expectArrayIncludes(repaired?.approvedScopes, ["operator.read"]);
474+
expectArrayIncludes(repaired?.approvedScopes, ["operator.admin", "operator.read"]);
526475
} finally {
527476
ws2?.close();
528477
await server.close();

‎src/gateway/server.silent-scope-upgrade-reconnect.poc.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ async function expectRejectedScopeUpgradeAttempt({
153153
}
154154

155155
describe("gateway silent scope-upgrade reconnect", () => {
156-
test("does not silently widen a read-scoped paired device to admin on shared-auth reconnect", async () => {
156+
test("keeps scope upgrades on manual approval when autoApproveLocal is disabled", async () => {
157+
const { replaceConfigFile } = await import("../config/config.js");
157158
const started = await startServerWithClient("secret");
158159
const paired = await issueReadScopedOperatorToken({
159160
name: "silent-scope-upgrade-reconnect-poc",
@@ -168,6 +169,14 @@ describe("gateway silent scope-upgrade reconnect", () => {
168169

169170
try {
170171
({ ws: watcherWs, requestedEvent } = await watchScopeUpgradeRequests(started.port));
172+
// autoApproveLocal=false is the operator opt-out that keeps every local
173+
// access grant — including scope upgrades — on the explicit prompt path.
174+
// Flipped after the watcher connected so only the upgrade attempt is
175+
// gated.
176+
await replaceConfigFile({
177+
nextConfig: { gateway: { nodes: { pairing: { autoApproveLocal: false } } } },
178+
afterWrite: { mode: "auto" },
179+
});
171180
sharedAuthReconnectWs = await openTrackedWs(started.port);
172181
const sharedAuthUpgradeAttempt = await connectReq(sharedAuthReconnectWs, {
173182
token: "secret",

0 commit comments

Comments
 (0)