Skip to content

Commit cbbf7e5

Browse files
committed
fix(server): background activity settings save without dropping the connection
RPC scope authorization decoded the settings payload a second time after the RPC server had already decoded it. Duration intervals were already Duration objects, so the second decode threw, and because the throw was synchronous inside middleware the server reported it as a connection defect. Authorization now reads the decoded payload directly. The same applies to asset URLs, project mutations, and the prepare pull request thread scope check. Scope calculation runs inside Effect.suspend so a throw fails only its request. Fixes #17233
1 parent 4b6d9a0 commit cbbf7e5

3 files changed

Lines changed: 75 additions & 29 deletions

File tree

‎apps/server/src/auth/RpcAuthorization.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
WsRpcGroup,
2121
} from "@t3tools/contracts";
2222
import { describe, expect, it } from "@effect/vitest";
23+
import * as Duration from "effect/Duration";
2324
import * as Effect from "effect/Effect";
2425
import * as Layer from "effect/Layer";
2526
import * as RpcTest from "effect/rpc/RpcTest";
@@ -304,6 +305,39 @@ describe("settings mutation authorization", () => {
304305
}).pipe(Effect.scoped),
305306
);
306307

308+
it.effect("authorizes interval settings from the decoded payload", () =>
309+
Effect.gen(function* () {
310+
let handled = 0;
311+
const client = yield* RpcTest.makeClient(group).pipe(
312+
Effect.provide(
313+
Layer.mergeAll(
314+
group.toLayerHandler(WS_METHODS.serverUpdateSettings, () =>
315+
Effect.sync(() => {
316+
handled++;
317+
return DEFAULT_SERVER_SETTINGS;
318+
}),
319+
),
320+
RpcAuthorization.layer([AuthSettingsWriteScope]),
321+
),
322+
),
323+
);
324+
yield* client[WS_METHODS.serverUpdateSettings]({
325+
patch: {
326+
automaticGitFetchInterval: Duration.minutes(5),
327+
backgroundActivity: {
328+
profile: "custom",
329+
overrides: {
330+
automaticGitFetchInterval: Duration.minutes(5),
331+
idleClientTtl: Duration.minutes(10),
332+
pauseWhenHostLocked: false,
333+
},
334+
},
335+
},
336+
});
337+
expect(handled).toBe(1);
338+
}).pipe(Effect.scoped),
339+
);
340+
307341
it.effect("does not let a settings grant create or remove providers", () =>
308342
Effect.gen(function* () {
309343
let handled = false;

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
3329
import * as Effect from "effect/Effect";
34-
import * as Schema from "effect/Schema";
3530
import * as Layer from "effect/Layer";
31+
import type * as Rpc from "effect/rpc/Rpc";
3632
import 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+
*/
251248
const 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+
*/
278285
export 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. */
286297
export const requiredScopeForDeviceList = (input: DeviceListInput): AuthEnvironmentScope =>

‎packages/contracts/src/clientRpcPermissions.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as Schema from "effect/Schema";
2-
import { GitPreparePullRequestThreadInput } from "./git.ts";
1+
import type { GitPreparePullRequestThreadInput } from "./git.ts";
32
import {
43
AuthOrchestrationOperateScope,
54
AuthSettingsWriteScope,
@@ -48,14 +47,16 @@ export const CLIENT_GUARDED_RPC_SCOPES = {
4847
} as const;
4948
export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
5049

51-
const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
52-
50+
/**
51+
* `input` is the RPC's decoded payload: the client's typed input before encoding, or what
52+
* the server's RPC layer already decoded. Never decode it again.
53+
*/
5354
export function clientRpcRequiredScopes(
5455
method: string,
5556
input: unknown,
5657
): readonly AuthEnvironmentScope[] {
5758
if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
58-
const payload = decodePrepareThread(input);
59+
const payload = input as GitPreparePullRequestThreadInput;
5960
if (payload.mode === "worktree" && payload.threadId !== undefined)
6061
return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
6162
}

0 commit comments

Comments
 (0)