Skip to content

Commit f749de4

Browse files
authored
fix(device-pairing): don't churn requestId on subset re-requests (#98145)
* fix(device-pairing): don't churn requestId on subset re-requests A reconnect that re-requested a subset of an already-pending device pairing request still superseded it with a fresh requestId. This is the root cause of the "unknown requestId" failures during device approval: 1. A TUI connect files a broad scope-upgrade request; the owner copies its id from `openclaw devices list`. 2. `openclaw devices approve <id>` reconnects as a CLI probe that only needs `operator.pairing` — a subset of the pending scopes. 3. That subset re-request superseded the pending request with a new id, so the originally-listed id no longer existed and approve failed. Refresh the existing request in place when the incoming request only asks for roles/scopes a single pending request (same device key + role) already covers. Escalations that request *more* than the pending request still supersede with a fresh id, so the requestId stays bound to at least the scope snapshot the owner saw (the existing security-motivated behavior and its test are preserved). AI-assisted (Claude Code). * fix(device-pairing): align subset pairing with scope coverage
1 parent e172447 commit f749de4

2 files changed

Lines changed: 126 additions & 1 deletion

File tree

‎src/infra/device-pairing-churn.test.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,86 @@ describe("device pairing requestId churn", () => {
115115
expectScopesToContain(pending.scopes, ["operator.pairing", "operator.read"]);
116116
});
117117

118+
test("keeps the requestId when a reconnect re-requests a subset of a pending request", async () => {
119+
const baseDir = await makeDevicePairingDir();
120+
121+
// A TUI connect files a broad scope-upgrade request the owner sees in
122+
// `devices list`.
123+
const broad = await requestDevicePairing(
124+
{
125+
deviceId: DEVICE_ID,
126+
publicKey: PUBLIC_KEY,
127+
role: "operator",
128+
scopes: ["operator.admin", "operator.pairing", "operator.read", "operator.write"],
129+
},
130+
baseDir,
131+
);
132+
expect(broad.created).toBe(true);
133+
134+
// `openclaw devices approve <id>` reconnects as a CLI probe that only needs
135+
// `operator.pairing`. That subset re-request must not mint a new id, or the
136+
// id the owner copied would fail to approve with "unknown requestId".
137+
const subsetReconnect = await requestDevicePairing(
138+
{
139+
deviceId: DEVICE_ID,
140+
publicKey: PUBLIC_KEY,
141+
role: "operator",
142+
scopes: ["operator.pairing"],
143+
},
144+
baseDir,
145+
);
146+
147+
expect(subsetReconnect.created).toBe(false);
148+
expect(subsetReconnect.request.requestId).toBe(broad.request.requestId);
149+
// The broader pending scopes the owner already saw are preserved.
150+
expectScopesToContain(subsetReconnect.request.scopes, [
151+
"operator.admin",
152+
"operator.pairing",
153+
"operator.read",
154+
"operator.write",
155+
]);
156+
157+
const pairingList = await listDevicePairing(baseDir);
158+
expect(pairingList.pending).toHaveLength(1);
159+
expect(pairingList.pending[0]?.requestId).toBe(broad.request.requestId);
160+
161+
// The originally-listed id remains approvable.
162+
const approved = await approveDevicePairing(
163+
broad.request.requestId,
164+
{ callerScopes: ["operator.admin"] },
165+
baseDir,
166+
);
167+
expect(approved?.status).toBe("approved");
168+
});
169+
170+
test("keeps the requestId when operator.admin covers a pairing reconnect", async () => {
171+
const baseDir = await makeDevicePairingDir();
172+
173+
const adminRequest = await requestDevicePairing(
174+
{
175+
deviceId: DEVICE_ID,
176+
publicKey: PUBLIC_KEY,
177+
role: "operator",
178+
scopes: ["operator.admin"],
179+
},
180+
baseDir,
181+
);
182+
183+
const pairingReconnect = await requestDevicePairing(
184+
{
185+
deviceId: DEVICE_ID,
186+
publicKey: PUBLIC_KEY,
187+
role: "operator",
188+
scopes: ["operator.pairing"],
189+
},
190+
baseDir,
191+
);
192+
193+
expect(pairingReconnect.created).toBe(false);
194+
expect(pairingReconnect.request.requestId).toBe(adminRequest.request.requestId);
195+
expect(pairingReconnect.request.scopes).toEqual(["operator.admin"]);
196+
});
197+
118198
test("supports cron-first progressive operator escalation from read to pairing to admin", async () => {
119199
const baseDir = await makeDevicePairingDir();
120200

‎src/infra/device-pairing.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,49 @@ function samePendingApprovalSnapshot(
351351
return true;
352352
}
353353

354+
function isStringSubset(subset: readonly string[], superset: readonly string[]): boolean {
355+
const supersetSet = new Set(superset);
356+
for (const value of subset) {
357+
if (!supersetSet.has(value)) {
358+
return false;
359+
}
360+
}
361+
return true;
362+
}
363+
364+
// True when the incoming request only asks for roles/scopes a single existing pending
365+
// request (same key + role) already covers. Such subset re-requests refresh in place so
366+
// the owner's listed requestId stays valid; escalations still supersede with a fresh id.
367+
function incomingApprovalCoveredByExisting(
368+
existing: DevicePairingPendingRequest,
369+
incoming: Omit<DevicePairingPendingRequest, "requestId" | "ts" | "isRepair">,
370+
): boolean {
371+
if (existing.publicKey !== incoming.publicKey) {
372+
return false;
373+
}
374+
if (normalizeRole(existing.role) !== normalizeRole(incoming.role)) {
375+
return false;
376+
}
377+
const incomingRoles = resolveRequestedRoles(incoming);
378+
if (!isStringSubset(incomingRoles, resolveRequestedRoles(existing))) {
379+
return false;
380+
}
381+
const existingScopes = resolveRequestedScopes(existing);
382+
for (const scope of resolveRequestedScopes(incoming)) {
383+
const covered = incomingRoles.some((role) =>
384+
roleScopesAllow({
385+
role,
386+
requestedScopes: [scope],
387+
allowedScopes: existingScopes,
388+
}),
389+
);
390+
if (!covered) {
391+
return false;
392+
}
393+
}
394+
return true;
395+
}
396+
354397
function refreshPendingDevicePairingRequest(
355398
existing: DevicePairingPendingRequest,
356399
incoming: Omit<DevicePairingPendingRequest, "requestId" | "ts" | "isRepair">,
@@ -618,7 +661,9 @@ export async function requestDevicePairing(
618661
pendingById: state.pendingById,
619662
existing: pendingForDevice,
620663
incoming: req,
621-
canRefreshSingle: (existing, incoming) => samePendingApprovalSnapshot(existing, incoming),
664+
canRefreshSingle: (existing, incoming) =>
665+
samePendingApprovalSnapshot(existing, incoming) ||
666+
incomingApprovalCoveredByExisting(existing, incoming),
622667
refreshSingle: (existing, incoming) =>
623668
refreshPendingDevicePairingRequest(existing, incoming, isRepair),
624669
buildReplacement: ({ existing, incoming }) => {

0 commit comments

Comments
 (0)