Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 AuthOrchestrationReadScope,
4 AuthRelayReadScope,
5 AuthRelayWriteScope,
6 WS_METHODS,
7 WsRpcGroup,
8} from "@t3tools/contracts";
9import { describe, expect, it } from "@effect/vitest";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as RpcTest from "effect/rpc/RpcTest";
13
14import {
15 RPC_REQUIRED_SCOPES,
16 requiredScopeForRpcMethod,
17 requiredScopeForDeviceList,
18} from "./RpcAuthorization.ts";
19import * as RpcAuthorization from "./RpcAuthorization.ts";
20
21describe("RPC authorization scopes", () => {
22 it("declares exactly one scope for every RPC in the server group", () => {
23 expect(new Set(Object.keys(RPC_REQUIRED_SCOPES))).toEqual(new Set(WsRpcGroup.requests.keys()));
24 });
25
26 it("authorizes background policy reporting and observation deliberately", () => {
27 expect(requiredScopeForRpcMethod(WS_METHODS.serverReportClientActivity)).toBe(
28 AuthOrchestrationReadScope,
29 );
30 expect(requiredScopeForRpcMethod(WS_METHODS.serverReportHostPowerState)).toBe(
31 AuthOrchestrationOperateScope,
32 );
33 expect(requiredScopeForRpcMethod(WS_METHODS.serverGetBackgroundPolicy)).toBe(
34 AuthOrchestrationReadScope,
35 );
36 expect(requiredScopeForRpcMethod(WS_METHODS.subscribeBackgroundPolicy)).toBe(
37 AuthOrchestrationReadScope,
38 );
39 });
40
41 it("keeps webhook delivery logs, which hold request bodies, behind operate scope", () => {
42 for (const method of [
43 WS_METHODS.scheduledTasksListWebhookDeliveries,
44 WS_METHODS.scheduledTasksGetWebhookDelivery,
45 WS_METHODS.scheduledTasksRotateWebhookToken,
46 ]) {
47 expect(requiredScopeForRpcMethod(method)).toBe(AuthOrchestrationOperateScope);
48 }
49 });
50
51 it("allows relay status reads without granting relay installation access", () => {
52 expect(requiredScopeForRpcMethod(WS_METHODS.cloudGetRelayClientStatus)).toBe(
53 AuthRelayReadScope,
54 );
55 expect(requiredScopeForRpcMethod(WS_METHODS.cloudInstallRelayClient)).toBe(AuthRelayWriteScope);
56 });
57
58 it("requires permission to operate on a thread before uploading feedback", () => {
59 expect(requiredScopeForRpcMethod(WS_METHODS.providerUploadFeedback)).toBe(
60 AuthOrchestrationOperateScope,
61 );
62 });
63
64 it("requires write access to import agent session history", () => {
65 expect(requiredScopeForRpcMethod(WS_METHODS.agentSessionsScan)).toBe(
66 AuthOrchestrationReadScope,
67 );
68 expect(requiredScopeForRpcMethod(WS_METHODS.agentSessionsImport)).toBe(
69 AuthOrchestrationOperateScope,
70 );
71 });
72
73 it("separates ACP Registry discovery from provisioning", () => {
74 expect(requiredScopeForRpcMethod(WS_METHODS.serverSearchAcpRegistry)).toBe(
75 AuthOrchestrationReadScope,
76 );
77 expect(requiredScopeForRpcMethod(WS_METHODS.serverPrepareAcpRegistryAgent)).toBe(
78 AuthOrchestrationOperateScope,
79 );
80 expect(requiredScopeForRpcMethod(WS_METHODS.serverUninstallAcpRegistryManagedBinary)).toBe(
81 AuthOrchestrationOperateScope,
82 );
83 expect(requiredScopeForRpcMethod(WS_METHODS.serverAcceptAcpRegistryUrlAuth)).toBe(
84 AuthOrchestrationOperateScope,
85 );
86 expect(requiredScopeForRpcMethod(WS_METHODS.serverListAcpRegistrySessions)).toBe(
87 AuthOrchestrationReadScope,
88 );
89 expect(requiredScopeForRpcMethod(WS_METHODS.serverImportAcpRegistrySession)).toBe(
90 AuthOrchestrationOperateScope,
91 );
92 expect(requiredScopeForRpcMethod(WS_METHODS.serverLogoutAcpRegistry)).toBe(
93 AuthOrchestrationOperateScope,
94 );
95 });
96
97 it("reads the reviewer menu under the same scope as the pull request it belongs to", () => {
98 // The candidate list is a read like the detail beside it, and asking somebody for a review is
99 // a write like every other pull request operation.
100 expect(requiredScopeForRpcMethod(WS_METHODS.pullRequestsChecks)).toBe(
101 AuthOrchestrationReadScope,
102 );
103 expect(requiredScopeForRpcMethod(WS_METHODS.pullRequestsReviewerCandidates)).toBe(
104 requiredScopeForRpcMethod(WS_METHODS.pullRequestsDetail),
105 );
106 expect(requiredScopeForRpcMethod(WS_METHODS.pullRequestsRequestReviewers)).toBe(
107 requiredScopeForRpcMethod(WS_METHODS.pullRequestsComment),
108 );
109 });
110
111 it("rejects unknown RPC method names", () => {
112 for (const method of ["server.notRegistered", "toString", "constructor"]) {
113 expect(() => requiredScopeForRpcMethod(method)).toThrow(
114 `RPC method ${method} has no declared authorization scope.`,
115 );
116 }
117 });
118});
119
120it("requires operate permission for host retry while preserving read-only listing", () => {
121 expect(requiredScopeForDeviceList({})).toBe(AuthOrchestrationReadScope);
122 expect(requiredScopeForDeviceList({ retryHostId: "remote-host" })).toBe(
123 AuthOrchestrationOperateScope,
124 );
125});
126
127it("requires operate permission for tool updates even alongside a read-only check", () => {
128 expect(requiredScopeForDeviceList({ updateTool: "agent", inspectOnly: true })).toBe(
129 AuthOrchestrationOperateScope,
130 );
131 expect(requiredScopeForDeviceList({ updateTool: "hub" })).toBe(AuthOrchestrationOperateScope);
132});
133
134describe("RPC scope middleware", () => {
135 const tested = [WS_METHODS.serverProbe, WS_METHODS.serverRetryResourceTelemetry] as const;
136 const group = WsRpcGroup.omit(
137 ...[...WsRpcGroup.requests.keys()].filter(
138 (tag): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, (typeof tested)[number]> =>
139 !(tested as ReadonlyArray<string>).includes(tag),
140 ),
141 );
142
143 it.effect("checks each RPC's declared scope before its handler runs", () =>
144 Effect.gen(function* () {
145 const handled: Array<string> = [];
146 const client = yield* RpcTest.makeClient(group).pipe(
147 Effect.provide(
148 Layer.mergeAll(
149 group.toLayerHandler(WS_METHODS.serverProbe, () => Effect.succeed({})),
150 group.toLayerHandler(WS_METHODS.serverRetryResourceTelemetry, () =>
151 Effect.sync(() => handled.push("retry")).pipe(Effect.andThen(Effect.never)),
152 ),
153 RpcAuthorization.layer([AuthOrchestrationReadScope]),
154 ),
155 ),
156 );
157
158 expect(yield* client[WS_METHODS.serverProbe]({})).toEqual({});
159 expect(
160 yield* client[WS_METHODS.serverRetryResourceTelemetry]({}).pipe(Effect.flip),
161 ).toMatchObject({
162 _tag: "EnvironmentAuthorizationError",
163 requiredScope: AuthOrchestrationOperateScope,
164 });
165 expect(handled).toEqual([]);
166 }).pipe(Effect.scoped),
167 );
168});