GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(server): background activity settings save without dropping the c… · pingdotgg/t3code@9aecd4d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/auth
    • RpcAuthorization.test.ts
    • RpcAuthorization.ts
  • packages/contracts/src
    • clientRpcPermissions.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/auth/RpcAuthorization.test.ts
+34Lines 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;
Collapse file

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

Copy file name to clipboardExpand all lines: apps/server/src/auth/RpcAuthorization.ts
+35-24Lines 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
@@ -232,13 +230,9 @@ export const rpcAuthorizationError = (requiredScope: AuthEnvironmentScope) =>
232230
...authScopeRequiredResponse(requiredScope),
233231
});
234232

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+
) => {
242236
const scopes = requiredScopesForServerSettingsPatch(input.patch);
243237
if (input.providerInstanceMutation === undefined) return scopes;
244238
// An atomic provider mutation carries an empty patch unless it also changes settings.
@@ -247,8 +241,11 @@ const requiredScopesForSettingsUpdate = (payload: unknown) => {
247241
: [...new Set([...scopes, AuthProvidersManageScope])];
248242
};
249243

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+
*/
252249
const requiredScopesForRpcCall = (
253250
method: string,
254251
payload: unknown,
@@ -257,7 +254,7 @@ const requiredScopesForRpcCall = (
257254
return [AuthEnvironmentMaintainScope, AuthDiagnosticsReadScope];
258255
}
259256
if (method === WS_METHODS.assetsCreateUrl) {
260-
const { resource } = Schema.decodeUnknownSync(AssetCreateUrlInput)(payload);
257+
const { resource } = payload as WsRpcPayload<typeof WS_METHODS.assetsCreateUrl>;
261258
return [
262259
resource._tag === "workspace-file" ||
263260
resource._tag === "media-file" ||
@@ -266,22 +263,36 @@ const requiredScopesForRpcCall = (
266263
: AuthOrchestrationReadScope,
267264
];
268265
}
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+
}
270271
if (method === WS_METHODS.projectsMutate) {
271-
return requiredScopesForProjectMutation(decodeProjectMutation(payload));
272+
return requiredScopesForProjectMutation(
273+
payload as WsRpcPayload<typeof WS_METHODS.projectsMutate>,
274+
);
272275
}
273276
const guarded = clientRpcRequiredScopes(method, payload);
274277
if (guarded.length > 0) return guarded;
275278
return [requiredScopeForRpcMethod(method)];
276279
};
277280

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+
*/
279286
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+
);
285296

286297
/** Retrying can install or restart tools even though ordinary listing is readable. */
287298
export const requiredScopeForDeviceList = (input: DeviceListInput): AuthEnvironmentScope =>
Collapse file

‎packages/contracts/src/clientRpcPermissions.ts‎

Copy file name to clipboardExpand all lines: packages/contracts/src/clientRpcPermissions.ts
+6-5Lines 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,
@@ -50,14 +49,16 @@ export const CLIENT_GUARDED_RPC_SCOPES = {
5049
} as const;
5150
export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
5251

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

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.