Repository navigation
Device pairing: bind setup codes to node approvals - #46794
vincentkoc wants to merge 3 commits into
Conversation
Greptile SummaryThis PR fixes a security gap in device-pairing where setup codes were issued without binding them to the intended node role and scope set, allowing a verification request with a wider role or scope to succeed under the same code. The fix records Key changes:
Confidence Score: 4/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/infra/device-bootstrap.ts
Line: 125-133
Comment:
**Scope comparison is order-sensitive and relies on normalizer internals**
The element-by-element index comparison is safe *today* because `normalizeDeviceAuthScopes` returns a sorted, deduplicated array for both sides. However, this is an implicit contract: if the normalization function ever stops sorting (e.g. changes to preserve insertion order), the comparison silently breaks without any compile-time or test-time signal.
A more robust alternative is to compare using the same `Set`-based approach that `normalizeDeviceAuthScopes` already uses internally, or to at least leave a comment tying the comparison to the sort guarantee:
```ts
// Both arrays are sorted+deduplicated by normalizeDeviceAuthScopes,
// so index equality is equivalent to set equality here.
if (
allowedScopes.length !== requestedScopes.length ||
allowedScopes.some((value, index) => value !== requestedScopes[index])
) {
return { ok: false, reason: "bootstrap_token_invalid" };
}
```
Alternatively, a set-based check avoids the dependency entirely:
```ts
const allowedSet = new Set(allowedScopes);
const requestedSet = new Set(requestedScopes);
if (
allowedSet.size !== requestedSet.size ||
[...requestedSet].some((s) => !allowedSet.has(s))
) {
return { ok: false, reason: "bootstrap_token_invalid" };
}
```
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: src/infra/device-bootstrap.test.ts
Line: 221-259
Comment:
**Missing happy-path test for constrained token verification**
The PR adds two negative tests (role mismatch → reject, scope mismatch → reject) but never adds a corresponding positive test: issue a token with `role: "node", scopes: []` and then verify it with the *same* role and scopes, expecting `{ ok: true }`.
Without this test, a regression that accidentally inverts or short-circuits the `allowedRoles` / `allowedScopes` check (e.g. always returning `{ ok: false }` for constrained tokens) would not be caught by the test suite. The negative tests would still pass even if matching tokens were never actually accepted.
Consider adding a test that issues a role+scope-constrained token and asserts `verifyDeviceBootstrapToken` resolves to `{ ok: true }` when the same role and scopes are presented in the verification call.
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: 7b3630e |
| if (Array.isArray(entry.scopes)) { | ||
| const allowedScopes = normalizeDeviceAuthScopes(entry.scopes); | ||
| if ( | ||
| allowedScopes.length !== requestedScopes.length || | ||
| allowedScopes.some((value, index) => value !== requestedScopes[index]) | ||
| ) { | ||
| return { ok: false, reason: "bootstrap_token_invalid" }; | ||
| } | ||
| } |
There was a problem hiding this comment.
Scope comparison is order-sensitive and relies on normalizer internals
The element-by-element index comparison is safe today because normalizeDeviceAuthScopes returns a sorted, deduplicated array for both sides. However, this is an implicit contract: if the normalization function ever stops sorting (e.g. changes to preserve insertion order), the comparison silently breaks without any compile-time or test-time signal.
A more robust alternative is to compare using the same Set-based approach that normalizeDeviceAuthScopes already uses internally, or to at least leave a comment tying the comparison to the sort guarantee:
// Both arrays are sorted+deduplicated by normalizeDeviceAuthScopes,
// so index equality is equivalent to set equality here.
if (
allowedScopes.length !== requestedScopes.length ||
allowedScopes.some((value, index) => value !== requestedScopes[index])
) {
return { ok: false, reason: "bootstrap_token_invalid" };
}Alternatively, a set-based check avoids the dependency entirely:
const allowedSet = new Set(allowedScopes);
const requestedSet = new Set(requestedScopes);
if (
allowedSet.size !== requestedSet.size ||
[...requestedSet].some((s) => !allowedSet.has(s))
) {
return { ok: false, reason: "bootstrap_token_invalid" };
}Prompt To Fix With AI
This is a comment left during a code review.
Path: src/infra/device-bootstrap.ts
Line: 125-133
Comment:
**Scope comparison is order-sensitive and relies on normalizer internals**
The element-by-element index comparison is safe *today* because `normalizeDeviceAuthScopes` returns a sorted, deduplicated array for both sides. However, this is an implicit contract: if the normalization function ever stops sorting (e.g. changes to preserve insertion order), the comparison silently breaks without any compile-time or test-time signal.
A more robust alternative is to compare using the same `Set`-based approach that `normalizeDeviceAuthScopes` already uses internally, or to at least leave a comment tying the comparison to the sort guarantee:
```ts
// Both arrays are sorted+deduplicated by normalizeDeviceAuthScopes,
// so index equality is equivalent to set equality here.
if (
allowedScopes.length !== requestedScopes.length ||
allowedScopes.some((value, index) => value !== requestedScopes[index])
) {
return { ok: false, reason: "bootstrap_token_invalid" };
}
```
Alternatively, a set-based check avoids the dependency entirely:
```ts
const allowedSet = new Set(allowedScopes);
const requestedSet = new Set(requestedScopes);
if (
allowedSet.size !== requestedSet.size ||
[...requestedSet].some((s) => !allowedSet.has(s))
) {
return { ok: false, reason: "bootstrap_token_invalid" };
}
```
How can I resolve this? If you propose a fix, please make it concise.| it("rejects a role that does not match the issued pairing profile", async () => { | ||
| const baseDir = await createTempDir(); | ||
| const issued = await issueDeviceBootstrapToken({ | ||
| baseDir, | ||
| role: "node", | ||
| scopes: [], | ||
| }); | ||
|
|
||
| await expect( | ||
| verifyDeviceBootstrapToken({ | ||
| token: issued.token, | ||
| deviceId: "device-123", | ||
| publicKey: "public-key-123", | ||
| role: "operator", | ||
| scopes: [], | ||
| baseDir, | ||
| }), | ||
| ).resolves.toEqual({ ok: false, reason: "bootstrap_token_invalid" }); | ||
| }); | ||
|
|
||
| it("rejects scopes that do not match the issued pairing profile", async () => { | ||
| const baseDir = await createTempDir(); | ||
| const issued = await issueDeviceBootstrapToken({ | ||
| baseDir, | ||
| role: "node", | ||
| scopes: [], | ||
| }); | ||
|
|
||
| await expect( | ||
| verifyDeviceBootstrapToken({ | ||
| token: issued.token, | ||
| deviceId: "device-123", | ||
| publicKey: "public-key-123", | ||
| role: "node", | ||
| scopes: ["operator.admin"], | ||
| baseDir, | ||
| }), | ||
| ).resolves.toEqual({ ok: false, reason: "bootstrap_token_invalid" }); | ||
| }); |
There was a problem hiding this comment.
Missing happy-path test for constrained token verification
The PR adds two negative tests (role mismatch → reject, scope mismatch → reject) but never adds a corresponding positive test: issue a token with role: "node", scopes: [] and then verify it with the same role and scopes, expecting { ok: true }.
Without this test, a regression that accidentally inverts or short-circuits the allowedRoles / allowedScopes check (e.g. always returning { ok: false } for constrained tokens) would not be caught by the test suite. The negative tests would still pass even if matching tokens were never actually accepted.
Consider adding a test that issues a role+scope-constrained token and asserts verifyDeviceBootstrapToken resolves to { ok: true } when the same role and scopes are presented in the verification call.
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/infra/device-bootstrap.test.ts
Line: 221-259
Comment:
**Missing happy-path test for constrained token verification**
The PR adds two negative tests (role mismatch → reject, scope mismatch → reject) but never adds a corresponding positive test: issue a token with `role: "node", scopes: []` and then verify it with the *same* role and scopes, expecting `{ ok: true }`.
Without this test, a regression that accidentally inverts or short-circuits the `allowedRoles` / `allowedScopes` check (e.g. always returning `{ ok: false }` for constrained tokens) would not be caught by the test suite. The negative tests would still pass even if matching tokens were never actually accepted.
Consider adding a test that issues a role+scope-constrained token and asserts `verifyDeviceBootstrapToken` resolves to `{ ok: true }` when the same role and scopes are presented in the verification call.
How can I resolve this? If you propose a fix, please make it concise.|
Addressed the follow-up notes.
Verification:
|
|
Codex review: found issues before merge. Reviewed July 5, 2026, 8:17 AM ET / 12:17 UTC. Summary PR surface: Source +34, Tests +76, Docs +1. Total +111 across 5 files. Reproducibility: yes. for the current merge blocker: source inspection shows current main uses PAIRING_SETUP_BOOTSTRAP_PROFILE and profile redemption state while this branch hard-codes node/empty scopes. I did not run a live device-pair session in this read-only review. Review metrics: 1 noteworthy metric.
Stored data model Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Risk before merge
Maintainer options:
Next step before merge
Maintainer decision needed
Security Review findings
Review detailsBest possible solution: Retire this branch in favor of the merged profile-based handoff work; any renewed fix should start from current main and preserve PAIRING_SETUP_BOOTSTRAP_PROFILE, pending/redeemed profile tracking, and the gateway/mobile handoff docs and tests. Do we have a high-confidence way to reproduce the issue? Yes for the current merge blocker: source inspection shows current main uses PAIRING_SETUP_BOOTSTRAP_PROFILE and profile redemption state while this branch hard-codes node/empty scopes. I did not run a live device-pair session in this read-only review. Is this the best way to solve the issue? No. The best fix is the current profile-based bootstrap contract on main, not merging this stale role/scope-only branch shape as-is. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 785ab7477982. Label changesLabel justifications:
Evidence reviewedPR surface: Source +34, Tests +76, Docs +1. Total +111 across 5 files. View PR surface stats
Security concerns:
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
|
ClawSweeper PR egg 🔥 Warming up: real-behavior proof passed; findings, security review, or rank-up moves are still in progress. Hatch commandComment Hatchability rules:
What is this egg doing here?
|
|
Closing as superseded by the current bootstrap-pairing hardening on
Those maintained changes cover the privilege-escalation concern more completely than this branch's older exact role/scope array comparison, including gateway handshake and client reconnect tests. There is no unique fix left to replay. Thanks @vincentkoc for identifying the missing bootstrap-profile binding. |
Summary
Change Type
User-visible / Behavior Changes
Verification
pnpm test -- src/infra/device-bootstrap.test.tsHuman Verification
Compatibility / Migration
Failure Recovery