@@ -157,9 +157,9 @@ import * as ThreadSearch from "./orchestration-v2/ThreadSearch.ts";
157157import * as OrchestrationEventStore from "./persistence/Services/OrchestrationEventStore.ts" ;
158158import { userFacingDispatchErrorMessage } from "./orchestration-v2/UserFacingErrors.ts" ;
159159import {
160- observeRpcEffect as instrumentRpcEffect ,
161- observeRpcStream as instrumentRpcStream ,
162- observeRpcStreamEffect as instrumentRpcStreamEffect ,
160+ observeRpcEffect ,
161+ observeRpcStream ,
162+ observeRpcStreamEffect ,
163163} from "./observability/RpcInstrumentation.ts" ;
164164import * as ProviderRegistry from "./provider/Services/ProviderRegistry.ts" ;
165165import * as ProviderInstanceRegistry from "./provider/Services/ProviderInstanceRegistry.ts" ;
@@ -207,7 +207,11 @@ import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
207207import * as RemoteOpenTargets from "./environment/RemoteOpenTargets.ts" ;
208208import * as BackgroundPolicy from "./background/BackgroundPolicy.ts" ;
209209import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts" ;
210- import { requiredScopeForRpcMethod , requiredScopeForDeviceList } from "./auth/RpcAuthorization.ts" ;
210+ import {
211+ requiredScopeForDeviceList ,
212+ rpcAuthorizationError ,
213+ rpcScopeAuthorizationLayer ,
214+ } from "./auth/RpcAuthorization.ts" ;
211215import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts" ;
212216import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts" ;
213217import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts" ;
@@ -1309,25 +1313,15 @@ const makeWsRpcLayer = (
13091313 const processResourceMonitor = yield * ProcessResourceMonitor . ProcessResourceMonitor ;
13101314 const resourceTelemetry = yield * ResourceTelemetry . ResourceTelemetry ;
13111315 const relayClient = yield * RelayClient . RelayClient ;
1312- const authorizationError = ( requiredScope : AuthEnvironmentScope ) =>
1313- new EnvironmentAuthorizationError ( {
1314- message : `The authenticated token is missing required scope: ${ requiredScope } .` ,
1315- requiredScope,
1316- } ) ;
1316+ // RpcScopeAuthorization checks each RPC's declared scope before its handler
1317+ // runs. This covers the one RPC whose scope depends on its input.
13171318 const authorizeEffect = < A , E , R > (
13181319 requiredScope : AuthEnvironmentScope ,
13191320 effect : Effect . Effect < A , E , R > ,
13201321 ) : Effect . Effect < A , E | EnvironmentAuthorizationError , R > =>
13211322 currentSession . scopes . includes ( requiredScope )
13221323 ? effect
1323- : Effect . fail ( authorizationError ( requiredScope ) ) ;
1324- const authorizeStream = < A , E , R > (
1325- requiredScope : AuthEnvironmentScope ,
1326- stream : Stream . Stream < A , E , R > ,
1327- ) : Stream . Stream < A , E | EnvironmentAuthorizationError , R > =>
1328- currentSession . scopes . includes ( requiredScope )
1329- ? stream
1330- : Stream . fail ( authorizationError ( requiredScope ) ) ;
1324+ : Effect . fail ( rpcAuthorizationError ( requiredScope ) ) ;
13311325
13321326 const acpRegistryProject = Effect . fn ( "ws.acpRegistry.project" ) ( function * (
13331327 projectId : ProjectId ,
@@ -1650,40 +1644,6 @@ const makeWsRpcLayer = (
16501644 yield * providerRegistry . refreshInstance ( input . instanceId ) ;
16511645 return { disabled : true } as const ;
16521646 } ) ;
1653- const observeRpcEffect = < A , E , R > (
1654- method : string ,
1655- effect : Effect . Effect < A , E , R > ,
1656- traceAttributes ?: Readonly < Record < string , unknown > > ,
1657- ) =>
1658- instrumentRpcEffect (
1659- method ,
1660- authorizeEffect ( requiredScopeForRpcMethod ( method ) , effect ) ,
1661- traceAttributes ,
1662- ) ;
1663- const observeRpcStream = < A , E , R > (
1664- method : string ,
1665- stream : Stream . Stream < A , E , R > ,
1666- traceAttributes ?: Readonly < Record < string , unknown > > ,
1667- ) =>
1668- instrumentRpcStream (
1669- method ,
1670- authorizeStream ( requiredScopeForRpcMethod ( method ) , stream ) ,
1671- traceAttributes ,
1672- ) ;
1673- const observeRpcStreamEffect = < A , StreamError , StreamContext , EffectError , EffectContext > (
1674- method : string ,
1675- effect : Effect . Effect <
1676- Stream . Stream < A , StreamError , StreamContext > ,
1677- EffectError ,
1678- EffectContext
1679- > ,
1680- traceAttributes ?: Readonly < Record < string , unknown > > ,
1681- ) =>
1682- instrumentRpcStreamEffect (
1683- method ,
1684- authorizeEffect ( requiredScopeForRpcMethod ( method ) , effect ) ,
1685- traceAttributes ,
1686- ) ;
16871647 const loadAuthAccessSnapshot = ( ) =>
16881648 Effect . all ( {
16891649 pairingLinks : serverAuth . listPairingLinks ( ) ,
@@ -3842,6 +3802,7 @@ export const websocketRpcRouteLayer = Layer.unwrap(
38423802 const { protocol, httpEffect } = yield * RpcServer . makeProtocolWithHttpEffectWebsocket ;
38433803 yield * RpcServer . make ( ServerWsRpcGroup , { disableTracing : true } ) . pipe (
38443804 Effect . provideService ( RpcServer . Protocol , withTerminalOutputWindow ( protocol ) ) ,
3805+ Effect . provide ( rpcScopeAuthorizationLayer ( session . scopes ) ) ,
38453806 Effect . forkScoped ,
38463807 ) ;
38473808 // @effect -diagnostics-next-line returnEffectInGen:off
0 commit comments