@@ -3,13 +3,9 @@ import { |
3 | 3 | type DeviceListInput, |
4 | 4 | clientRpcRequiredScopes, |
5 | 5 | authScopeRequiredResponse, |
6 | | - AssetCreateUrlInput, |
7 | 6 | AuthAccessReadScope, |
8 | | - ServerSettingsPatch, |
9 | | - ProviderInstanceMutation, |
10 | 7 | requiredScopesForServerSettingsPatch, |
11 | 8 | requiredScopesForProjectMutation, |
12 | | - ProjectMutation, |
13 | 9 | AuthSettingsWriteScope, |
14 | 10 | AuthProvidersManageScope, |
15 | 11 | AuthEnvironmentMaintainScope, |
@@ -31,11 +27,13 @@ import { |
31 | 27 | WsRpcGroup, |
32 | 28 | } from "@t3tools/contracts"; |
33 | 29 | import * as Effect from "effect/Effect"; |
34 | | -import * as Schema from "effect/Schema"; |
35 | 30 | import * as Layer from "effect/Layer"; |
| 31 | +import type * as Rpc from "effect/rpc/Rpc"; |
36 | 32 | import type * as RpcGroup from "effect/rpc/RpcGroup"; |
37 | 33 |
|
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>>; |
39 | 37 |
|
40 | 38 | /** |
41 | 39 | * Keep authorization coverage coupled to the RPC group itself. Adding an RPC to |
@@ -232,13 +230,9 @@ export const rpcAuthorizationError = (requiredScope: AuthEnvironmentScope) => |
232 | 230 | ...authScopeRequiredResponse(requiredScope), |
233 | 231 | }); |
234 | 232 |
|
235 | | -const SettingsUpdate = Schema.Struct({ |
236 | | - patch: ServerSettingsPatch, |
237 | | - providerInstanceMutation: Schema.optionalKey(ProviderInstanceMutation), |
238 | | -}); |
239 | | - |
240 | | -const requiredScopesForSettingsUpdate = (payload: unknown) => { |
241 | | - const input = Schema.decodeUnknownSync(SettingsUpdate)(payload); |
| 233 | +const requiredScopesForSettingsUpdate = ( |
| 234 | + input: WsRpcPayload<typeof WS_METHODS.serverUpdateSettings>, |
| 235 | +) => { |
242 | 236 | const scopes = requiredScopesForServerSettingsPatch(input.patch); |
243 | 237 | if (input.providerInstanceMutation === undefined) return scopes; |
244 | 238 | // An atomic provider mutation carries an empty patch unless it also changes settings. |
@@ -247,8 +241,11 @@ const requiredScopesForSettingsUpdate = (payload: unknown) => { |
247 | 241 | : [...new Set([...scopes, AuthProvidersManageScope])]; |
248 | 242 | }; |
249 | 243 |
|
250 | | -const decodeProjectMutation = Schema.decodeUnknownSync(ProjectMutation); |
251 | | - |
| 244 | +/** |
| 245 | + * The RPC server decodes `payload` before middleware runs, so it is already the |
| 246 | + * schema's Type side. Decoding it again fails on transformed fields, such as |
| 247 | + * Duration intervals in a settings patch. |
| 248 | + */ |
252 | 249 | const requiredScopesForRpcCall = ( |
253 | 250 | method: string, |
254 | 251 | payload: unknown, |
@@ -257,7 +254,7 @@ const requiredScopesForRpcCall = ( |
257 | 254 | return [AuthEnvironmentMaintainScope, AuthDiagnosticsReadScope]; |
258 | 255 | } |
259 | 256 | if (method === WS_METHODS.assetsCreateUrl) { |
260 | | - const { resource } = Schema.decodeUnknownSync(AssetCreateUrlInput)(payload); |
| 257 | + const { resource } = payload as WsRpcPayload<typeof WS_METHODS.assetsCreateUrl>; |
261 | 258 | return [ |
262 | 259 | resource._tag === "workspace-file" || |
263 | 260 | resource._tag === "media-file" || |
@@ -266,22 +263,36 @@ const requiredScopesForRpcCall = ( |
266 | 263 | : AuthOrchestrationReadScope, |
267 | 264 | ]; |
268 | 265 | } |
269 | | - if (method === WS_METHODS.serverUpdateSettings) return requiredScopesForSettingsUpdate(payload); |
| 266 | + if (method === WS_METHODS.serverUpdateSettings) { |
| 267 | + return requiredScopesForSettingsUpdate( |
| 268 | + payload as WsRpcPayload<typeof WS_METHODS.serverUpdateSettings>, |
| 269 | + ); |
| 270 | + } |
270 | 271 | if (method === WS_METHODS.projectsMutate) { |
271 | | - return requiredScopesForProjectMutation(decodeProjectMutation(payload)); |
| 272 | + return requiredScopesForProjectMutation( |
| 273 | + payload as WsRpcPayload<typeof WS_METHODS.projectsMutate>, |
| 274 | + ); |
272 | 275 | } |
273 | 276 | const guarded = clientRpcRequiredScopes(method, payload); |
274 | 277 | if (guarded.length > 0) return guarded; |
275 | 278 | return [requiredScopeForRpcMethod(method)]; |
276 | 279 | }; |
277 | 280 |
|
278 | | -/** Authorizes every RPC on one connection against that connection's session scopes. */ |
| 281 | +/** |
| 282 | + * Authorizes every RPC on one connection against that connection's session scopes. |
| 283 | + * Suspended so a throw while computing scopes fails that request; thrown synchronously, |
| 284 | + * the RPC server reports it as a connection defect and the client drops every request. |
| 285 | + */ |
279 | 286 | export const layer = (scopes: ReadonlyArray<AuthEnvironmentScope>) => |
280 | | - Layer.succeed(RpcScopeAuthorization)((effect, { rpc, payload }) => { |
281 | | - const requiredScopes = requiredScopesForRpcCall(rpc._tag, payload); |
282 | | - const requiredScope = requiredScopes.find((scope) => !scopes.includes(scope)); |
283 | | - return requiredScope === undefined ? effect : Effect.fail(rpcAuthorizationError(requiredScope)); |
284 | | - }); |
| 287 | + Layer.succeed(RpcScopeAuthorization)((effect, { rpc, payload }) => |
| 288 | + Effect.suspend(() => { |
| 289 | + const requiredScopes = requiredScopesForRpcCall(rpc._tag, payload); |
| 290 | + const requiredScope = requiredScopes.find((scope) => !scopes.includes(scope)); |
| 291 | + return requiredScope === undefined |
| 292 | + ? effect |
| 293 | + : Effect.fail(rpcAuthorizationError(requiredScope)); |
| 294 | + }), |
| 295 | + ); |
285 | 296 |
|
286 | 297 | /** Retrying can install or restart tools even though ordinary listing is readable. */ |
287 | 298 | export const requiredScopeForDeviceList = (input: DeviceListInput): AuthEnvironmentScope => |
|