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

clientRpcPermissions.ts

78 lines4.0 KB

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