mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 21:21:27 +01:00
fix(local): one resolver for the operator key and mode, a rotating read cookie, and fail closed on a blank setting
- The key sits next to the instance's config file, so run, the server and `paperclipai open` always use the same key. - `open` reads the deployment mode like the server (environment, then the selected config) and reports an unreadable config. - A blank PAPERCLIP_LOCAL_OPERATOR_CREDENTIAL now requires the credential. - The read cookie rotates and expires within a day; the board page renews it with its header token. Downloads the page starts with fetch no longer accept it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
539301b99a
commit
3d2cc4a3ec
10 files changed
+302
-112
No files matched your search
@@ -10,23 +10,36 @@ import {
|
||||
ensureLocalOperatorKey,
|
||||
localOperatorBrowserToken,
|
||||
parseLocalOperatorCredentialMode,
|
||||
resolveLocalOperatorKeyPath,
|
||||
localOperatorReadCookieValue,
|
||||
resolveLocalOperatorKeyPathForConfig,
|
||||
verifyLocalOperatorReadCookieValue,
|
||||
LOCAL_OPERATOR_READ_COOKIE_WINDOW_MS,
|
||||
} from "@paperclipai/shared/local-operator-credential";
|
||||
|
||||
describe("the local operator credential setting", () => {
|
||||
it("requires the credential for every value except an explicit off, so a typo keeps the protection", () => {
|
||||
for (const value of ["required", "1", "yes", "true", "on", "requird"]) {
|
||||
for (const value of ["required", "1", "yes", "true", "on", "requird", "", " "]) {
|
||||
expect(parseLocalOperatorCredentialMode(value)?.mode).toBe("required");
|
||||
}
|
||||
expect(parseLocalOperatorCredentialMode("requird")?.recognized).toBe(false);
|
||||
for (const value of ["off", "0", "no", "false"]) {
|
||||
expect(parseLocalOperatorCredentialMode(value)?.mode).toBe("off");
|
||||
}
|
||||
expect(parseLocalOperatorCredentialMode("")).toBeNull();
|
||||
expect(parseLocalOperatorCredentialMode(undefined)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("the read cookie", () => {
|
||||
it("stops working within two rotation windows, so a copied cookie expires", () => {
|
||||
const key = Buffer.alloc(32, 3);
|
||||
const issued = 1_000 * LOCAL_OPERATOR_READ_COOKIE_WINDOW_MS + 1;
|
||||
const value = localOperatorReadCookieValue(key, issued);
|
||||
expect(verifyLocalOperatorReadCookieValue(key, value, issued)).toBe(true);
|
||||
expect(verifyLocalOperatorReadCookieValue(key, value, issued + LOCAL_OPERATOR_READ_COOKIE_WINDOW_MS)).toBe(true);
|
||||
expect(verifyLocalOperatorReadCookieValue(key, value, issued + 2 * LOCAL_OPERATOR_READ_COOKIE_WINDOW_MS)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(process.platform === "win32")("an existing operator key file", () => {
|
||||
it("is made readable by its owner only", () => {
|
||||
const dir = mkdtempSync(path.join(os.tmpdir(), "paperclip-operator-key-"));
|
||||
@@ -51,6 +64,7 @@ describe.skipIf(process.platform !== "linux" && process.platform !== "darwin")(
|
||||
home: process.env.PAPERCLIP_HOME,
|
||||
instance: process.env.PAPERCLIP_INSTANCE_ID,
|
||||
mode: process.env.PAPERCLIP_LOCAL_OPERATOR_CREDENTIAL,
|
||||
config: process.env.PAPERCLIP_CONFIG,
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
@@ -58,7 +72,9 @@ describe.skipIf(process.platform !== "linux" && process.platform !== "darwin")(
|
||||
process.env.PAPERCLIP_HOME = home;
|
||||
process.env.PAPERCLIP_INSTANCE_ID = "default";
|
||||
process.env.PAPERCLIP_LOCAL_OPERATOR_CREDENTIAL = "required";
|
||||
const keyPath = resolveLocalOperatorKeyPath();
|
||||
// The server keeps the key next to the config file it runs from.
|
||||
process.env.PAPERCLIP_CONFIG = path.join(home, "instances", "default", "config.json");
|
||||
const keyPath = resolveLocalOperatorKeyPathForConfig(process.env.PAPERCLIP_CONFIG);
|
||||
mkdirSync(path.dirname(keyPath), { recursive: true });
|
||||
token = localOperatorBrowserToken(ensureLocalOperatorKey(keyPath));
|
||||
vi.resetModules();
|
||||
@@ -69,6 +85,7 @@ describe.skipIf(process.platform !== "linux" && process.platform !== "darwin")(
|
||||
["PAPERCLIP_HOME", saved.home],
|
||||
["PAPERCLIP_INSTANCE_ID", saved.instance],
|
||||
["PAPERCLIP_LOCAL_OPERATOR_CREDENTIAL", saved.mode],
|
||||
["PAPERCLIP_CONFIG", saved.config],
|
||||
] as const) {
|
||||
if (value === undefined) delete process.env[key];
|
||||
else process.env[key] = value;
|
||||
|
||||
@@ -190,13 +190,27 @@ 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/heartbeat-runs/r1/provider-trace/download",
|
||||
"/api/companies/c1/audit/agent-actions.csv",
|
||||
"/api/plugins/p1/bridge/stream/updates?companyId=c1",
|
||||
]) {
|
||||
const response = await fetch(`${base}${file}`, { headers: { cookie } });
|
||||
expect(await response.json()).toEqual({ type: "board", source: "local_implicit" });
|
||||
}
|
||||
// The page downloads these with `fetch`, so they need its header token.
|
||||
for (const file of ["/api/heartbeat-runs/r1/provider-trace/download", "/api/companies/c1/audit/agent-actions.csv"]) {
|
||||
const response = await fetch(`${base}${file}`, { headers: { cookie } });
|
||||
expect(await response.json()).toEqual({ type: "none", source: "none" });
|
||||
}
|
||||
});
|
||||
|
||||
it("renews the read cookie only for the board page that holds the header token", async () => {
|
||||
const { token } = await signIn();
|
||||
expect((await fetch(`${base}/api/local-operator/read-cookie`, { method: "POST" })).status).toBe(403);
|
||||
const renewed = await fetch(`${base}/api/local-operator/read-cookie`, {
|
||||
method: "POST",
|
||||
headers: { [LOCAL_OPERATOR_HEADER]: token },
|
||||
});
|
||||
expect(renewed.status).toBe(204);
|
||||
expect(renewed.headers.get("set-cookie")).toMatch(/HttpOnly/i);
|
||||
});
|
||||
|
||||
it("accepts each sign-in link only once", async () => {
|
||||
|
||||
@@ -12,28 +12,29 @@ import {
|
||||
localOperatorReadCookieValue,
|
||||
localOperatorSignInUrl,
|
||||
parseLocalOperatorCredentialMode,
|
||||
resolveLocalOperatorKeyPath,
|
||||
LOCAL_OPERATOR_READ_COOKIE_WINDOW_MS,
|
||||
resolveLocalOperatorKeyPathForConfig,
|
||||
verifyLocalOperatorBrowserToken,
|
||||
verifyLocalOperatorReadCookieValue,
|
||||
verifyLocalOperatorSignInLink,
|
||||
type LocalOperatorCredentialMode,
|
||||
} from "@paperclipai/shared/local-operator-credential";
|
||||
import { logger } from "./logger.js";
|
||||
import { resolvePaperclipConfigPath } from "../paths.js";
|
||||
|
||||
const COOKIE_MAX_AGE_MS = 400 * 24 * 60 * 60 * 1000;
|
||||
const COOKIE_MAX_AGE_MS = 2 * LOCAL_OPERATOR_READ_COOKIE_WINDOW_MS;
|
||||
const DEFAULT_MODE: LocalOperatorCredentialMode = "off";
|
||||
|
||||
/**
|
||||
* Requests the browser makes on its own, without the board page's script:
|
||||
* file downloads, images and plugin event streams. Only these accept the
|
||||
* read-only cookie.
|
||||
* images and file links, and plugin event streams. Only these accept the
|
||||
* read-only cookie. Downloads the page starts with `fetch` carry the header
|
||||
* token instead and are not listed.
|
||||
*/
|
||||
const DIRECT_BROWSER_READ_PATHS = [
|
||||
export const DIRECT_BROWSER_READ_PATHS = [
|
||||
/^\/api\/attachments\/[^/]+\/content$/,
|
||||
/^\/api\/assets\/[^/]+\/content$/,
|
||||
/^\/api\/issues\/[^/]+\/file-resources\/content$/,
|
||||
/^\/api\/heartbeat-runs\/[^/]+\/provider-trace\/download$/,
|
||||
/^\/api\/companies\/[^/]+\/audit\/agent-actions\.csv$/,
|
||||
/^\/api\/plugins\/[^/]+\/bridge\/stream\/[^/]+$/,
|
||||
];
|
||||
|
||||
@@ -76,7 +77,9 @@ export function createLocalOperatorCredential(options: LocalOperatorCredentialOp
|
||||
const usedNonces = new Map<string, number>();
|
||||
|
||||
function operatorKey(): Buffer {
|
||||
key ??= ensureLocalOperatorKey(options.keyPath ?? resolveLocalOperatorKeyPath());
|
||||
key ??= ensureLocalOperatorKey(
|
||||
options.keyPath ?? resolveLocalOperatorKeyPathForConfig(resolvePaperclipConfigPath()),
|
||||
);
|
||||
return key;
|
||||
}
|
||||
|
||||
@@ -124,8 +127,28 @@ export function createLocalOperatorCredential(options: LocalOperatorCredentialOp
|
||||
return true;
|
||||
}
|
||||
|
||||
function setReadCookie(res: Response) {
|
||||
const operator = operatorKey();
|
||||
res.cookie(localOperatorReadCookieName(operator), localOperatorReadCookieValue(operator), {
|
||||
httpOnly: true,
|
||||
sameSite: "strict",
|
||||
path: "/api",
|
||||
maxAge: COOKIE_MAX_AGE_MS,
|
||||
});
|
||||
}
|
||||
|
||||
function signInRoutes() {
|
||||
const router = Router();
|
||||
// The board page renews its rotating read cookie with its header token.
|
||||
router.post("/local-operator/read-cookie", (req: Request, res: Response) => {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
if (!hasOperatorHeader(req.headers)) {
|
||||
res.status(403).json({ error: "Local operator sign-in required" });
|
||||
return;
|
||||
}
|
||||
setReadCookie(res);
|
||||
res.status(204).end();
|
||||
});
|
||||
router.get("/local-operator/sign-in", (req: Request, res: Response) => {
|
||||
res.setHeader("Cache-Control", "no-store");
|
||||
res.setHeader("Referrer-Policy", "no-referrer");
|
||||
@@ -142,12 +165,7 @@ export function createLocalOperatorCredential(options: LocalOperatorCredentialOp
|
||||
return;
|
||||
}
|
||||
const operator = operatorKey();
|
||||
res.cookie(localOperatorReadCookieName(operator), localOperatorReadCookieValue(operator), {
|
||||
httpOnly: true,
|
||||
sameSite: "strict",
|
||||
path: "/api",
|
||||
maxAge: COOKIE_MAX_AGE_MS,
|
||||
});
|
||||
setReadCookie(res);
|
||||
// The fragment never reaches a server. The board page moves the token
|
||||
// into storage that only its own origin can read.
|
||||
const fragment = `${LOCAL_OPERATOR_FRAGMENT_KEY}=${encodeURIComponent(localOperatorBrowserToken(operator))}`;
|
||||
|
||||
Reference in new issue
Block a user