Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

clientRpcPermissions.ts

81 lines4.1 KB

1import * as Schema from "effect/Schema";
2import { GitPreparePullRequestThreadInput, GitRunStackedActionInput } from "./git.ts";
3import {
4 AuthAccessWriteScope,
5 AuthOrchestrationOperateScope,
6 AuthOrchestrationReadScope,
7 AuthPreviewOperateScope,
8 AuthSettingsWriteScope,
9 AuthSourceControlWriteScope,
10 type AuthEnvironmentScope,
11} from "./auth.ts";
12import { ORCHESTRATION_V2_WS_METHODS } from "./orchestrationV2.ts";
13import { WS_METHODS } from "./rpc.ts";
14​
15/** Incremental client enforcement; the server still authorizes every request. */
16export const CLIENT_GUARDED_RPC_SCOPES = {
17 [WS_METHODS.previewClose]: AuthPreviewOperateScope,
18 [WS_METHODS.serverRunStorageCleanup]: AuthSettingsWriteScope,
19 [WS_METHODS.serverCheckOtlpEndpoint]: AuthSettingsWriteScope,
20 [WS_METHODS.pullRequestsRunAction]: AuthSourceControlWriteScope,
21 [WS_METHODS.pullRequestsUpdate]: AuthSourceControlWriteScope,
22 [WS_METHODS.pullRequestsComment]: AuthSourceControlWriteScope,
23 [WS_METHODS.pullRequestsUpdateComment]: AuthSourceControlWriteScope,
24 [WS_METHODS.pullRequestsSubmitReview]: AuthSourceControlWriteScope,
25 [WS_METHODS.pullRequestsReplyToThread]: AuthSourceControlWriteScope,
26 [WS_METHODS.pullRequestsSetThreadResolution]: AuthSourceControlWriteScope,
27 [WS_METHODS.pullRequestsSetReaction]: AuthSourceControlWriteScope,
28 [WS_METHODS.pullRequestsSetFilesViewed]: AuthSourceControlWriteScope,
29 [WS_METHODS.pullRequestsRequestReviewers]: AuthSourceControlWriteScope,
30 [WS_METHODS.pullRequestsSetLabels]: AuthSourceControlWriteScope,
31 // Writing a guide runs a model on this environment's providers, which reading one does not.
32 [WS_METHODS.pullRequestsGenerateReviewGuide]: AuthOrchestrationOperateScope,
33 [WS_METHODS.sourceControlCloneRepository]: AuthSourceControlWriteScope,
34 [WS_METHODS.sourceControlPublishRepository]: AuthSourceControlWriteScope,
35 [WS_METHODS.projectCloneStart]: AuthSourceControlWriteScope,
36 [WS_METHODS.projectCloneCancel]: AuthSourceControlWriteScope,
37 [WS_METHODS.projectCloneRetry]: AuthSourceControlWriteScope,
38 [WS_METHODS.vcsPull]: AuthSourceControlWriteScope,
39 [WS_METHODS.gitRunStackedAction]: AuthSourceControlWriteScope,
40 [WS_METHODS.gitPreparePullRequestThread]: AuthSourceControlWriteScope,
41 [WS_METHODS.vcsCreateWorktree]: AuthSourceControlWriteScope,
42 [WS_METHODS.vcsRemoveWorktree]: AuthSourceControlWriteScope,
43 [WS_METHODS.vcsCreateRef]: AuthSourceControlWriteScope,
44 [WS_METHODS.vcsSwitchRef]: AuthSourceControlWriteScope,
45 [WS_METHODS.vcsInit]: AuthSourceControlWriteScope,
46​
47 [WS_METHODS.scheduledTasksUpsert]: AuthOrchestrationOperateScope,
48 [WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
49 [WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
50 [WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
51 [WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
52 [ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: AuthOrchestrationOperateScope,
53 // A guest link hands thread access to someone outside, like a pairing link.
54 [WS_METHODS.guestShareCreate]: AuthAccessWriteScope,
55} as const;
56export type ClientGuardedRpcTag = keyof typeof CLIENT_GUARDED_RPC_SCOPES;
57​
58const decodePrepareThread = Schema.decodeUnknownSync(GitPreparePullRequestThreadInput);
59const decodeStackedAction = Schema.decodeUnknownSync(GitRunStackedActionInput);
60​
61export function clientRpcRequiredScopes(
62 method: string,
63 input: unknown,
64): readonly AuthEnvironmentScope[] {
65 if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
66 const payload = decodePrepareThread(input);
67 if (payload.mode === "worktree" && payload.threadId !== undefined)
68 return [AuthSourceControlWriteScope, AuthOrchestrationOperateScope];
69 }
70 if (method === WS_METHODS.gitRunStackedAction && input !== undefined) {
71 const payload = decodeStackedAction(input);
72 if (
73 payload.threadId !== undefined &&
74 (payload.action === "create_pr" || payload.action === "commit_push_pr")
75 )
76 return [AuthSourceControlWriteScope, AuthOrchestrationReadScope];
77 }
78 return Object.hasOwn(CLIENT_GUARDED_RPC_SCOPES, method)
79 ? [CLIENT_GUARDED_RPC_SCOPES[method as ClientGuardedRpcTag]]
80 : [];