@@ -42,10 +42,7 @@ import {
4242import { issueGatewayConnectDeviceTokens } from "./connect-device-tokens.js" ;
4343import { authorizeExistingGatewayDevice } from "./connect-existing-device.js" ;
4444import { startGatewayNodePairingSshApproval } from "./connect-node-pairing-ssh.js" ;
45- import {
46- resolvePairingApprovalPlan ,
47- resolveTrustedProxyDeviceAutoApproveScopes ,
48- } from "./connect-pairing-approval-plan.js" ;
45+ import { resolvePairingApprovalPlan } from "./connect-pairing-approval-plan.js" ;
4946import type {
5047 AuthenticatedGatewayConnect ,
5148 DeviceAuthorizedGatewayConnect ,
@@ -86,18 +83,29 @@ export async function authorizeGatewayConnectDevice(
8683 skipLocalBackendSelfPairing,
8784 controlUiPairingKind,
8885 } = state ;
89- const roleConfiguredHumanOperator = role === "operator" && Boolean ( configSnapshot . gateway ?. roles ) ;
90- const sharedSecretOwner = authMethod === "token" || authMethod === "password" ;
91- if ( roleConfiguredHumanOperator && ! sharedSecretOwner && ! authResult . user ?. trim ( ) ) {
92- const message = "operator role policies require a verified user identity" ;
86+ const failPairingHandshake = ( params : {
87+ message : string ;
88+ details ?: ReturnType < typeof buildPairingConnectErrorDetails > ;
89+ closeCause ?: { cause : string ; meta : Record < string , unknown > } ;
90+ closeReason ?: string ;
91+ } ) => {
92+ const { message, details, closeCause, closeReason } = params ;
9393 setHandshakeState ( "failed" ) ;
94+ if ( closeCause ) {
95+ setCloseCause ( closeCause . cause , closeCause . meta ) ;
96+ }
9497 send ( {
9598 type : "res" ,
9699 id : frame . id ,
97100 ok : false ,
98- error : errorShape ( ErrorCodes . NOT_PAIRED , message ) ,
101+ error : errorShape ( ErrorCodes . NOT_PAIRED , message , details ? { details } : undefined ) ,
99102 } ) ;
100- close ( 1008 , truncateCloseReason ( message ) ) ;
103+ close ( 1008 , truncateCloseReason ( closeReason ?? message ) ) ;
104+ } ;
105+ const roleConfiguredHumanOperator = role === "operator" && Boolean ( configSnapshot . gateway ?. roles ) ;
106+ const sharedSecretOwner = authMethod === "token" || authMethod === "password" ;
107+ if ( roleConfiguredHumanOperator && ! sharedSecretOwner && ! authResult . user ?. trim ( ) ) {
108+ failPairingHandshake ( { message : "operator role policies require a verified user identity" } ) ;
101109 return undefined ;
102110 }
103111 let hasServerApprovedDeviceTokenBaseline = false ;
@@ -143,19 +151,14 @@ export async function authorizeGatewayConnectDevice(
143151 const pairingStateAllowsRequestedAccess = (
144152 pairedCandidate : Awaited < ReturnType < typeof getPairedDevice > > ,
145153 requestedScopes = scopes ,
146- ) : boolean => {
147- if ( ! pairedCandidate || pairedCandidate . publicKey !== devicePublicKey ) {
148- return false ;
149- }
150- if ( ! hasEffectivePairedDeviceRole ( pairedCandidate , role ) ) {
151- return false ;
152- }
153- return roleScopesAllow ( {
154+ ) : boolean =>
155+ pairedCandidate ?. publicKey === devicePublicKey &&
156+ hasEffectivePairedDeviceRole ( pairedCandidate , role ) &&
157+ roleScopesAllow ( {
154158 role,
155159 requestedScopes,
156160 allowedScopes : resolvePairedAccessScopes ( pairedCandidate ) ,
157161 } ) ;
158- } ;
159162 const plan = await resolvePairingApprovalPlan ( {
160163 reason,
161164 existingPairedDevice,
@@ -168,25 +171,15 @@ export async function authorizeGatewayConnectDevice(
168171 deviceId : device . id ,
169172 devicePublicKey,
170173 scopes,
174+ hasRequestedScopes,
175+ connectionScopeCap : ( capped ) =>
176+ applyConnectionScopeCap ( { scopes : capped , upgradeReq : context . handler . upgradeReq } ) ,
171177 } ) ;
172- const trustedProxyAutoApproveConfig =
173- configSnapshot . gateway ?. auth ?. trustedProxy ?. deviceAutoApprove ;
174- const trustedProxyUser = authResult . user ?. trim ( ) ;
175- const trustedProxyAutoApproveScopes = plan . allowTrustedProxyDeviceAutoApproval
176- ? applyConnectionScopeCap ( {
177- scopes : resolveTrustedProxyDeviceAutoApproveScopes ( {
178- requestedScopes : scopes ,
179- hasRequestedScopes,
180- configuredScopes : trustedProxyAutoApproveConfig ?. scopes ,
181- } ) ,
182- upgradeReq : context . handler . upgradeReq ,
183- } )
184- : null ;
185178 // Same-key reconnects reuse paired grants without pairing or false upgrade audits.
186179 if (
187180 reason === "scope-upgrade" &&
188181 plan . isTrustedProxySameKeyUpgrade &&
189- trustedProxyAutoApproveScopes !== null
182+ plan . trustedProxyAutoApproveScopes !== null
190183 ) {
191184 // Authority is the live row, not the pre-plan snapshot: a revoke, key
192185 // replacement, or grant reduction landing during the plan await must
@@ -195,11 +188,11 @@ export async function authorizeGatewayConnectDevice(
195188 const livePaired = await getPairedDevice ( device . id ) ;
196189 if (
197190 livePaired &&
198- pairingStateAllowsRequestedAccess ( livePaired , trustedProxyAutoApproveScopes )
191+ pairingStateAllowsRequestedAccess ( livePaired , plan . trustedProxyAutoApproveScopes )
199192 ) {
200193 const livePairedScopes = resolvePairedAccessScopes ( livePaired ) ;
201194 scopes = normalizeSortedUniqueTrimmedStringList (
202- [ ...scopes , ...trustedProxyAutoApproveScopes ] . filter ( ( scope ) =>
195+ [ ...scopes , ...plan . trustedProxyAutoApproveScopes ] . filter ( ( scope ) =>
203196 roleScopesAllow ( { role, requestedScopes : [ scope ] , allowedScopes : livePairedScopes } ) ,
204197 ) ,
205198 ) ;
@@ -229,7 +222,7 @@ export async function authorizeGatewayConnectDevice(
229222 } ) ;
230223 const trustedProxyApprovalScopes =
231224 pairing . request . isRepair !== true || plan . isTrustedProxySameKeyUpgrade
232- ? trustedProxyAutoApproveScopes
225+ ? plan . trustedProxyAutoApproveScopes
233226 : null ;
234227 const requestContext = buildRequestContext ( ) ;
235228 // A replacement request obsoletes older pending requestIds; tell approval
@@ -305,9 +298,9 @@ export async function authorizeGatewayConnectDevice(
305298 if ( plan . bootstrapApprovalProfile ) {
306299 handoffBootstrapProfile = plan . bootstrapApprovalProfile ;
307300 }
308- if ( trustedProxyApprovalScopes !== null && trustedProxyUser ) {
301+ if ( trustedProxyApprovalScopes !== null && plan . trustedProxyUser ) {
309302 logGateway . warn (
310- `security audit: trusted-proxy browser device auto-approved user=${ formatForLog ( trustedProxyUser ) } device=${ formatForLog ( approved . device . deviceId . slice ( 0 , 12 ) ) } scopes=${ formatAuditList ( scopes ) } ` ,
303+ `security audit: trusted-proxy browser device auto-approved user=${ formatForLog ( plan . trustedProxyUser ) } device=${ formatForLog ( approved . device . deviceId . slice ( 0 , 12 ) ) } scopes=${ formatAuditList ( scopes ) } ` ,
311304 ) ;
312305 } else {
313306 logGateway . info (
@@ -401,46 +394,34 @@ export async function authorizeGatewayConnectDevice(
401394 // pairing-required behavior pauses the client reconnect loop.
402395 const retryWhileDetachedApprovalPending =
403396 retryAfterBootstrapPairingApproval || sshVerifyStarted ;
404- const pairingErrorDetails = buildPairingConnectErrorDetails ( {
405- reason,
406- requestId : recoveryRequestId ,
407- ...( retryWhileDetachedApprovalPending
408- ? {
409- recommendedNextStep : "wait_then_retry" ,
410- retryable : true ,
411- pauseReconnect : false ,
412- }
413- : { } ) ,
414- deviceId : device . id ,
415- requestedRole : role ,
416- requestedScopes : scopes ,
417- ...( approvedRoles . length > 0 ? { approvedRoles } : { } ) ,
418- ...( approvedScopes . length > 0 ? { approvedScopes } : { } ) ,
419- } ) ;
420- const pairingErrorMessage = buildPairingConnectErrorMessage ( reason ) ;
421- setHandshakeState ( "failed" ) ;
422- setCloseCause ( "pairing-required" , {
423- deviceId : device . id ,
424- ...( recoveryRequestId ? { requestId : recoveryRequestId } : { } ) ,
425- reason,
426- } ) ;
427- send ( {
428- type : "res" ,
429- id : frame . id ,
430- ok : false ,
431- error : errorShape ( ErrorCodes . NOT_PAIRED , pairingErrorMessage , {
432- details : pairingErrorDetails ,
397+ failPairingHandshake ( {
398+ message : buildPairingConnectErrorMessage ( reason ) ,
399+ details : buildPairingConnectErrorDetails ( {
400+ reason,
401+ requestId : recoveryRequestId ,
402+ ...( retryWhileDetachedApprovalPending
403+ ? {
404+ recommendedNextStep : "wait_then_retry" ,
405+ retryable : true ,
406+ pauseReconnect : false ,
407+ }
408+ : { } ) ,
409+ deviceId : device . id ,
410+ requestedRole : role ,
411+ requestedScopes : scopes ,
412+ ...( approvedRoles . length > 0 ? { approvedRoles } : { } ) ,
413+ ...( approvedScopes . length > 0 ? { approvedScopes } : { } ) ,
433414 } ) ,
434- } ) ;
435- close (
436- 1008 ,
437- truncateCloseReason (
438- buildPairingConnectCloseReason ( {
415+ closeCause : {
416+ cause : "pairing-required" ,
417+ meta : {
418+ deviceId : device . id ,
419+ ... ( recoveryRequestId ? { requestId : recoveryRequestId } : { } ) ,
439420 reason,
440- requestId : recoveryRequestId ,
441- } ) ,
442- ) ,
443- ) ;
421+ } ,
422+ } ,
423+ closeReason : buildPairingConnectCloseReason ( { reason , requestId : recoveryRequestId } ) ,
424+ } ) ;
444425 return false ;
445426 }
446427 return true ;
@@ -517,14 +498,7 @@ export async function authorizeGatewayConnectDevice(
517498 ( ! pairedBrowserOrigin || ! browserCopilotOrigin || pairedBrowserOrigin !== browserCopilotOrigin ) ;
518499 if ( browserCopilotIdentityMismatch || browserCopilotOriginMismatch ) {
519500 const message = "browser copilot requires a dedicated paired device identity" ;
520- setHandshakeState ( "failed" ) ;
521- send ( {
522- type : "res" ,
523- id : frame . id ,
524- ok : false ,
525- error : errorShape ( ErrorCodes . NOT_PAIRED , message ) ,
526- } ) ;
527- close ( 1008 , truncateCloseReason ( message ) ) ;
501+ failPairingHandshake ( { message } ) ;
528502 return undefined ;
529503 }
530504
0 commit comments