diff --git a/cli/src/__tests__/open.test.ts b/cli/src/__tests__/open.test.ts index af1602e2cf9..eb2294b9a18 100644 --- a/cli/src/__tests__/open.test.ts +++ b/cli/src/__tests__/open.test.ts @@ -114,6 +114,18 @@ describe("paperclipai open", () => { expect(resolveRunInstanceId({ config: "/elsewhere/config.json" })).toBe("default"); }); + it("loads the selected instance's .env, not another instance's, before choosing the mode", async () => { + const { loadPaperclipEnvFile } = await import("../config/env.js"); + const { resolveLocalBoardConfigPath } = await import("../commands/open.js"); + writeInstanceConfig("default", "local_trusted"); + writeFileSync(path.join(home, "instances", "default", ".env"), "PAPERCLIP_DEPLOYMENT_MODE=authenticated\n"); + const fooConfig = writeInstanceConfig("foo", "local_trusted"); + expect(resolveLocalBoardConfigPath({ instance: "foo" })).toBe(fooConfig); + loadPaperclipEnvFile(resolveLocalBoardConfigPath({ instance: "foo" })); + expect(process.env.PAPERCLIP_DEPLOYMENT_MODE).toBeUndefined(); + expect(resolveLocalBoardTarget({ instance: "foo" }).deploymentMode).toBe("local_trusted"); + }); + it("follows the server's deployment mode override from the environment", () => { writeInstanceConfig("default", "local_trusted"); process.env.PAPERCLIP_DEPLOYMENT_MODE = "authenticated"; diff --git a/cli/src/commands/open.ts b/cli/src/commands/open.ts index 20bae353197..7a019ea3d97 100644 --- a/cli/src/commands/open.ts +++ b/cli/src/commands/open.ts @@ -62,11 +62,15 @@ export function resolveConfigDeploymentMode(configPath: string): DeploymentMode * config: `--config`, else `--instance`, else the environment and working * directory. The key sits next to that config file. */ +export function resolveLocalBoardConfigPath(opts: Pick = {}): string { + if (opts.config?.trim()) return path.resolve(opts.config); + if (opts.instance?.trim()) return resolveDefaultConfigPath(resolvePaperclipInstanceId(opts.instance)); + return resolveConfigPath(); +} + export function resolveLocalBoardTarget(opts: Pick = {}): LocalBoardTarget { const instanceOverride = opts.instance?.trim() ? resolvePaperclipInstanceId(opts.instance) : null; - const configPath = opts.config?.trim() - ? path.resolve(opts.config) - : instanceOverride ? resolveDefaultConfigPath(instanceOverride) : resolveConfigPath(); + const configPath = resolveLocalBoardConfigPath(opts); const instanceId = instanceOverride ?? instanceIdForConfigPath(configPath) ?? resolvePaperclipInstanceId(); return { configPath, diff --git a/cli/src/index.ts b/cli/src/index.ts index 17cb4d28526..ecb1028f934 100644 --- a/cli/src/index.ts +++ b/cli/src/index.ts @@ -33,7 +33,7 @@ import { initTelemetryFromConfigFile, flushTelemetry } from "./telemetry.js"; import { registerWorktreeCommands } from "./commands/worktree.js"; import { registerPluginCommands } from "./commands/client/plugin.js"; import { registerClientAuthCommands } from "./commands/client/auth.js"; -import { registerOpenCommand } from "./commands/open.js"; +import { registerOpenCommand, resolveLocalBoardConfigPath } from "./commands/open.js"; import { registerConnectCommand } from "./commands/client/connect.js"; import { registerTokenCommands } from "./commands/client/token.js"; import { registerPromptCommands } from "./commands/client/prompt.js"; @@ -133,7 +133,11 @@ program.hook("preAction", async (_thisCommand, actionCommand) => { hasConfigOption: optionNames.has("config"), hasContextOption: optionNames.has("context"), }); - loadPaperclipEnvFile(options.config); + // `open --instance ` must load that instance's .env, not the default one, + // so its deployment mode decides whether the board opens with a sign-in link. + loadPaperclipEnvFile(actionCommand.name() === "open" + ? resolveLocalBoardConfigPath(options as { config?: string; instance?: string }) + : options.config); if (actionCommand.name() === "test-drive") { assertTestDriveDatabaseIsolation(options.config); } diff --git a/doc/DEPLOYMENT-MODES.md b/doc/DEPLOYMENT-MODES.md index 7f62835d6da..fc3a74e0bfd 100644 --- a/doc/DEPLOYMENT-MODES.md +++ b/doc/DEPLOYMENT-MODES.md @@ -88,7 +88,7 @@ Paperclip now treats **bind** as a separate concern from auth: - the operator credential is a key in `secrets/local-operator.key` next to the instance's config file (`/secrets/local-operator.key` for the standard layout). The server and `paperclipai open` find the same file whether the instance was chosen with `--instance`, `--config` or the environment. The server creates it with owner-only permissions on first use and restores those permissions on an existing key. Agent environments never receive it. - browsers sign in through a one-time link that expires after 10 minutes. `paperclipai onboard`, and any server started with `PAPERCLIP_OPEN_ON_LISTEN=true`, open the board with that link. `paperclipai open` opens a fresh link for the running server, or prints it with `--no-browser`. Use `--instance ` or `--config ` to pick a named instance. A browser that is not signed in sees a screen that asks the operator to run `paperclipai open`. - the link gives the board page a token through the URL fragment. The page keeps the token in its own origin's storage and sends it as a header on its own requests. A browser cannot set headers on a WebSocket, so the page adds the token as a query parameter to its live-events and plugin sockets only. The server accepts that parameter on socket upgrades only and never passes it to plugin code. Pages served by other local servers cannot read it, even on the same host name. - - browsers send cookies to every port on a host, so a cookie never grants board actions. The sign-in also sets an `HttpOnly`, `SameSite=Strict` cookie, named per instance, that only lets the browser read attachments, assets and workspace files it loads directly, and plugin event streams. Any local server the browser visits can receive that cookie. Its value rotates and stops working within a day, and the board page renews it with its header token. + - browsers send cookies to every port on a host, so a cookie never grants board actions. The sign-in also sets an `HttpOnly`, `SameSite=Strict` cookie, named per instance, that only lets the browser read attachments, assets, workspace files and agent instruction files it loads directly, and plugin event streams. Any local server the browser visits can receive that cookie. Its value rotates and stops working within a day, and the board page renews it with its header token. - the CLI and local assistants use a board key. Run `paperclipai auth login`, approve the request in the signed-in browser, and the CLI stores the key. - agent-run code running as the same OS user can still read the operator's files. Run agents with a filesystem sandbox that hides `~/.paperclip/instances//secrets`, or as a separate OS user, when you need that boundary to hold against a deliberate attempt. diff --git a/server/src/__tests__/local-operator-credential.test.ts b/server/src/__tests__/local-operator-credential.test.ts index 03721126a3c..4ce6976b366 100644 --- a/server/src/__tests__/local-operator-credential.test.ts +++ b/server/src/__tests__/local-operator-credential.test.ts @@ -42,6 +42,8 @@ function startServer(operator: LocalOperatorCredential): Promise<{ server: Serve app.post("/api/whoami", whoami); app.get("/api/attachments/:id/content", whoami); app.get("/api/issues/:id/file-resources/content", whoami); + app.get("/api/agents/:id/instructions-bundle/file", whoami); + app.put("/api/agents/:id/instructions-bundle/file", whoami); app.get("/api/assets/:id/content", whoami); app.get("/api/heartbeat-runs/:id/provider-trace/download", whoami); app.get("/api/companies/:id/audit/agent-actions.csv", whoami); @@ -184,6 +186,8 @@ describe.skipIf(!supported)("operator browser sign-in", () => { } const change = await fetch(`${base}/api/attachments/a1/content`, { method: "POST", headers: { cookie } }); expect(await change.json()).toEqual({ type: "none", source: "none" }); + const write = await fetch(`${base}/api/agents/a1/instructions-bundle/file`, { method: "PUT", headers: { cookie } }); + expect(await write.json()).toEqual({ type: "none", source: "none" }); }); it("lets the browser load a file it was shown with the cookie alone", async () => { @@ -192,6 +196,7 @@ describe.skipIf(!supported)("operator browser sign-in", () => { "/api/attachments/a1/content", "/api/assets/s1/content", "/api/issues/i1/file-resources/content?path=a.txt", + "/api/agents/a1/instructions-bundle/file?path=AGENTS.md", "/api/plugins/p1/bridge/stream/updates?companyId=c1", ]) { const response = await fetch(`${base}${file}`, { headers: { cookie } }); diff --git a/server/src/middleware/local-operator-credential.ts b/server/src/middleware/local-operator-credential.ts index c147bd31341..94ffa687807 100644 --- a/server/src/middleware/local-operator-credential.ts +++ b/server/src/middleware/local-operator-credential.ts @@ -36,6 +36,7 @@ export const DIRECT_BROWSER_READ_PATHS = [ /^\/api\/attachments\/[^/]+\/content$/, /^\/api\/assets\/[^/]+\/content$/, /^\/api\/issues\/[^/]+\/file-resources\/content$/, + /^\/api\/agents\/[^/]+\/instructions-bundle\/file$/, /^\/api\/plugins\/[^/]+\/bridge\/stream\/[^/]+$/, ];