@@ -3,13 +3,9 @@ import {
33 type DeviceListInput ,
44 clientRpcRequiredScopes ,
55 authScopeRequiredResponse ,
6- AssetCreateUrlInput ,
76 AuthAccessReadScope ,
8- ServerSettingsPatch ,
9- ProviderInstanceMutation ,
107 requiredScopesForServerSettingsPatch ,
118 requiredScopesForProjectMutation ,
12- ProjectMutation ,
139 AuthSettingsWriteScope ,
1410 AuthProvidersManageScope ,
1511 AuthEnvironmentMaintainScope ,
@@ -31,11 +27,13 @@ import {
3127 WsRpcGroup ,
3228} from "@t3tools/contracts" ;
3329import * as Effect from "effect/Effect" ;
34- import * as Schema from "effect/Schema" ;
3530import * as Layer from "effect/Layer" ;
31+ import type * as Rpc from "effect/rpc/Rpc" ;
3632import type * as RpcGroup from "effect/rpc/RpcGroup" ;
3733
38- type WsRpcMethod = RpcGroup . Rpcs < typeof WsRpcGroup > [ "_tag" ] ;
34+ type WsRpc = RpcGroup . Rpcs < typeof WsRpcGroup > ;
35+ type WsRpcMethod = WsRpc [ "_tag" ] ;
36+ type WsRpcPayload < Tag extends WsRpcMethod > = Rpc . Payload < Rpc . ExtractTag < WsRpc , Tag > > ;
3937
4038/**
4139 * Keep authorization coverage coupled to the RPC group itself. Adding an RPC to
@@ -231,13 +229,9 @@ export const rpcAuthorizationError = (requiredScope: AuthEnvironmentScope) =>
231229 ...authScopeRequiredResponse ( requiredScope ) ,
232230 } ) ;
233231
234- const SettingsUpdate = Schema . Struct ( {
235- patch : ServerSettingsPatch ,
236- providerInstanceMutation : Schema . optionalKey ( ProviderInstanceMutation ) ,
237- } ) ;
238-
239- const requiredScopesForSettingsUpdate = ( payload : unknown ) => {
240- const input = Schema . decodeUnknownSync ( SettingsUpdate ) ( payload ) ;
232+ const requiredScopesForSettingsUpdate = (
233+ input : WsRpcPayload < typeof WS_METHODS . serverUpdateSettings > ,
234+ ) => {
241235 const scopes = requiredScopesForServerSettingsPatch ( input . patch ) ;
242236 if ( input . providerInstanceMutation === undefined ) return scopes ;
243237 // An atomic provider mutation carries an empty patch unless it also changes settings.
@@ -246,8 +240,11 @@ const requiredScopesForSettingsUpdate = (payload: unknown) => {
246240 : [ ...new Set ( [ ...scopes , AuthProvidersManageScope ] ) ] ;
247241} ;
248242
249- const decodeProjectMutation = Schema . decodeUnknownSync ( ProjectMutation ) ;
250-
243+ /**
244+ * The RPC server decodes `payload` before middleware runs, so it is already the
245+ * schema's Type side. Decoding it again fails on transformed fields, such as
246+ * Duration intervals in a settings patch.
247+ */
251248const requiredScopesForRpcCall = (
252249 method : string ,
253250 payload : unknown ,
@@ -256,7 +253,7 @@ const requiredScopesForRpcCall = (
256253 return [ AuthEnvironmentMaintainScope , AuthDiagnosticsReadScope ] ;
257254 }
258255 if ( method === WS_METHODS . assetsCreateUrl ) {
259- const { resource } = Schema . decodeUnknownSync ( AssetCreateUrlInput ) ( payload ) ;
256+ const { resource } = payload as WsRpcPayload < typeof WS_METHODS . assetsCreateUrl > ;
260257 return [
261258 resource . _tag === "workspace-file" ||
262259 resource . _tag === "media-file" ||
@@ -265,22 +262,36 @@ const requiredScopesForRpcCall = (
265262 : AuthOrchestrationReadScope ,
266263 ] ;
267264 }
268- if ( method === WS_METHODS . serverUpdateSettings ) return requiredScopesForSettingsUpdate ( payload ) ;
265+ if ( method === WS_METHODS . serverUpdateSettings ) {
266+ return requiredScopesForSettingsUpdate (
267+ payload as WsRpcPayload < typeof WS_METHODS . serverUpdateSettings > ,
268+ ) ;
269+ }
269270 if ( method === WS_METHODS . projectsMutate ) {
270- return requiredScopesForProjectMutation ( decodeProjectMutation ( payload ) ) ;
271+ return requiredScopesForProjectMutation (
272+ payload as WsRpcPayload < typeof WS_METHODS . projectsMutate > ,
273+ ) ;
271274 }
272275 const guarded = clientRpcRequiredScopes ( method , payload ) ;
273276 if ( guarded . length > 0 ) return guarded ;
274277 return [ requiredScopeForRpcMethod ( method ) ] ;
275278} ;
276279
277- /** Authorizes every RPC on one connection against that connection's session scopes. */
280+ /**
281+ * Authorizes every RPC on one connection against that connection's session scopes.
282+ * Suspended so a throw while computing scopes fails that request; thrown synchronously,
283+ * the RPC server reports it as a connection defect and the client drops every request.
284+ */
278285export const layer = ( scopes : ReadonlyArray < AuthEnvironmentScope > ) =>
279- Layer . succeed ( RpcScopeAuthorization ) ( ( effect , { rpc, payload } ) => {
280- const requiredScopes = requiredScopesForRpcCall ( rpc . _tag , payload ) ;
281- const requiredScope = requiredScopes . find ( ( scope ) => ! scopes . includes ( scope ) ) ;
282- return requiredScope === undefined ? effect : Effect . fail ( rpcAuthorizationError ( requiredScope ) ) ;
283- } ) ;
286+ Layer . succeed ( RpcScopeAuthorization ) ( ( effect , { rpc, payload } ) =>
287+ Effect . suspend ( ( ) => {
288+ const requiredScopes = requiredScopesForRpcCall ( rpc . _tag , payload ) ;
289+ const requiredScope = requiredScopes . find ( ( scope ) => ! scopes . includes ( scope ) ) ;
290+ return requiredScope === undefined
291+ ? effect
292+ : Effect . fail ( rpcAuthorizationError ( requiredScope ) ) ;
293+ } ) ,
294+ ) ;
284295
285296/** Retrying can install or restart tools even though ordinary listing is readable. */
286297export const requiredScopeForDeviceList = ( input : DeviceListInput ) : AuthEnvironmentScope =>
0 commit comments