Skip to content

Commit 91cc37b

Browse files
authored
fix(gateway): stop fabricating scope-upgrade requests on trusted-proxy reconnects (#129563)
* fix(gateway): stop fabricating scope-upgrade requests on trusted-proxy reconnects Same-key trusted-proxy reconnects whose auto-approvable scopes are already covered by the paired grant now narrow the session in place instead of running the pairing request/approve machinery: no pending-store writes and no false 'device access upgrade requested' / 'auto-approved' security-audit warns on every Control UI page load. The upgrade audit warn moved into requirePairing so it fires only when a pairing request is materialized. Durable widening (broadened deviceAutoApprove.scopes) and key-mismatch repairs keep the existing lanes. * fix(gateway): revalidate the live paired grant in the reconnect fast path Address ClawSweeper P1: the narrowing decision now rereads the authoritative paired row as the last await before returning, so a concurrent revoke, key replacement, or grant reduction fails closed into the pairing lane instead of riding the pre-plan snapshot. Extract the pairing approval-plan resolver into connect-pairing-approval-plan.ts to keep the authorization owner under the 700-line lint boundary.
1 parent 84c469a commit 91cc37b

5 files changed

Lines changed: 359 additions & 271 deletions

File tree

‎docs/gateway/trusted-proxy-auth.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Trusted-proxy auth can optionally use the proxy identity as the approval boundar
194194
The default is `enabled: false`. When enabled, all of these rules apply:
195195

196196
1. The WebSocket must have authenticated through the `trusted-proxy` method with a non-empty user identity that passed `allowUsers` when an allowlist is configured. Token, password, Tailscale, and unauthenticated connections never use this policy.
197-
2. Only a new Control UI or WebChat browser device can be approved automatically. Any request for an existing device, including a scope upgrade, remains pending for manual approval with `openclaw devices approve <requestId>`.
197+
2. New Control UI or WebChat browser devices and scope upgrades from an existing device with the same paired public key resolve automatically. If the existing grant already covers the automatically approvable scopes, the session narrows to that grant without a pairing request or audit entry; otherwise, `deviceAutoApprove.scopes` can automatically approve the widened intersection. A public-key mismatch remains pending for manual approval with `openclaw devices approve <requestId>`.
198198
3. The device is approved with role `operator`. If the connect request includes scopes, the grant is the exact intersection of the requested scopes and `deviceAutoApprove.scopes`. If the request omits scopes, the configured list is granted; when that list is omitted, it defaults to `operator.read`, `operator.write`, and `operator.approvals`. The resulting grant is then additionally capped by the connection's [`x-openclaw-scopes`](#control-ui-pairing-behavior) proxy header when present, so a proxy that narrows a user's scopes also limits the **persistent** device grant, not just the session — a present-but-empty header yields no scopes. This cap applies even when the client omits its own scope list.
199199
4. `operator.admin` is allowed only through explicit listing in `deviceAutoApprove.scopes`. When listed, every proxy-authenticated user can request and automatically receive full admin on a new browser device; requests without scopes receive full admin automatically. `openclaw security audit` reports the CRITICAL `gateway.trusted_proxy_device_auto_approve_admin` finding, and the Gateway logs a warning once at startup. Prefer a targeted [`identityScopes`](#per-identity-scope-grants) admin grant when selected verified users need session admin without a persistent admin device grant.
200200

‎src/gateway/server.auth.trusted-proxy-device-autoapprove.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,60 @@ describe("trusted-proxy browser device auto-approval", () => {
465465
]);
466466
});
467467

468+
test("narrows same-key reconnects without a pairing round-trip", async () => {
469+
await writeGatewayAuthConfig({
470+
mode: "trusted-proxy",
471+
deviceAutoApprove: { enabled: true, scopes: ["operator.read", "operator.write"] },
472+
});
473+
const identityPath = deviceIdentityPath("trusted-proxy-noop-scope-upgrade");
474+
const identity = loadOrCreateDeviceIdentity({ path: identityPath });
475+
const warnings: string[] = [];
476+
477+
await withGatewayServer(async ({ port }) => {
478+
const initial = await connectBrowser({
479+
port,
480+
identityPath,
481+
scopes: ["operator.read", "operator.write"],
482+
});
483+
expect(initial.ok).toBe(true);
484+
485+
loggingState.rawConsole = {
486+
log: vi.fn(),
487+
info: vi.fn(),
488+
warn: vi.fn((message: string) => warnings.push(message)),
489+
error: vi.fn(),
490+
};
491+
setLoggerOverride({ level: "silent", consoleLevel: "warn" });
492+
try {
493+
const reconnect = await connectBrowser({
494+
port,
495+
identityPath,
496+
scopes: ["operator.read", "operator.write", "operator.admin", "operator.pairing"],
497+
});
498+
expect(reconnect.ok).toBe(true);
499+
expect((reconnect.payload as { auth?: { scopes?: string[] } })?.auth?.scopes).toEqual([
500+
"operator.read",
501+
"operator.write",
502+
]);
503+
} finally {
504+
loggingState.rawConsole = null;
505+
resetLogger();
506+
}
507+
});
508+
509+
expect(
510+
(await listDevicePairing()).pending.filter((entry) => entry.deviceId === identity.deviceId),
511+
).toEqual([]);
512+
expect((await getPairedDevice(identity.deviceId))?.approvedScopes).toEqual([
513+
"operator.read",
514+
"operator.write",
515+
]);
516+
expect(
517+
warnings.filter((message) => message.includes("device access upgrade requested")),
518+
).toEqual([]);
519+
expect(warnings.filter((message) => message.includes("auto-approved"))).toEqual([]);
520+
});
521+
468522
test("rejects a foreign-key connect for an already-paired deviceId", async () => {
469523
await writeGatewayAuthConfig({
470524
mode: "trusted-proxy",

0 commit comments

Comments
 (0)