fix(local): send the operator token only to sockets that use it and redact it in terminal upgrade logs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-25 09:12:31 -06:00
1 parent 0b7e627224
commit f8ca98ece0
4 files changed
+17 -5

No files matched your search

+1 -1
View File
@@ -87,7 +87,7 @@ Paperclip now treats **bind** as a separate concern from auth:
- the process check identifies agent runs by their process tree and run markers. A process that an agent run detaches from its tree and starts with an empty environment carries neither, so on its own the check cannot stop it. Set `PAPERCLIP_LOCAL_OPERATOR_CREDENTIAL=required` to close that gap: implicit board access then also requires the operator credential. The process check still applies. Any value other than `off`, `false`, `0` or `no`, including an empty one, turns the credential on; the server logs a warning for a value it does not recognize.
- the operator credential is a key in `secrets/local-operator.key` next to the instance's config file (`<instance>/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 <id>` or `--config <path>` 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 the sockets it opens, including plugin sockets. 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.
- 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.
- 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/<id>/secrets`, or as a separate OS user, when you need that boundary to hold against a deliberate attempt.
@@ -1,4 +1,5 @@
import type { IncomingMessage, Server as HttpServer } from "node:http";
import { LOCAL_OPERATOR_SOCKET_PARAM } from "@paperclipai/shared/local-operator-constants";
import { createRequire } from "node:module";
import type { Duplex } from "node:stream";
import type { Db } from "@paperclipai/db";
@@ -183,8 +184,8 @@ function safeUpgradePath(rawUrl: string | undefined): string | undefined {
if (!rawUrl) return undefined;
try {
const url = new URL(rawUrl, "http://localhost");
if (url.searchParams.has("token")) {
url.searchParams.set("token", "[redacted]");
for (const secret of ["token", LOCAL_OPERATOR_SOCKET_PARAM]) {
if (url.searchParams.has(secret)) url.searchParams.set(secret, "[redacted]");
}
return `${url.pathname}${url.search}`;
} catch {
+4
View File
@@ -86,7 +86,11 @@ describe("sockets the board page opens", () => {
installLocalOperatorWebSocket(target);
new target.WebSocket(`ws://${window.location.host}/api/plugins/p1/api/socket`);
new target.WebSocket("wss://elsewhere.test/socket");
const terminal = `ws://${window.location.host}/api/environment-custom-image-setup-sessions/s1/terminal/ws`;
new target.WebSocket(terminal);
expect(new URL(opened[0]!).searchParams.get("operator")).toBe("v1.token");
expect(opened[1]).toBe("wss://elsewhere.test/socket");
// Sockets that authenticate another way never receive the token.
expect(opened[2]).toBe(terminal);
});
});
+9 -2
View File
@@ -65,9 +65,15 @@ export function installLocalOperatorFetch(target: typeof globalThis = globalThis
};
}
/** The only sockets that authenticate with the operator token. Others never receive it. */
const OPERATOR_SOCKET_PATHS = [
/^\/api\/companies\/[^/]+\/events\/ws$/,
/^\/api\/plugins\/[^/]+\/api\//,
];
/**
* Adds the operator token to every same-origin socket this page opens,
* including plugin sockets, since a browser cannot set headers on a WebSocket.
* Adds the operator token to the live-events and plugin sockets this page
* opens, since a browser cannot set headers on a WebSocket.
*/
export function installLocalOperatorWebSocket(target: typeof globalThis = globalThis): void {
const Original = target.WebSocket;
@@ -85,6 +91,7 @@ export function withLocalOperatorSocketToken(url: string): string {
if (!token) return url;
const parsed = new URL(url, window.location.href);
if (parsed.host !== browserReachableHost(window.location)) return url;
if (!OPERATOR_SOCKET_PATHS.some((pattern) => pattern.test(parsed.pathname))) return url;
parsed.searchParams.set(LOCAL_OPERATOR_SOCKET_PARAM, token);
return parsed.toString();
}