The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { |
| 2 | AuthEnvironmentMaintainScope, |
| 3 | AuthDiagnosticsReadScope, |
| 4 | AuthFilesystemReadScope, |
| 5 | AuthProvidersManageScope, |
| 6 | AuthSettingsWriteScope, |
| 7 | AuthAccessWriteScope, |
| 8 | DEFAULT_SERVER_SETTINGS, |
| 9 | EnvironmentId, |
| 10 | ProviderDriverKind, |
| 11 | ProviderInstanceId, |
| 12 | ProjectId, |
| 13 | IssueTrackerKind, |
| 14 | SecretProviderId, |
| 15 | ThreadId, |
| 16 | TurnItemId, |
| 17 | AuthOrchestrationOperateScope, |
| 18 | AuthOrchestrationReadScope, |
| 19 | AuthSourceControlWriteScope, |
| 20 | AuthTerminalReadScope, |
| 21 | AuthTerminalOperateScope, |
| 22 | CommandId, |
| 23 | MessageId, |
| 24 | ORCHESTRATION_V2_WS_METHODS, |
| 25 | RunId, |
| 26 | WS_METHODS, |
| 27 | WsRpcGroup, |
| 28 | } from "@t3tools/contracts"; |
| 29 | import { describe, expect, it } from "@effect/vitest"; |
| 30 | import * as Effect from "effect/Effect"; |
| 31 | import * as Layer from "effect/Layer"; |
| 32 | import * as Stream from "effect/Stream"; |
| 33 | import * as RpcTest from "effect/rpc/RpcTest"; |
| 34 | |
| 35 | import { |
| 36 | RPC_REQUIRED_SCOPES, |
| 37 | requiredScopeForRpcMethod, |
| 38 | requiredScopeForDeviceList, |
| 39 | } from "./RpcAuthorization.ts"; |
| 40 | import * as RpcAuthorization from "./RpcAuthorization.ts"; |
| 41 | |
| 42 | describe("RPC authorization scopes", () => { |
| 43 | it("declares exactly one scope for every RPC in the server group", () => { |
| 44 | expect(new Set(Object.keys(RPC_REQUIRED_SCOPES))).toEqual(new Set(WsRpcGroup.requests.keys())); |
| 45 | }); |
| 46 | |
| 47 | it("keeps webhook delivery logs, which hold request bodies, behind operate scope", () => { |
| 48 | for (const method of [ |
| 49 | WS_METHODS.scheduledTasksListWebhookDeliveries, |
| 50 | WS_METHODS.scheduledTasksGetWebhookDelivery, |
| 51 | WS_METHODS.scheduledTasksRotateWebhookToken, |
| 52 | ]) { |
| 53 | expect(requiredScopeForRpcMethod(method)).toBe(AuthOrchestrationOperateScope); |
| 54 | } |
| 55 | }); |
| 56 | |
| 57 | it("keeps triage, which seeds an agent that reads logs and the database, behind operate scope", () => { |
| 58 | expect(requiredScopeForRpcMethod(WS_METHODS.serverPrepareTriage)).toBe( |
| 59 | AuthOrchestrationOperateScope, |
| 60 | ); |
| 61 | }); |
| 62 | |
| 63 | it("separates passive terminal observation from operations that can change a shell", () => { |
| 64 | for (const method of [WS_METHODS.terminalObserve, WS_METHODS.subscribeTerminalMetadata]) { |
| 65 | expect(requiredScopeForRpcMethod(method)).toBe(AuthTerminalReadScope); |
| 66 | } |
| 67 | for (const method of [ |
| 68 | WS_METHODS.terminalAttach, |
| 69 | WS_METHODS.terminalOpen, |
| 70 | WS_METHODS.terminalWrite, |
| 71 | WS_METHODS.terminalResize, |
| 72 | WS_METHODS.terminalClear, |
| 73 | WS_METHODS.terminalClose, |
| 74 | WS_METHODS.previewStopLocalServer, |
| 75 | ]) { |
| 76 | expect(requiredScopeForRpcMethod(method)).toBe(AuthTerminalOperateScope); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | it("lets thread readers observe and refresh checkout branches", () => { |
| 81 | for (const method of [WS_METHODS.subscribeVcsBranch, WS_METHODS.vcsRefreshBranch]) { |
| 82 | expect(requiredScopeForRpcMethod(method)).toBe(AuthOrchestrationReadScope); |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | it("still lets readers load settings the way installed Swift clients ask", () => { |
| 87 | expect(requiredScopeForRpcMethod("server.getSettings")).toBe(AuthOrchestrationReadScope); |
| 88 | }); |
| 89 | |
| 90 | it("rejects unknown RPC method names", () => { |
| 91 | for (const method of ["server.notRegistered", "toString", "constructor"]) { |
| 92 | expect(() => requiredScopeForRpcMethod(method)).toThrow( |
| 93 | `RPC method ${method} has no declared authorization scope.`, |
| 94 | ); |
| 95 | } |
| 96 | }); |
| 97 | }); |
| 98 | |
| 99 | it("requires operate permission for host retry while preserving read-only listing", () => { |
| 100 | expect(requiredScopeForDeviceList({})).toBe(AuthOrchestrationReadScope); |
| 101 | expect(requiredScopeForDeviceList({ retryHostId: "remote-host" })).toBe( |
| 102 | AuthOrchestrationOperateScope, |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | it("requires operate permission for tool updates even alongside a read-only check", () => { |
| 107 | expect(requiredScopeForDeviceList({ updateTool: "agent", inspectOnly: true })).toBe( |
| 108 | AuthOrchestrationOperateScope, |
| 109 | ); |
| 110 | expect(requiredScopeForDeviceList({ updateTool: "hub" })).toBe(AuthOrchestrationOperateScope); |
| 111 | }); |
| 112 | |
| 113 | describe("RPC scope middleware", () => { |
| 114 | const tested = [WS_METHODS.serverProbe, WS_METHODS.serverRetryResourceTelemetry] as const; |
| 115 | const group = WsRpcGroup.omit( |
| 116 | ...[...WsRpcGroup.requests.keys()].filter( |
| 117 | (tag): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, (typeof tested)[number]> => |
| 118 | !(tested as ReadonlyArray<string>).includes(tag), |
| 119 | ), |
| 120 | ); |
| 121 | |
| 122 | it.effect.each([ |
| 123 | { scopes: [AuthOrchestrationReadScope], missing: AuthEnvironmentMaintainScope }, |
| 124 | { |
| 125 | scopes: [AuthOrchestrationReadScope, AuthEnvironmentMaintainScope], |
| 126 | missing: AuthDiagnosticsReadScope, |
| 127 | }, |
| 128 | ])("rejects telemetry retry without $missing before its handler runs", ({ scopes, missing }) => |
| 129 | Effect.gen(function* () { |
| 130 | const handled: Array<string> = []; |
| 131 | const client = yield* RpcTest.makeClient(group).pipe( |
| 132 | Effect.provide( |
| 133 | Layer.mergeAll( |
| 134 | group.toLayerHandler(WS_METHODS.serverProbe, () => Effect.succeed({})), |
| 135 | group.toLayerHandler(WS_METHODS.serverRetryResourceTelemetry, () => |
| 136 | Effect.sync(() => handled.push("retry")).pipe(Effect.andThen(Effect.never)), |
| 137 | ), |
| 138 | RpcAuthorization.layer(scopes), |
| 139 | ), |
| 140 | ), |
| 141 | ); |
| 142 | |
| 143 | expect(yield* client[WS_METHODS.serverProbe]({})).toEqual({}); |
| 144 | expect( |
| 145 | yield* client[WS_METHODS.serverRetryResourceTelemetry]({}).pipe(Effect.flip), |
| 146 | ).toMatchObject({ |
| 147 | _tag: "EnvironmentAuthorizationError", |
| 148 | requiredPermission: missing, |
| 149 | }); |
| 150 | expect(handled).toEqual([]); |
| 151 | }), |
| 152 | ); |
| 153 | }); |
| 154 | |
| 155 | it.effect("allows issue reads but requires a source-control grant before commenting", () => |
| 156 | Effect.gen(function* () { |
| 157 | const tested = [WS_METHODS.issuesList, WS_METHODS.issuesComment] as const; |
| 158 | const group = WsRpcGroup.omit( |
| 159 | ...[...WsRpcGroup.requests.keys()].filter( |
| 160 | (tag): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, (typeof tested)[number]> => |
| 161 | !(tested as ReadonlyArray<string>).includes(tag), |
| 162 | ), |
| 163 | ); |
| 164 | const comments: string[] = []; |
| 165 | const handlers = Layer.mergeAll( |
| 166 | group.toLayerHandler(WS_METHODS.issuesList, () => |
| 167 | Effect.succeed({ entries: [], trackers: [], errors: [], viewers: {}, nextCursors: {} }), |
| 168 | ), |
| 169 | group.toLayerHandler(WS_METHODS.issuesComment, ({ body }) => |
| 170 | Effect.sync(() => { |
| 171 | comments.push(body); |
| 172 | }), |
| 173 | ), |
| 174 | ); |
| 175 | const reader = yield* RpcTest.makeClient(group).pipe( |
| 176 | Effect.provide(Layer.merge(handlers, RpcAuthorization.layer([AuthOrchestrationReadScope]))), |
| 177 | ); |
| 178 | const comment = { |
| 179 | projectId: ProjectId.make("web"), |
| 180 | tracker: IssueTrackerKind.make("github"), |
| 181 | scope: "acme/web", |
| 182 | key: "42", |
| 183 | body: "Repro attached.", |
| 184 | }; |
| 185 | |
| 186 | expect((yield* reader[WS_METHODS.issuesList]({ state: "open" })).entries).toEqual([]); |
| 187 | expect(yield* reader[WS_METHODS.issuesComment](comment).pipe(Effect.flip)).toMatchObject({ |
| 188 | _tag: "EnvironmentAuthorizationError", |
| 189 | requiredPermission: AuthSourceControlWriteScope, |
| 190 | }); |
| 191 | expect(comments).toEqual([]); |
| 192 | |
| 193 | const writer = yield* RpcTest.makeClient(group).pipe( |
| 194 | Effect.provide( |
| 195 | Layer.merge( |
| 196 | handlers, |
| 197 | RpcAuthorization.layer([AuthOrchestrationReadScope, AuthSourceControlWriteScope]), |
| 198 | ), |
| 199 | ), |
| 200 | ); |
| 201 | yield* writer[WS_METHODS.issuesComment](comment); |
| 202 | expect(comments).toEqual(["Repro attached."]); |
| 203 | }).pipe(Effect.scoped), |
| 204 | ); |
| 205 | |
| 206 | it.effect("reads a linked environment's thread only for a client that may read threads here", () => |
| 207 | Effect.gen(function* () { |
| 208 | const linkedThreadTags = [ |
| 209 | WS_METHODS.peerLinksSubscribeThread, |
| 210 | WS_METHODS.peerLinksThreadHistoryPage, |
| 211 | WS_METHODS.peerLinksThreadItem, |
| 212 | WS_METHODS.peerLinksNames, |
| 213 | ] as const; |
| 214 | const group = WsRpcGroup.omit( |
| 215 | ...[...WsRpcGroup.requests.keys()].filter( |
| 216 | ( |
| 217 | tag, |
| 218 | ): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, (typeof linkedThreadTags)[number]> => |
| 219 | !(linkedThreadTags as ReadonlyArray<string>).includes(tag), |
| 220 | ), |
| 221 | ); |
| 222 | const read: string[] = []; |
| 223 | const input = { |
| 224 | environmentId: EnvironmentId.make("environment-box"), |
| 225 | threadId: ThreadId.make("thread:box"), |
| 226 | }; |
| 227 | const handlers = group.toLayer({ |
| 228 | [WS_METHODS.peerLinksSubscribeThread]: ({ threadId }) => |
| 229 | Stream.sync(() => { |
| 230 | read.push(threadId); |
| 231 | return { kind: "synchronized" as const }; |
| 232 | }), |
| 233 | [WS_METHODS.peerLinksThreadHistoryPage]: () => Effect.die("unused"), |
| 234 | [WS_METHODS.peerLinksThreadItem]: ({ threadId }) => |
| 235 | Effect.sync(() => { |
| 236 | read.push(threadId); |
| 237 | return { item: null }; |
| 238 | }), |
| 239 | // A thread link resolves to the linked environment by its name. |
| 240 | [WS_METHODS.peerLinksNames]: () => |
| 241 | Effect.succeed({ links: [{ environmentId: input.environmentId, label: "Box" }] }), |
| 242 | }); |
| 243 | // Managing links and writing settings here say nothing about reading threads. |
| 244 | const outsider = yield* RpcTest.makeClient(group).pipe( |
| 245 | Effect.provide( |
| 246 | Layer.merge( |
| 247 | handlers, |
| 248 | RpcAuthorization.layer([AuthAccessWriteScope, AuthSettingsWriteScope]), |
| 249 | ), |
| 250 | ), |
| 251 | ); |
| 252 | const refused = { |
| 253 | _tag: "EnvironmentAuthorizationError", |
| 254 | requiredPermission: AuthOrchestrationReadScope, |
| 255 | }; |
| 256 | expect( |
| 257 | yield* outsider[WS_METHODS.peerLinksSubscribeThread](input).pipe( |
| 258 | Stream.runCollect, |
| 259 | Effect.flip, |
| 260 | ), |
| 261 | ).toMatchObject(refused); |
| 262 | expect( |
| 263 | yield* outsider[WS_METHODS.peerLinksThreadItem]({ |
| 264 | ...input, |
| 265 | itemId: TurnItemId.make("item:1"), |
| 266 | }).pipe(Effect.flip), |
| 267 | ).toMatchObject(refused); |
| 268 | expect( |
| 269 | yield* outsider[WS_METHODS.peerLinksThreadHistoryPage]({ ...input, cursor: "c" }).pipe( |
| 270 | Effect.flip, |
| 271 | ), |
| 272 | ).toMatchObject(refused); |
| 273 | expect(read).toEqual([]); |
| 274 | expect(yield* outsider[WS_METHODS.peerLinksNames]({}).pipe(Effect.flip)).toMatchObject({ |
| 275 | requiredPermission: AuthOrchestrationReadScope, |
| 276 | }); |
| 277 | |
| 278 | const reader = yield* RpcTest.makeClient(group).pipe( |
| 279 | Effect.provide(Layer.merge(handlers, RpcAuthorization.layer([AuthOrchestrationReadScope]))), |
| 280 | ); |
| 281 | expect( |
| 282 | Array.from(yield* reader[WS_METHODS.peerLinksSubscribeThread](input).pipe(Stream.runCollect)), |
| 283 | ).toEqual([{ kind: "synchronized" }]); |
| 284 | expect(read).toEqual([input.threadId]); |
| 285 | expect((yield* reader[WS_METHODS.peerLinksNames]({})).links).toEqual([ |
| 286 | { environmentId: input.environmentId, label: "Box" }, |
| 287 | ]); |
| 288 | }).pipe(Effect.scoped), |
| 289 | ); |
| 290 | |
| 291 | it.effect("pairs a client with a linked environment only for one that may hand out access", () => |
| 292 | Effect.gen(function* () { |
| 293 | const group = WsRpcGroup.omit( |
| 294 | ...[...WsRpcGroup.requests.keys()].filter( |
| 295 | ( |
| 296 | tag, |
| 297 | ): tag is Exclude< |
| 298 | keyof typeof RPC_REQUIRED_SCOPES, |
| 299 | typeof WS_METHODS.peerLinksPairClient |
| 300 | > => tag !== WS_METHODS.peerLinksPairClient, |
| 301 | ), |
| 302 | ); |
| 303 | let asked = 0; |
| 304 | const environmentId = EnvironmentId.make("environment-box"); |
| 305 | const handlers = group.toLayerHandler(WS_METHODS.peerLinksPairClient, () => |
| 306 | Effect.sync(() => { |
| 307 | asked++; |
| 308 | return { |
| 309 | type: "unsupported" as const, |
| 310 | reason: "link_access" as const, |
| 311 | environmentId, |
| 312 | label: "Box", |
| 313 | urls: [], |
| 314 | }; |
| 315 | }), |
| 316 | ); |
| 317 | // Running agents and reading the access list do not include handing access out. |
| 318 | const operator = yield* RpcTest.makeClient(group).pipe( |
| 319 | Effect.provide( |
| 320 | Layer.merge( |
| 321 | handlers, |
| 322 | RpcAuthorization.layer([AuthOrchestrationReadScope, AuthOrchestrationOperateScope]), |
| 323 | ), |
| 324 | ), |
| 325 | ); |
| 326 | expect( |
| 327 | yield* operator[WS_METHODS.peerLinksPairClient]({ environmentId }).pipe(Effect.flip), |
| 328 | ).toMatchObject({ |
| 329 | _tag: "EnvironmentAuthorizationError", |
| 330 | requiredPermission: AuthAccessWriteScope, |
| 331 | }); |
| 332 | expect(asked).toBe(0); |
| 333 | |
| 334 | const manager = yield* RpcTest.makeClient(group).pipe( |
| 335 | Effect.provide(Layer.merge(handlers, RpcAuthorization.layer([AuthAccessWriteScope]))), |
| 336 | ); |
| 337 | expect((yield* manager[WS_METHODS.peerLinksPairClient]({ environmentId })).type).toBe( |
| 338 | "unsupported", |
| 339 | ); |
| 340 | expect(asked).toBe(1); |
| 341 | }).pipe(Effect.scoped), |
| 342 | ); |
| 343 | |
| 344 | it.effect.each([WS_METHODS.subscribeVcsBranch, WS_METHODS.subscribeVcsStatus] as const)( |
| 345 | "enforces read permission on stream %s", |
| 346 | (method) => |
| 347 | Effect.gen(function* () { |
| 348 | const group = WsRpcGroup.omit( |
| 349 | ...[...WsRpcGroup.requests.keys()].filter( |
| 350 | ( |
| 351 | tag, |
| 352 | ): tag is Exclude< |
| 353 | keyof typeof RPC_REQUIRED_SCOPES, |
| 354 | typeof WS_METHODS.subscribeVcsBranch | typeof WS_METHODS.subscribeVcsStatus |
| 355 | > => tag !== WS_METHODS.subscribeVcsBranch && tag !== WS_METHODS.subscribeVcsStatus, |
| 356 | ), |
| 357 | ); |
| 358 | let handled = 0; |
| 359 | const handlers = Layer.mergeAll( |
| 360 | group.toLayerHandler(WS_METHODS.subscribeVcsBranch, () => |
| 361 | Stream.fromEffect( |
| 362 | Effect.sync(() => { |
| 363 | handled++; |
| 364 | return { refName: "main" }; |
| 365 | }), |
| 366 | ), |
| 367 | ), |
| 368 | group.toLayerHandler(WS_METHODS.subscribeVcsStatus, () => |
| 369 | Stream.fromEffect( |
| 370 | Effect.sync(() => { |
| 371 | handled++; |
| 372 | }), |
| 373 | ).pipe(Stream.drain), |
| 374 | ), |
| 375 | ); |
| 376 | const denied = yield* RpcTest.makeClient(group).pipe( |
| 377 | Effect.provide(Layer.merge(handlers, RpcAuthorization.layer([]))), |
| 378 | ); |
| 379 | expect( |
| 380 | yield* Stream.runDrain( |
| 381 | method === WS_METHODS.subscribeVcsBranch |
| 382 | ? denied[method]({ cwd: "/repo" }).pipe(Stream.drain) |
| 383 | : denied[method]({ cwd: "/repo" }).pipe(Stream.drain), |
| 384 | ).pipe(Effect.flip), |
| 385 | ).toMatchObject({ requiredPermission: AuthOrchestrationReadScope }); |
| 386 | expect(handled).toBe(0); |
| 387 | const granted = yield* RpcTest.makeClient(group).pipe( |
| 388 | Effect.provide(Layer.merge(handlers, RpcAuthorization.layer([AuthOrchestrationReadScope]))), |
| 389 | ); |
| 390 | yield* Stream.runDrain( |
| 391 | method === WS_METHODS.subscribeVcsBranch |
| 392 | ? granted[method]({ cwd: "/repo" }).pipe(Stream.drain) |
| 393 | : granted[method]({ cwd: "/repo" }).pipe(Stream.drain), |
| 394 | ); |
| 395 | expect(handled).toBe(1); |
| 396 | }), |
| 397 | ); |
| 398 | |
| 399 | describe("settings mutation authorization", () => { |
| 400 | const group = WsRpcGroup.omit( |
| 401 | ...[...WsRpcGroup.requests.keys()].filter( |
| 402 | ( |
| 403 | tag, |
| 404 | ): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, typeof WS_METHODS.serverUpdateSettings> => |
| 405 | tag !== WS_METHODS.serverUpdateSettings, |
| 406 | ), |
| 407 | ); |
| 408 | const providerInstanceMutation = { |
| 409 | operation: "remove" as const, |
| 410 | instanceId: ProviderInstanceId.make("codex_work"), |
| 411 | }; |
| 412 | |
| 413 | it.effect("allows provider-only mutations while denying mixed settings without their grant", () => |
| 414 | Effect.gen(function* () { |
| 415 | let handled = 0; |
| 416 | const client = yield* RpcTest.makeClient(group).pipe( |
| 417 | Effect.provide( |
| 418 | Layer.mergeAll( |
| 419 | group.toLayerHandler(WS_METHODS.serverUpdateSettings, () => |
| 420 | Effect.sync(() => { |
| 421 | handled++; |
| 422 | return DEFAULT_SERVER_SETTINGS; |
| 423 | }), |
| 424 | ), |
| 425 | RpcAuthorization.layer([AuthProvidersManageScope]), |
| 426 | ), |
| 427 | ), |
| 428 | ); |
| 429 | yield* client[WS_METHODS.serverUpdateSettings]({ patch: {}, providerInstanceMutation }); |
| 430 | expect(handled).toBe(1); |
| 431 | yield* client[WS_METHODS.serverUpdateSettings]({ |
| 432 | patch: {}, |
| 433 | providerInstanceMutation: { |
| 434 | operation: "upsert", |
| 435 | instanceId: ProviderInstanceId.make("codex_work"), |
| 436 | instance: { |
| 437 | driver: ProviderDriverKind.make("codex"), |
| 438 | environment: [ |
| 439 | { |
| 440 | name: "OPENAI_API_KEY", |
| 441 | sensitive: true, |
| 442 | value: { |
| 443 | provider: SecretProviderId.make("personal"), |
| 444 | reference: "op://Private/OpenAI/credential", |
| 445 | }, |
| 446 | }, |
| 447 | ], |
| 448 | }, |
| 449 | }, |
| 450 | }); |
| 451 | expect(handled).toBe(2); |
| 452 | expect( |
| 453 | yield* client[WS_METHODS.serverUpdateSettings]({ |
| 454 | patch: { defaultRuntimeMode: "full-access" }, |
| 455 | providerInstanceMutation, |
| 456 | }).pipe(Effect.flip), |
| 457 | ).toMatchObject({ requiredPermission: AuthSettingsWriteScope }); |
| 458 | // Removing a secret provider clears settings fields that read from it. |
| 459 | expect( |
| 460 | yield* client[WS_METHODS.serverUpdateSettings]({ |
| 461 | patch: { secretProviders: { [SecretProviderId.make("personal")]: null } }, |
| 462 | }).pipe(Effect.flip), |
| 463 | ).toMatchObject({ requiredPermission: AuthSettingsWriteScope }); |
| 464 | expect(handled).toBe(2); |
| 465 | }), |
| 466 | ); |
| 467 | |
| 468 | it.effect( |
| 469 | "does not let a settings grant manage providers or turn off T3 Connect device approval", |
| 470 | () => |
| 471 | Effect.gen(function* () { |
| 472 | let handled = false; |
| 473 | const client = yield* RpcTest.makeClient(group).pipe( |
| 474 | Effect.provide( |
| 475 | Layer.mergeAll( |
| 476 | group.toLayerHandler(WS_METHODS.serverUpdateSettings, () => |
| 477 | Effect.sync(() => { |
| 478 | handled = true; |
| 479 | return DEFAULT_SERVER_SETTINGS; |
| 480 | }), |
| 481 | ), |
| 482 | RpcAuthorization.layer([AuthSettingsWriteScope]), |
| 483 | ), |
| 484 | ), |
| 485 | ); |
| 486 | expect( |
| 487 | yield* client[WS_METHODS.serverUpdateSettings]({ |
| 488 | patch: {}, |
| 489 | providerInstanceMutation, |
| 490 | }).pipe(Effect.flip), |
| 491 | ).toMatchObject({ requiredPermission: AuthProvidersManageScope }); |
| 492 | expect( |
| 493 | yield* client[WS_METHODS.serverUpdateSettings]({ |
| 494 | patch: { secretProviders: { [SecretProviderId.make("personal")]: null } }, |
| 495 | }).pipe(Effect.flip), |
| 496 | ).toMatchObject({ requiredPermission: AuthProvidersManageScope }); |
| 497 | expect( |
| 498 | yield* client[WS_METHODS.serverUpdateSettings]({ |
| 499 | patch: { requireConnectDeviceApproval: false }, |
| 500 | }).pipe(Effect.flip), |
| 501 | ).toMatchObject({ requiredPermission: AuthAccessWriteScope }); |
| 502 | // A reference names any secret the store holds, and the host entry names where it goes. |
| 503 | expect( |
| 504 | yield* client[WS_METHODS.serverUpdateSettings]({ |
| 505 | patch: { |
| 506 | simlockDeviceHosts: [ |
| 507 | { |
| 508 | kind: "simlock", |
| 509 | id: "simlock_evil", |
| 510 | label: "Gateway", |
| 511 | gatewayUrl: "https://attacker.example", |
| 512 | agentToken: { |
| 513 | provider: SecretProviderId.make("personal"), |
| 514 | reference: "op://Private/OpenAI/credential", |
| 515 | }, |
| 516 | maxLeases: 2, |
| 517 | maxLeaseMinutes: 60, |
| 518 | }, |
| 519 | ], |
| 520 | }, |
| 521 | }).pipe(Effect.flip), |
| 522 | ).toMatchObject({ requiredPermission: AuthProvidersManageScope }); |
| 523 | expect(handled).toBe(false); |
| 524 | }), |
| 525 | ); |
| 526 | |
| 527 | it.effect("does not let a settings grant send a named secret to a gateway it picks", () => |
| 528 | Effect.gen(function* () { |
| 529 | const group = WsRpcGroup.omit( |
| 530 | ...[...WsRpcGroup.requests.keys()].filter( |
| 531 | ( |
| 532 | tag, |
| 533 | ): tag is Exclude< |
| 534 | keyof typeof RPC_REQUIRED_SCOPES, |
| 535 | typeof WS_METHODS.deviceSimlockIdentity |
| 536 | > => tag !== WS_METHODS.deviceSimlockIdentity, |
| 537 | ), |
| 538 | ); |
| 539 | let handled = 0; |
| 540 | const client = yield* RpcTest.makeClient(group).pipe( |
| 541 | Effect.provide( |
| 542 | Layer.mergeAll( |
| 543 | group.toLayerHandler(WS_METHODS.deviceSimlockIdentity, () => |
| 544 | Effect.sync(() => { |
| 545 | handled++; |
| 546 | return { |
| 547 | gatewayUrl: "https://attacker.example", |
| 548 | signedInAs: "agent", |
| 549 | tokenSource: "entered" as const, |
| 550 | }; |
| 551 | }), |
| 552 | ), |
| 553 | RpcAuthorization.layer([AuthSettingsWriteScope]), |
| 554 | ), |
| 555 | ), |
| 556 | ); |
| 557 | yield* client[WS_METHODS.deviceSimlockIdentity]({ |
| 558 | gatewayUrl: "https://attacker.example", |
| 559 | agentToken: "typed-token", |
| 560 | }); |
| 561 | expect(handled).toBe(1); |
| 562 | expect( |
| 563 | yield* client[WS_METHODS.deviceSimlockIdentity]({ |
| 564 | gatewayUrl: "https://attacker.example", |
| 565 | agentToken: { |
| 566 | provider: SecretProviderId.make("personal"), |
| 567 | reference: "op://Private/OpenAI/credential", |
| 568 | }, |
| 569 | }).pipe(Effect.flip), |
| 570 | ).toMatchObject({ requiredPermission: AuthProvidersManageScope }); |
| 571 | expect(handled).toBe(1); |
| 572 | }), |
| 573 | ); |
| 574 | }); |
| 575 | |
| 576 | it.effect("requires task permission before attaching a prepared worktree to a thread", () => |
| 577 | Effect.gen(function* () { |
| 578 | const group = WsRpcGroup.omit( |
| 579 | ...[...WsRpcGroup.requests.keys()].filter( |
| 580 | ( |
| 581 | tag, |
| 582 | ): tag is Exclude< |
| 583 | keyof typeof RPC_REQUIRED_SCOPES, |
| 584 | typeof WS_METHODS.gitPreparePullRequestThread |
| 585 | > => tag !== WS_METHODS.gitPreparePullRequestThread, |
| 586 | ), |
| 587 | ); |
| 588 | let handled = false; |
| 589 | const client = yield* RpcTest.makeClient(group).pipe( |
| 590 | Effect.provide( |
| 591 | Layer.mergeAll( |
| 592 | group.toLayerHandler(WS_METHODS.gitPreparePullRequestThread, () => |
| 593 | Effect.sync(() => { |
| 594 | handled = true; |
| 595 | }).pipe(Effect.andThen(Effect.never)), |
| 596 | ), |
| 597 | RpcAuthorization.layer([AuthSourceControlWriteScope]), |
| 598 | ), |
| 599 | ), |
| 600 | ); |
| 601 | expect( |
| 602 | yield* client[WS_METHODS.gitPreparePullRequestThread]({ |
| 603 | cwd: "/repo", |
| 604 | reference: "42", |
| 605 | mode: "worktree", |
| 606 | threadId: ThreadId.make("thread"), |
| 607 | }).pipe(Effect.flip), |
| 608 | ).toMatchObject({ requiredPermission: AuthOrchestrationOperateScope }); |
| 609 | expect(handled).toBe(false); |
| 610 | }), |
| 611 | ); |
| 612 | |
| 613 | describe("thread context authorization", () => { |
| 614 | const group = WsRpcGroup.omit( |
| 615 | ...[...WsRpcGroup.requests.keys()].filter( |
| 616 | ( |
| 617 | tag, |
| 618 | ): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, typeof WS_METHODS.gitRunStackedAction> => |
| 619 | tag !== WS_METHODS.gitRunStackedAction, |
| 620 | ), |
| 621 | ); |
| 622 | |
| 623 | it.effect.each(["commit", "create_pr", "commit_push_pr"] as const)( |
| 624 | "%s needs thread read permission only when generating a PR from a thread", |
| 625 | (action) => |
| 626 | Effect.gen(function* () { |
| 627 | const handled: string[] = []; |
| 628 | const handler = group.toLayerHandler(WS_METHODS.gitRunStackedAction, ({ actionId }) => |
| 629 | Stream.fromEffect(Effect.sync(() => handled.push(actionId))).pipe(Stream.drain), |
| 630 | ); |
| 631 | const client = yield* RpcTest.makeClient(group).pipe( |
| 632 | Effect.provide( |
| 633 | Layer.merge(handler, RpcAuthorization.layer([AuthSourceControlWriteScope])), |
| 634 | ), |
| 635 | ); |
| 636 | const input = { actionId: "without-thread", cwd: "/repo", action }; |
| 637 | const withThread = { ...input, actionId: "with-thread", threadId: ThreadId.make("thread") }; |
| 638 | yield* Stream.runDrain(client[WS_METHODS.gitRunStackedAction](input)); |
| 639 | if (action === "create_pr" || action === "commit_push_pr") { |
| 640 | expect( |
| 641 | yield* Stream.runDrain(client[WS_METHODS.gitRunStackedAction](withThread)).pipe( |
| 642 | Effect.flip, |
| 643 | ), |
| 644 | ).toMatchObject({ requiredPermission: AuthOrchestrationReadScope }); |
| 645 | expect(handled).toEqual(["without-thread"]); |
| 646 | } else { |
| 647 | yield* Stream.runDrain(client[WS_METHODS.gitRunStackedAction](withThread)); |
| 648 | expect(handled).toEqual(["without-thread", "with-thread"]); |
| 649 | } |
| 650 | const reader = yield* RpcTest.makeClient(group).pipe( |
| 651 | Effect.provide( |
| 652 | Layer.merge( |
| 653 | handler, |
| 654 | RpcAuthorization.layer([AuthSourceControlWriteScope, AuthOrchestrationReadScope]), |
| 655 | ), |
| 656 | ), |
| 657 | ); |
| 658 | yield* Stream.runDrain( |
| 659 | reader[WS_METHODS.gitRunStackedAction]({ ...withThread, actionId: "allowed" }), |
| 660 | ); |
| 661 | expect(handled.at(-1)).toBe("allowed"); |
| 662 | }), |
| 663 | ); |
| 664 | }); |
| 665 | |
| 666 | it.effect("separates host file URLs from readable attachment URLs", () => |
| 667 | Effect.gen(function* () { |
| 668 | const group = WsRpcGroup.omit( |
| 669 | ...[...WsRpcGroup.requests.keys()].filter( |
| 670 | ( |
| 671 | tag, |
| 672 | ): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, typeof WS_METHODS.assetsCreateUrl> => |
| 673 | tag !== WS_METHODS.assetsCreateUrl, |
| 674 | ), |
| 675 | ); |
| 676 | let handled = 0; |
| 677 | const client = yield* RpcTest.makeClient(group).pipe( |
| 678 | Effect.provide( |
| 679 | Layer.mergeAll( |
| 680 | group.toLayerHandler(WS_METHODS.assetsCreateUrl, () => |
| 681 | Effect.sync(() => { |
| 682 | handled++; |
| 683 | return { relativeUrl: "/api/assets/file", expiresAt: 1 }; |
| 684 | }), |
| 685 | ), |
| 686 | RpcAuthorization.layer([AuthOrchestrationReadScope]), |
| 687 | ), |
| 688 | ), |
| 689 | ); |
| 690 | yield* client[WS_METHODS.assetsCreateUrl]({ |
| 691 | resource: { _tag: "attachment", attachmentId: "image" }, |
| 692 | }); |
| 693 | yield* client[WS_METHODS.assetsCreateUrl]({ |
| 694 | resource: { _tag: "native-app-icon", app: { _tag: "app-id", appId: "com.apple.calculator" } }, |
| 695 | }); |
| 696 | for (const resource of [ |
| 697 | { _tag: "workspace-file", threadId: ThreadId.make("thread"), path: "file.txt" }, |
| 698 | { _tag: "media-file", threadId: ThreadId.make("thread"), path: "/repo/image.png" }, |
| 699 | { _tag: "draft-workspace-file", cwd: "/repo", path: "file.txt" }, |
| 700 | { _tag: "native-app-icon", app: { _tag: "app-path", path: "/private/Secret.app" } }, |
| 701 | ] as const) { |
| 702 | expect( |
| 703 | yield* client[WS_METHODS.assetsCreateUrl]({ resource }).pipe(Effect.flip), |
| 704 | ).toMatchObject({ |
| 705 | requiredScope: AuthOrchestrationReadScope, |
| 706 | requiredPermission: AuthFilesystemReadScope, |
| 707 | }); |
| 708 | } |
| 709 | expect(handled).toBe(2); |
| 710 | }), |
| 711 | ); |
| 712 | |
| 713 | it.effect("denies manual cleanup before the handler without settings permission", () => |
| 714 | Effect.gen(function* () { |
| 715 | const group = WsRpcGroup.omit( |
| 716 | ...[...WsRpcGroup.requests.keys()].filter( |
| 717 | ( |
| 718 | tag, |
| 719 | ): tag is Exclude< |
| 720 | keyof typeof RPC_REQUIRED_SCOPES, |
| 721 | typeof WS_METHODS.serverRunStorageCleanup |
| 722 | > => tag !== WS_METHODS.serverRunStorageCleanup, |
| 723 | ), |
| 724 | ); |
| 725 | let handled = false; |
| 726 | const client = yield* RpcTest.makeClient(group).pipe( |
| 727 | Effect.provide( |
| 728 | Layer.mergeAll( |
| 729 | group.toLayerHandler(WS_METHODS.serverRunStorageCleanup, () => |
| 730 | Effect.sync(() => { |
| 731 | handled = true; |
| 732 | return { |
| 733 | trigger: "manual" as const, |
| 734 | startedAt: "2026-10-09T12:00:00.000Z", |
| 735 | finishedAt: "2026-10-09T12:00:00.000Z", |
| 736 | entries: [], |
| 737 | counts: { removed: 0, kept: 0, failed: 0 }, |
| 738 | omittedCount: 0, |
| 739 | bytesFreed: 0, |
| 740 | }; |
| 741 | }), |
| 742 | ), |
| 743 | RpcAuthorization.layer([AuthOrchestrationReadScope]), |
| 744 | ), |
| 745 | ), |
| 746 | ); |
| 747 | expect(yield* client[WS_METHODS.serverRunStorageCleanup]({}).pipe(Effect.flip)).toMatchObject({ |
| 748 | requiredPermission: AuthSettingsWriteScope, |
| 749 | }); |
| 750 | expect(handled).toBe(false); |
| 751 | }), |
| 752 | ); |
| 753 | |
| 754 | it.effect("requires provider management to send /logout through chat", () => |
| 755 | Effect.gen(function* () { |
| 756 | const group = WsRpcGroup.omit( |
| 757 | ...[...WsRpcGroup.requests.keys()].filter( |
| 758 | ( |
| 759 | tag, |
| 760 | ): tag is Exclude< |
| 761 | keyof typeof RPC_REQUIRED_SCOPES, |
| 762 | typeof ORCHESTRATION_V2_WS_METHODS.dispatchCommand |
| 763 | > => tag !== ORCHESTRATION_V2_WS_METHODS.dispatchCommand, |
| 764 | ), |
| 765 | ); |
| 766 | const handled: string[] = []; |
| 767 | const handler = group.toLayerHandler(ORCHESTRATION_V2_WS_METHODS.dispatchCommand, (command) => |
| 768 | Effect.sync(() => { |
| 769 | handled.push(command.commandId); |
| 770 | return { sequence: 1 }; |
| 771 | }), |
| 772 | ); |
| 773 | const message = (commandId: string, text: string) => ({ |
| 774 | type: "message.dispatch" as const, |
| 775 | createdBy: "user" as const, |
| 776 | creationSource: "web" as const, |
| 777 | commandId: CommandId.make(commandId), |
| 778 | threadId: ThreadId.make("thread"), |
| 779 | messageId: MessageId.make(commandId), |
| 780 | text, |
| 781 | attachments: [], |
| 782 | dispatchMode: { type: "start_immediately" as const }, |
| 783 | }); |
| 784 | const edit = (commandId: string) => ({ |
| 785 | type: "queued-run.edit" as const, |
| 786 | commandId: CommandId.make(commandId), |
| 787 | threadId: ThreadId.make("thread"), |
| 788 | runId: RunId.make("run"), |
| 789 | text: "/logout", |
| 790 | }); |
| 791 | // With an attachment, `/logout` reaches the provider as a prompt, not a sign-out. |
| 792 | const attachments = [ |
| 793 | { type: "image" as const, id: "shot", name: "shot.png", mimeType: "image/png", sizeBytes: 1 }, |
| 794 | ]; |
| 795 | const operator = yield* RpcTest.makeClient(group).pipe( |
| 796 | Effect.provide(Layer.merge(handler, RpcAuthorization.layer([AuthOrchestrationOperateScope]))), |
| 797 | ); |
| 798 | const dispatch = operator[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]; |
| 799 | yield* dispatch(message("hello", "hello")); |
| 800 | yield* dispatch({ ...message("attached", "/logout"), attachments }); |
| 801 | yield* dispatch({ ...edit("attached-edit"), attachments }); |
| 802 | // This edit keeps the queued attachments; message intake decides it. |
| 803 | yield* dispatch(edit("kept-attachments-edit")); |
| 804 | for (const command of [message("logout", " /LOGOUT "), { ...edit("edit"), attachments: [] }]) { |
| 805 | expect(yield* dispatch(command).pipe(Effect.flip)).toMatchObject({ |
| 806 | requiredPermission: AuthProvidersManageScope, |
| 807 | }); |
| 808 | } |
| 809 | const manager = yield* RpcTest.makeClient(group).pipe( |
| 810 | Effect.provide( |
| 811 | Layer.merge( |
| 812 | handler, |
| 813 | RpcAuthorization.layer([AuthOrchestrationOperateScope, AuthProvidersManageScope]), |
| 814 | ), |
| 815 | ), |
| 816 | ); |
| 817 | yield* manager[ORCHESTRATION_V2_WS_METHODS.dispatchCommand](message("manager", "/logout")); |
| 818 | expect(handled).toEqual([ |
| 819 | "hello", |
| 820 | "attached", |
| 821 | "attached-edit", |
| 822 | "kept-attachments-edit", |
| 823 | "manager", |
| 824 | ]); |
| 825 | }).pipe(Effect.scoped), |
| 826 | ); |