// The shared secret between this proxy and the local Pipelines server // (internal/servertoken, docs/notes/security-boundary.md). The installer writes // it to a private file beside web-auth.json; the server, when configured with // it, refuses any request without it. Only requests to the local upstream carry // it: the peer relay never forwards it to another machine. import { lstatSync, readFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; export const SERVER_TOKEN_HEADER = "x-pipelines-token"; // The proxy's opaque id of the signed-in web session (protocol.SessionHeader). export const SESSION_HEADER = "x-pipelines-session"; export function serverTokenPath(env = process.env) { if (env.PIPELINES_SERVER_TOKEN_FILE) return env.PIPELINES_SERVER_TOKEN_FILE; return join(env.XDG_CONFIG_HOME || join(homedir(), ".config"), "pipelines", "server-token"); } let cached = { key: "", token: "" }; // The token, re-read when the file changes; "" when no file exists. A file // another user could read or replace is refused (same rule as the Go reader). export function serverToken(path = serverTokenPath()) { let info; try { info = lstatSync(path); } catch { return ""; } if (!info.isFile()) throw new Error(`server token ${path} is not a regular file`); if (typeof process.getuid === "function" && (info.uid !== process.getuid() || info.mode & 0o077)) throw new Error(`server token ${path} must be owned by this user and private (chmod 600)`); const key = `${path}:${info.ino}:${info.mtimeMs}:${info.size}`; if (cached.key !== key) { const token = readFileSync(path, "utf8").trim(); if (token.length < 32 || /\s/.test(token)) throw new Error(`server token ${path} is malformed`); cached = { key, token }; } return cached.token; } // Headers for the local upstream: any client-supplied token or session id is // dropped and the proxy's own token is set, so the browser never chooses what // the server sees. session, when given, is the proxy's derived session id // (AuthStore.sessionId), set only where the server keys elevation by it. export function withServerToken(headers, token = serverToken(), session = "") { const result = { ...headers }; for (const name of Object.keys(result)) { const lower = name.toLowerCase(); if (lower === SERVER_TOKEN_HEADER || lower === SESSION_HEADER) delete result[name]; } if (token) result[SERVER_TOKEN_HEADER] = token; if (token && session) result[SESSION_HEADER] = session; return result; }