#21664: fix(gateway): require re-pairing for legacy devices that lack scope metadata
gateway
size: XS
Cluster:
Device Pairing and Gateway Fixes
## Summary
- Legacy-paired devices (paired before role/scope tracking) bypass all scope and role validation because the check is inside `if (!hasLegacyPairedMetadata)`
- This allows legacy devices to self-declare any scopes including `operator.admin` without re-approval
- Fix: require re-pairing approval when a legacy device connects, so the user must approve the requested scopes
## Test plan
- Verify legacy devices (with `paired.roles === undefined && paired.scopes === undefined`) now trigger re-pairing
- Verify non-legacy devices continue to work as before
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Fixes critical security vulnerability where legacy devices (paired before role/scope tracking was implemented) could bypass all scope and role validation. Previously, when `paired.roles === undefined && paired.scopes === undefined`, the validation logic was completely skipped, allowing these devices to self-declare any scopes including privileged ones like `operator.admin`.
The fix adds an `else` branch that unconditionally requires re-pairing approval for legacy devices. This ensures users must explicitly approve the requested roles and scopes before the device can connect, establishing proper metadata for all future connections.
Also includes minor import reordering to match project style guidelines (type imports grouped at top).
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge - it closes a critical security hole without breaking existing functionality
- The fix correctly addresses the vulnerability by adding an unconditional re-pairing requirement for legacy devices. The logic is straightforward and matches the existing pattern for scope upgrades. Import reordering is style-only and follows project conventions. No breaking changes for non-legacy devices.
- No files require special attention
<sub>Last reviewed commit: c72a6f9</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#22312: Fix legacy paired metadata handling for reconnect scope compatibility
by AIflow-Labs · 2026-02-21
87.1%
#23690: fix(gateway): subagent sessions fail with pairing required on loopb...
by yinghaosang · 2026-02-22
85.5%
#22280: fix(gateway): silently auto-approve local paired-device scope upgrades
by abhishekp76 · 2026-02-21
85.3%
#23708: fix(gateway): auto-approve scope upgrades for loopback clients
by widingmarcus-cyber · 2026-02-22
84.6%
#21666: fix(gateway): restrict auto-paired device scopes to safe defaults
by AI-Reviewer-QS · 2026-02-20
84.0%
#17425: fix(gateway): auto-approve scope/role upgrades for already-paired d...
by sauerdaniel · 2026-02-15
83.2%
#16827: fix: allow device tokens with empty scopes to accept requested scopes
by MisterGuy420 · 2026-02-15
83.1%
#22712: fix(gateway): auto-approve all device pairing for localhost connect...
by NewdlDewdl · 2026-02-21
81.9%
#22583: fix(gateway): add operator.write to scope hierarchy (#22574)
by lailoo · 2026-02-21
81.6%
#22253: fix: auto-approve local loopback pairing for role/scope upgrades
by cjpraia · 2026-02-20
81.6%