diff --git a/server/src/__tests__/board-access-denial.test.ts b/server/src/__tests__/board-access-denial.test.ts index 04da1f29281..2d9f681769f 100644 --- a/server/src/__tests__/board-access-denial.test.ts +++ b/server/src/__tests__/board-access-denial.test.ts @@ -48,6 +48,11 @@ describe("board-only denial", () => { ["get", "/api/plugins/ui-contributions", "[Open plugin settings](//company/settings/instance/plugins)"], ["post", `/api/projects/${AGENT_ID}/access-members`, `[Open the project's access settings](//projects/${AGENT_ID}/configuration)`], ["delete", `/api/projects/${AGENT_ID}/access-members/m-1`, `[Open the project's access settings](//projects/${AGENT_ID}/configuration)`], + ["get", `/api/companies/${COMPANY_ID}/environments`, "[Open environment settings](//company/settings/instance/environments)"], + ["get", `/api/companies/${COMPANY_ID}/environments/capabilities`, "[Open environment settings](//company/settings/instance/environments)"], + ["post", `/api/environments/${AGENT_ID}/probe`, "[Open environment settings](//company/settings/instance/environments)"], + ["get", `/api/environment-leases/${AGENT_ID}`, "[Open environment settings](//company/settings/instance/environments)"], + ["get", `/api/environment-custom-image-setup-sessions/${AGENT_ID}`, "[Open environment settings](//company/settings/instance/environments)"], ] as const)("tells an agent %s %s is final and names the board page", async (method, path, link) => { const res = await request(createApp(agentActor))[method](path).expect(403); expect(res.body.error).toBe("Board access required"); @@ -122,6 +127,28 @@ describe("board-only denial", () => { } }); + it("tells an agent denied an environment route that testing and changing computers are board-only too", async () => { + const res = await request(createApp(agentActor, assertBoardOrgAccess)).get(`/api/companies/${COMPANY_ID}/environments/capabilities`).expect(403); + expect(res.body.remediation).toContain("Execution environments are board-only throughout"); + expect(res.body.remediation).toContain("testing (probing) them"); + const other = await request(createApp(agentActor)).get(`/api/companies/${COMPANY_ID}/project-environments`).expect(403); + expect(other.body.remediation).not.toContain("Execution environments"); + expect(other.body.remediation).not.toContain("/instance/environments"); + }); + + it("does not link environment settings while the operator hides them", async () => { + const previous = process.env.PAPERCLIP_HIDDEN_SETTINGS; + process.env.PAPERCLIP_HIDDEN_SETTINGS = "instance.environments"; + try { + const res = await request(createApp(agentActor)).get(`/api/companies/${COMPANY_ID}/environments`).expect(403); + expect(res.body.remediation).toContain("this denial is final"); + expect(res.body.remediation).not.toContain("/instance/environments"); + } finally { + if (previous === undefined) delete process.env.PAPERCLIP_HIDDEN_SETTINGS; + else process.env.PAPERCLIP_HIDDEN_SETTINGS = previous; + } + }); + it("applies through the org-access guard", async () => { const res = await request(createApp(agentActor, assertBoardOrgAccess)).delete(`/api/companies/${COMPANY_ID}`).expect(403); expect(res.body.code).toBe(BOARD_ACCESS_REQUIRED_CODE); diff --git a/server/src/__tests__/environment-routes.test.ts b/server/src/__tests__/environment-routes.test.ts index 14842a5661c..235621dc517 100644 --- a/server/src/__tests__/environment-routes.test.ts +++ b/server/src/__tests__/environment-routes.test.ts @@ -1526,6 +1526,24 @@ describe("environment routes", () => { expect(mockEnvironmentService.list).not.toHaveBeenCalled(); }); + it.each([ + ["get", "/api/companies/company-1/environments/capabilities"], + ["post", "/api/companies/company-1/environments"], + ["post", "/api/environments/env-1/probe"], + ["patch", "/api/environments/env-1"], + ["delete", "/api/environments/env-1"], + ["get", "/api/environments/env-1/leases"], + ] as const)("gives an agent denied %s %s the final environment settings guidance", async (method, path) => { + const app = createApp({ type: "agent", agentId: "agent-1", companyId: "company-1", source: "agent_key", runId: "run-1" }); + const res = await request(app)[method](path).send(method === "post" && path.endsWith("/environments") ? { name: "Agent computer", driver: "local", config: {} } : {}); + expect(res.status).toBe(403); + expect(res.body.error).toBe("Board access required"); + expect(res.body.remediation).toContain("this denial is final"); + expect(res.body.remediation).toContain("Execution environments are board-only throughout"); + expect(res.body.remediation).toContain("[Open environment settings](//company/settings/instance/environments)"); + expect(mockEnvironmentService.create).not.toHaveBeenCalled(); + }); + it("rejects agent detail reads for instance-scoped environments", async () => { mockEnvironmentService.getById.mockResolvedValue(createEnvironment()); mockAgentService.getById.mockResolvedValue({ @@ -1679,7 +1697,7 @@ describe("environment routes", () => { }); expect(res.status).toBe(403); - expect(res.body.error).toContain("board operators"); + expect(res.body.error).toBe("Board access required"); expect(mockEnvironmentService.create).not.toHaveBeenCalled(); }); @@ -2577,7 +2595,7 @@ describe("environment routes", () => { }); expect(res.status).toBe(403); - expect(res.body.error).toContain("board operators"); + expect(res.body.error).toBe("Board access required"); expect(mockEnvironmentService.create).not.toHaveBeenCalled(); }); diff --git a/server/src/routes/authz.ts b/server/src/routes/authz.ts index 4fab0967fa7..83dce412aa6 100644 --- a/server/src/routes/authz.ts +++ b/server/src/routes/authz.ts @@ -95,6 +95,13 @@ function pluginControlPage(path: string): [string, string] | null { const PLUGIN_ROUTE = /^\/plugins(?:\/|$)/; const PLUGIN_ADMINISTRATION_SCOPE = "Plugin management is board-only throughout: listing, installing, configuring (including choosing a secret for a plugin setting), enabling, disabling, upgrading and removing plugins have no agent route either."; +const ENVIRONMENT_ROUTE = /^\/(?:companies\/[^/]+\/)?environments(?:\/|$)|^\/environment-(?:leases|custom-image-setup-sessions)(?:\/|$)/; +const ENVIRONMENT_ADMINISTRATION_SCOPE = "Execution environments are board-only throughout: listing computers, reading which coding harnesses they support, testing (probing) them and changing them have no agent route either."; +function environmentControlPage(path: string): [string, string] | null { + if (!ENVIRONMENT_ROUTE.test(path) || getHiddenSettings().has("instance.environments")) return null; + return ["environment settings", "//company/settings/instance/environments"]; +} + function deniedApiPath(req: Request) { return (req.originalUrl ?? "").split("?")[0]!.replace(/^\/api(?=\/)/, ""); } @@ -103,6 +110,8 @@ function boardControlPage(req: Request) { const path = deniedApiPath(req); const pluginPage = pluginControlPage(path); if (pluginPage) return pluginPage; + const environmentPage = environmentControlPage(path); + if (environmentPage) return environmentPage; for (const [pattern, page] of BOARD_CONTROL_PAGES) { const match = pattern.exec(path); if (match) return page(match, req); @@ -117,7 +126,9 @@ function boardControlPage(req: Request) { export function boardAccessRequired(req: Request, controlPage?: BoardControlPage) { if (req.actor.type !== "agent") return forbidden("Board access required"); const page = controlPage ?? boardControlPage(req); - const scope = PLUGIN_ROUTE.test(deniedApiPath(req)) ? `${PLUGIN_ADMINISTRATION_SCOPE} ` : ""; + const deniedPath = deniedApiPath(req); + const scope = PLUGIN_ROUTE.test(deniedPath) ? `${PLUGIN_ADMINISTRATION_SCOPE} ` + : ENVIRONMENT_ROUTE.test(deniedPath) ? `${ENVIRONMENT_ADMINISTRATION_SCOPE} ` : ""; const where = page ? `They can do it in the Paperclip web app: link [Open ${page[0]}](${page[1]}), with this conversation's company prefix as .` : "They can do it in the Paperclip web app or with their own board credentials. Link a page only if you already know its route; do not search for one."; diff --git a/server/src/routes/environments.ts b/server/src/routes/environments.ts index ffdad6429df..506fe19ee8f 100644 --- a/server/src/routes/environments.ts +++ b/server/src/routes/environments.ts @@ -59,7 +59,7 @@ import { assertAgentsReadyForEnvironmentChange, } from "../services/plugin-environment-driver.js"; import { getConfiguredSecretProvider } from "../secrets/configured-provider.js"; -import { assertBoard, assertBoardOrgAccess, getActorInfo } from "./authz.js"; +import { assertBoard, assertBoardOrgAccess, boardAccessRequired, getActorInfo } from "./authz.js"; import type { PluginWorkerManager } from "../services/plugin-worker-manager.js"; import { environmentService } from "../services/environments.js"; import { SANDBOX_PLATFORM_PROBE_METADATA_KEY } from "../services/environment-custom-image-runtime.js"; @@ -388,6 +388,7 @@ export function environmentRoutes( } function assertCanAccessInstanceEnvironments(req: Request) { + if (req.actor.type === "agent") throw boardAccessRequired(req); if (req.actor.type !== "board") { throw forbidden("Instance environment management is restricted to board operators"); }