From 855de407baca6ca7304d43c0efc68696d18392cc Mon Sep 17 00:00:00 2001 From: Rebecca Sliter <571084+rsliter@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:23:57 -0800 Subject: [PATCH] fix(openclaw): qualify TUI grants retained from CLI pairing Signed-off-by: Rebecca Sliter <571084+rsliter@users.noreply.github.com> --- .../launch-readiness-pairing-probe.test.ts | 22 ++++++++++- .../openclaw-pairing-qualification.test.ts | 10 ++--- .../openclaw-pairing-qualification.ts | 21 +++++----- .../openclaw-pairing-tui.test.ts | 38 +++++++++++++++++++ 4 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/lib/actions/sandbox/launch-readiness-pairing-probe.test.ts b/src/lib/actions/sandbox/launch-readiness-pairing-probe.test.ts index 15bd2fd1446..d909f6d78e1 100644 --- a/src/lib/actions/sandbox/launch-readiness-pairing-probe.test.ts +++ b/src/lib/actions/sandbox/launch-readiness-pairing-probe.test.ts @@ -118,9 +118,27 @@ it.each([ nextMode: "ui", finalMode: "ui", }, + { + profile: "TUI after CLI pairing", + clientId: "openclaw-tui", + initialMode: "ui", + scopes: [...TUI_SCOPES, "operator.pairing"], + tokenScopes: [...TUI_SCOPES, "operator.pairing"], + authScopes: TUI_SCOPES, + nextMode: "ui", + finalMode: "ui", + }, ])( "keeps a published $profile lease accepted until its pairing state changes (#13100, #13668)", - async ({ clientId, initialMode, scopes, tokenScopes, nextMode, finalMode }) => { + async ({ + clientId, + initialMode, + scopes, + tokenScopes, + authScopes = tokenScopes, + nextMode, + finalMode, + }) => { const fixture = createOpenClawPairingFixture(); try { const pairedPath = path.join(fixture.stateDirectory, "devices", "paired.json"); @@ -133,7 +151,7 @@ it.each([ fs.writeFileSync(pairedPath, JSON.stringify(paired)); const authPath = path.join(fixture.stateDirectory, "identity", "device-auth.json"); const auth = JSON.parse(fs.readFileSync(authPath, "utf8")); - auth.tokens.operator.scopes = [...tokenScopes]; + auth.tokens.operator.scopes = [...authScopes]; fs.writeFileSync(authPath, JSON.stringify(auth)); let published: LaunchReadinessLease | null = null; const deps: LaunchReadinessDeps = { diff --git a/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.test.ts b/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.test.ts index 1fde75c0d50..8ba494bff5e 100644 --- a/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.test.ts +++ b/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.test.ts @@ -513,14 +513,14 @@ describe("OpenClaw launch-readiness pairing qualification", () => { ], authScopes: ["operator.admin", "operator.read", "operator.write"], }, - { + ...[TUI_SCOPES, [...TUI_SCOPES, "operator.pairing"]].map((pairedScopes) => ({ clientId: "openclaw-tui", clientMode: "ui", - pairedScopes: TUI_SCOPES, - pairedTokenScopes: TUI_SCOPES, + pairedScopes, + pairedTokenScopes: pairedScopes, authScopes: TUI_SCOPES, - }, - ])("qualifies the exact persisted $clientId state", (profile) => { + })), + ])("qualifies persisted $clientId with paired scopes $pairedScopes", (profile) => { writeSqlitePairingState(stateDirectory, { deviceId, publicKey, privateKeyPem, ...profile }); expect(observe()).toMatchObject({ kind: "openclaw-pairing" }); expect(observeSettlement()).toMatchObject({ state: "settled" }); diff --git a/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.ts b/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.ts index 83b6bf48b2c..6e9e662d873 100644 --- a/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.ts +++ b/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-qualification.ts @@ -345,6 +345,7 @@ ADMIN_REQUEST_SCOPES = ['operator.admin', 'operator.pairing', 'operator.write'] ADMIN_PAIRED_TOKEN_SCOPES = ['operator.admin', 'operator.pairing', 'operator.read', 'operator.write'] ADMIN_AUTH_TOKEN_SCOPES = ['operator.admin', 'operator.read', 'operator.write'] TUI_SCOPES = ['operator.admin', 'operator.approvals', 'operator.read', 'operator.write'] +TUI_SCOPES_WITH_PAIRING = [*TUI_SCOPES, 'operator.pairing'] ALLOW_CANONICAL_PENDING = ${mode === "ordinary-settlement" || mode === "repair-settlement" ? "True" : "False"} ORDINARY_SETTLEMENT = ${mode === "ordinary-settlement" ? "True" : "False"} REPORT_CANONICAL_PENDING = ${mode === "repair-settlement" ? "True" : "False"} @@ -920,13 +921,15 @@ try: and exact_string_set(paired_operator.get('scopes'), ADMIN_PAIRED_TOKEN_SCOPES) and exact_string_set(auth_operator.get('scopes'), ADMIN_AUTH_TOKEN_SCOPES) ) - # The native TUI reuses the local device identity and records its own - # already-approved grants. Observe that exact settled profile without - # approving a pending request or changing either credential view. - tui_settled = tui_client and all( - exact_string_set(view, TUI_SCOPES) for view in ( - paired_device.get('scopes'), paired_device.get('approvedScopes'), - paired_operator.get('scopes'), auth_operator.get('scopes'), + # After CLI pairing, the native TUI retains operator.pairing in all three + # server-side records. Its client credential still records only the TUI grant. + tui_server_scopes = ( + TUI_SCOPES_WITH_PAIRING if exact_string_set(paired_device.get('scopes'), TUI_SCOPES_WITH_PAIRING) + else TUI_SCOPES + ) + tui_settled = tui_client and exact_string_set(auth_operator.get('scopes'), TUI_SCOPES) and all( + exact_string_set(view, tui_server_scopes) for view in ( + paired_device.get('scopes'), paired_device.get('approvedScopes'), paired_operator.get('scopes'), ) ) settled = baseline_settled or admin_settled or tui_settled @@ -981,8 +984,8 @@ try: : "if not settled:\n reject()" } - projected_request_scopes = TUI_SCOPES if tui_settled else ADMIN_REQUEST_SCOPES if admin_settled else (TOKEN_SCOPES if explicit_read_grant else REQUEST_SCOPES) - projected_paired_token_scopes = TUI_SCOPES if tui_settled else ADMIN_PAIRED_TOKEN_SCOPES if admin_settled else TOKEN_SCOPES + projected_request_scopes = tui_server_scopes if tui_settled else ADMIN_REQUEST_SCOPES if admin_settled else (TOKEN_SCOPES if explicit_read_grant else REQUEST_SCOPES) + projected_paired_token_scopes = tui_server_scopes if tui_settled else ADMIN_PAIRED_TOKEN_SCOPES if admin_settled else TOKEN_SCOPES projected_auth_token_scopes = TUI_SCOPES if tui_settled else ADMIN_AUTH_TOKEN_SCOPES if admin_settled else TOKEN_SCOPES projection = { 'deviceIdentitySha256': device_identity_sha256, diff --git a/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-tui.test.ts b/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-tui.test.ts index 101340ebe07..c9fd559e42d 100644 --- a/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-tui.test.ts +++ b/src/lib/actions/sandbox/launch-readiness/openclaw-pairing-tui.test.ts @@ -84,6 +84,44 @@ describe("readiness after a native OpenClaw TUI session", () => { expect([fs.readFileSync(pairedPath), fs.readFileSync(authPath)]).toEqual(before); }); + function retainPairing() { + const device = paired[fixture.deviceId]!; + device.scopes.push("operator.pairing"); + device.approvedScopes.push("operator.pairing"); + device.tokens.operator.scopes.push("operator.pairing"); + return device; + } + + it("binds retained CLI pairing to the TUI receipt", () => { + const fresh = observe(); + const device = retainPairing(); + writeState(); + const retained = observe(); + expect(retained.deviceIdentitySha256).toBe(fresh.deviceIdentitySha256); + expect(retained.pairingStateSha256).not.toBe(fresh.pairingStateSha256); + device.scopes.reverse(); + writeState(); + expect(observe()).toEqual(retained); + }); + + it.each(["scopes", "approvedScopes", "token"] as const)( + "rejects a retained grant missing pairing from the server %s", + (key) => { + const device = retainPairing(); + const scopes = key === "token" ? device.tokens.operator.scopes : device[key]; + scopes.splice(scopes.indexOf("operator.pairing"), 1); + writeState(); + expect(() => observe()).toThrow(OpenClawPairingQualificationError); + }, + ); + + it("rejects retained pairing in the client grant", () => { + retainPairing(); + auth.tokens.operator.scopes.push("operator.pairing"); + writeState(); + expect(() => observe()).toThrow(OpenClawPairingQualificationError); + }); + it("binds the TUI grant in a stable digest distinct from the same CLI identity", () => { const tuiReceipt = observe(); const device = paired[fixture.deviceId]!;