Files
pipelines/web/serve.mjs
T
alan 547d4d9822 Send the server token from the web proxy and limit peer relay addresses
The proxy adds the shared token to every upstream request and drops any
client copy. The peer relay accepts only tailnet, LAN, ts.net and discovered
hosts, resolves each name once and pins the checked address, and flags
ws:// endpoints with insecure_transport.
2026-11-10 00:59:39 +00:00

610 lines
24 KiB
JavaScript

// The authenticated web front door for Pipelines: the sign-in page, the Flutter
// web app, and a proxy to the Pipelines server, with no dependencies.
// node serve.mjs [--port 8090] [--host 0.0.0.0] [--pipelines 127.0.0.1:8080]
// [--root DIR] [--updates DIR]
//
// Serves the app (--root, default ../app/build/web) and proxies /connect,
// /artifacts/, /files and /chat-uploads to the Pipelines server, so the page, the
// WebSocket and artifact files share one origin. --updates serves the Android
// app's update feed at /updates/.
//
// Why the proxy exists: internal/server/websocket.go calls
// `websocket.Accept(w, r, nil)` (github.com/coder/websocket), which rejects an
// upgrade whose Origin host does not match the request Host — a browser would
// get 403 from a cross-origin page (a Node/curl client sends no Origin and is
// unaffected, which is why the smoke test passes). Proxying makes the page
// same-origin, and the Origin header is dropped on the way through (the server
// refuses any /connect carrying Origin). Every request to the server carries the
// shared server token (src/serverToken.js, docs/notes/security-boundary.md).
//
// The default host is 127.0.0.1. Use --host 0.0.0.0 only on a trusted LAN/VPN.
import { createServer, request } from "node:http";
import { createHmac, timingSafeEqual, randomBytes } from "node:crypto";
import { createReadStream, readFileSync } from "node:fs";
import { homedir } from "node:os";
import { readFile, stat } from "node:fs/promises";
import { extname, join, normalize, resolve, sep } from "node:path";
import { fileURLToPath } from "node:url";
import net from "node:net";
import { sameOriginUpgrade } from "./src/proxySecurity.js";
import { withServerToken } from "./src/serverToken.js";
import { PeerRelay, proxyDiscovery } from "./src/peerRelay.js";
// The proxy's own directory holds the sign-in page's icons.
const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
// The page root: `--root DIR` (the installer's Flutter web build), by default
// the checkout's app/build/web. `--updates DIR` serves the Android app's
// version.json and APK at /updates/, behind the same sign-in.
const ROOT = resolve(flag(process.argv, "--root", "") || join(HERE, "..", "app", "build", "web"));
const UPDATES = flag(process.argv, "--updates", "") ? resolve(flag(process.argv, "--updates", "")) : null;
// ---------------------------------------------------------------------------
// Access control
//
// The proxy can be reached from a tailnet, so it refuses to serve anything
// without a session cookie. The password and cookie secret live in a 0600 file
// outside the repository; tests may disable the gate explicitly.
// ---------------------------------------------------------------------------
const AUTH_DISABLED = process.env.PIPELINES_WEB_AUTH === "disabled";
const AUTH_FILE =
process.env.PIPELINES_WEB_AUTH_FILE ?? join(homedir(), ".config", "pipelines", "web-auth.json");
const SESSION_COOKIE = "pipelines_session";
function loadAuth(fatal = true) {
if (AUTH_DISABLED) return null;
try {
const parsed = JSON.parse(readFileSync(AUTH_FILE, "utf8"));
if (
typeof parsed.password === "string" &&
parsed.password.length >= 8 &&
typeof parsed.secret === "string" &&
parsed.secret.length >= 32
) {
return { password: parsed.password, secret: parsed.secret };
}
} catch {
// Fall through to the refusal below.
}
if (!fatal) throw new Error("Web authentication is temporarily unavailable");
console.error(`web auth: refusing to start without ${AUTH_FILE} (or PIPELINES_WEB_AUTH=disabled for tests)`);
process.exit(1);
}
let auth = loadAuth();
function refreshAuth() { auth = loadAuth(false); }
function cookieValue(req, name) {
for (const part of String(req.headers.cookie ?? "").split(";")) {
const i = part.indexOf("=");
if (i < 0) continue;
if (part.slice(0, i).trim() === name) {
try {
return decodeURIComponent(part.slice(i + 1).trim());
} catch {
return "";
}
}
}
return "";
}
function signSession(expires) {
const payload = `${expires}.${randomBytes(18).toString("base64url")}`;
return `${payload}.${createHmac("sha256", auth.secret).update(payload).digest("base64url")}`;
}
const SESSION_LONG_SECONDS = 30 * 24 * 60 * 60;
const SESSION_SHORT_SECONDS = 12 * 60 * 60;
function sessionToken(remember = false) {
const ttl = remember ? SESSION_LONG_SECONDS : SESSION_SHORT_SECONDS;
return signSession(Math.floor(Date.now() / 1000) + ttl);
}
function validSession(value) {
if (!auth || typeof value !== "string") return false;
const parts = value.split(".");
if (parts.length !== 2 && parts.length !== 3) return false;
const expires = parts[0], signature = parts.at(-1);
if (!/^\d+$/.test(expires) || !signature) return false;
const expected = createHmac("sha256", auth.secret).update(parts.slice(0, -1).join(".")).digest();
let given;
try {
given = Buffer.from(signature, "base64url");
} catch {
return false;
}
if (given.length !== expected.length || !timingSafeEqual(given, expected)) return false;
return Number(expires) > Math.floor(Date.now() / 1000);
}
function isAuthed(req) {
return !auth || validSession(cookieValue(req, SESSION_COOKIE));
}
const peerRelay = new PeerRelay((req) => AUTH_DISABLED ? "test-auth-disabled" : cookieValue(req, SESSION_COOKIE));
function passwordOk(candidate) {
if (!auth) return true;
const given = Buffer.from(String(candidate ?? ""));
const expected = Buffer.from(auth.password);
return given.length === expected.length && timingSafeEqual(given, expected);
}
function sessionCookie(req, remember = false) {
// Lax, not Strict: launching the installed app or a link from another app is
// a top-level navigation, and a Strict cookie is withheld on that first
// request, which made a remembered session look forgotten. Lax still keeps
// the cookie off cross-site subresource and POST requests.
const bits = [`${SESSION_COOKIE}=${sessionToken(remember)}`, "Path=/", "HttpOnly", "SameSite=Lax"];
// Remember me persists the cookie; otherwise it is a browser-session cookie
// (and the signed token itself expires after 12 hours).
if (remember) {
const expires = new Date(Date.now() + SESSION_LONG_SECONDS * 1000).toUTCString();
bits.push(`Max-Age=${SESSION_LONG_SECONDS}`, `Expires=${expires}`);
}
// Tailscale terminates TLS and usually forwards the scheme; when it does, the
// cookie is Secure. A direct plain-http LAN visit still works without it.
const forwarded = String(req.headers["x-forwarded-proto"] ?? "").split(",")[0].trim();
if (forwarded === "https") bits.push("Secure");
return bits.join("; ");
}
/** Small per-address brake on password guessing. */
const loginFailures = new Map();
function loginBlocked(address) {
const entry = loginFailures.get(address);
return Boolean(entry && entry.until > Date.now());
}
function noteLoginFailure(address) {
const now = Date.now();
const entry = loginFailures.get(address) ?? { count: 0, until: 0 };
entry.count += 1;
if (entry.count >= 8) {
entry.count = 0;
entry.until = now + 30_000;
}
loginFailures.set(address, entry);
}
function safeNext(value) {
const next = String(value ?? "");
return next.startsWith("/") && !next.startsWith("//") ? next : "/";
}
function loginPage({ next = "/", error = "" } = {}) {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noindex" />
<link rel="icon" href="/favicon.svg" type="image/svg+xml" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
<meta name="theme-color" content="#0a0a0b" />
<title>Pipelines · sign in</title>
<style>
:root { color-scheme: dark; }
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; display: grid; place-items: center;
background: #101013; color: #e8e8ea;
font: 14px/1.5 ui-sans-serif, -apple-system, "Segoe UI", sans-serif; }
main { width: min(360px, calc(100vw - 32px)); padding: 28px 24px 22px;
background: #17171b; border: 1px solid #2a2a31; border-radius: 16px; }
.mark { width: 40px; height: 40px; border-radius: 10px; display: block;
border: 1px solid #2a2a31; background: #101013; }
h1 { margin: 14px 0 4px; font-size: 20px; }
p { margin: 0 0 18px; color: #9a9aa2; font-size: 13px; }
label { display: block; font-size: 12px; color: #b8b8c0; margin-bottom: 6px; }
input { width: 100%; padding: 11px 12px; border-radius: 9px; border: 1px solid #33333c;
background: #101013; color: #e8e8ea; font: inherit; }
input:focus { outline: 2px solid #8fa8c8; outline-offset: 1px; }
.remember { display: flex; align-items: center; gap: 8px; margin-top: 12px;
color: #b8b8c0; font-size: 12px; cursor: pointer; }
.remember input { width: auto; margin: 0; accent-color: #8fa8c8; }
button { width: 100%; margin-top: 14px; padding: 11px 12px; border: 0; border-radius: 9px;
background: #8fa8c8; color: #101013; font: inherit; font-weight: 650; cursor: pointer; }
.error { margin: 10px 0 0; color: #e08a8a; font-size: 13px; }
.hint { margin: 16px 0 0; font-size: 11px; color: #77777f; }
</style>
</head>
<body>
<main>
<img class="mark" src="/favicon.svg" alt="" width="40" height="40" />
<h1>Pipelines</h1>
<p>Sign in to reach this server.</p>
<form method="post" action="/login">
<input type="hidden" name="next" value="${next.replace(/"/g, "&quot;")}" />
<label for="password">Password</label>
<input id="password" name="password" type="password" autocomplete="current-password" autofocus required />
<label class="remember"><input type="checkbox" name="remember" checked /> Remember me on this device</label>
${error ? `<p class="error">${error}</p>` : ""}
<button type="submit">Sign in</button>
</form>
<p class="hint">Remembered sessions last 30 days; otherwise the sign-in ends when the browser closes.</p>
</main>
</body>
</html>`;
}
function readRequestBody(req, limit = 4096) {
return new Promise((resolveBody, rejectBody) => {
let size = 0;
const chunks = [];
req.on("data", (chunk) => {
size += chunk.length;
if (size > limit) {
rejectBody(new Error("request too large"));
req.destroy();
return;
}
chunks.push(chunk);
});
req.on("end", () => resolveBody(Buffer.concat(chunks).toString("utf8")));
req.on("error", rejectBody);
});
}
const MIME = {
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".mjs": "text/javascript; charset=utf-8",
".css": "text/css; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".webmanifest": "application/manifest+json",
".png": "image/png",
".ico": "image/x-icon",
".map": "application/json; charset=utf-8",
".wasm": "application/wasm",
".otf": "font/otf",
".ttf": "font/ttf",
".woff": "font/woff",
".woff2": "font/woff2",
".bin": "application/octet-stream",
".frag": "application/octet-stream",
".apk": "application/vnd.android.package-archive",
};
// A path is asset-looking when its last segment ends in a known file type, and a
// page route when its first segment names one of the client's destinations. A
// stage key may itself end in an extension (`plan.js`): under a page prefix the
// route still serves the shell, while an asset-looking path elsewhere (`/nope.js`)
// stays an honest 404 and a real file (`/src/app.js`) is still served.
const ASSET_EXTENSIONS = new Set([...Object.keys(MIME), ".woff", ".woff2", ".txt"]);
const PAGE_PREFIXES = new Set(["pipelines", "runs", "chat", "settings", "templates"]);
/** The decoded first path segment, or "" for the root. */
function firstSegment(pathname) {
const raw = pathname.split("/").filter(Boolean)[0] ?? "";
try {
return decodeURIComponent(raw);
} catch {
return raw;
}
}
function flag(argv, name, fallback) {
const i = argv.indexOf(name);
if (i !== -1 && argv[i + 1]) return argv[i + 1];
return fallback;
}
function parsePort(argv) {
return Number(flag(argv, "--port", process.env.PORT ?? 8090));
}
/**
* Resolve a request path to a file inside ROOT. Returns null when the percent
* escapes are malformed (a guarded decode, so a bad `%` cannot throw out of the
* request handler) and false when the path escapes ROOT; the caller treats the
* first as an unknown page route and the second as forbidden.
*/
function resolvePath(urlPath) {
let decoded;
try {
decoded = decodeURIComponent(urlPath.split("?")[0]);
} catch {
return null;
}
const rel = normalize(decoded).replace(/^([/\\])+/, "");
const target = resolve(join(ROOT, rel || "index.html"));
if (target !== ROOT && !target.startsWith(ROOT + sep)) return false;
return target;
}
/** Icons and the web manifest, which need no sign-in. Fixed names only. */
const PUBLIC_ASSET =
/^\/(favicon\.(svg|png|ico)|apple-touch-icon\.png|manifest\.json|icons\/[A-Za-z0-9-]+\.png)$/;
function isPublicAsset(pathname) {
return PUBLIC_ASSET.test(pathname);
}
/** One file of the Android app's update feed, or 404. */
async function serveUpdate(req, res, pathname) {
let name;
try {
name = decodeURIComponent(pathname.slice("/updates/".length));
} catch {
name = "";
}
const path = resolve(join(UPDATES, name));
const info = name && path.startsWith(UPDATES + sep) ? await stat(path).catch(() => null) : null;
if (!info?.isFile()) {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
return;
}
res.writeHead(200, {
"Content-Type": MIME[extname(path)] ?? "application/octet-stream",
"Content-Length": info.size,
"Cache-Control": "no-store",
});
if (req.method === "HEAD") {
res.end();
return;
}
createReadStream(path).pipe(res);
}
/** Send index.html (the page shell). The router reads the real URL in the browser. */
async function serveIndex(res) {
const html = await readFile(join(ROOT, "index.html"), "utf8").catch(() => null);
if (html === null) {
// The installer builds the app in Docker; without it there is no page yet.
res.writeHead(503, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
res.end(
'<!doctype html><meta charset="utf-8"><title>Pipelines</title>' +
'<body style="font-family:system-ui;padding:2rem;background:#101013;color:#cfd9e6">' +
"<h1>The web app is not built yet</h1>" +
"<p>On the server, start Docker and run <code>./install --web</code>, then reload.</p>",
);
return;
}
// A deep URL such as /runs/440 would otherwise resolve the shell's relative
// `./src/app.js` against /runs/. A root <base> keeps the shell's own asset URLs
// absolute without changing the address bar (and without editing index.html).
const body = html.replace(/<head>/i, '<head>\n <base href="/" />');
res.writeHead(200, {
"Content-Type": MIME[".html"],
"Cache-Control": "no-store",
});
res.end(body);
}
/**
* Proxy one artifact request to the Pipelines server. Artifact URLs are resolved
* against the configured server, which in a proxied deployment is the page's own
* origin, so without this the gallery's thumbnails, the viewer and the session
* export would all 404 while the WebSocket worked.
*/
function proxyArtifact(req, res) {
let headers;
try {
headers = withServerToken(req.headers);
} catch {
res.writeHead(503, { "Content-Type": "text/plain; charset=utf-8" }).end("Server token unavailable");
return;
}
delete headers.origin;
headers.host = `${targetHost}:${targetPort}`;
const upstream = request(
{ host: targetHost, port: targetPort, method: req.method, path: req.url, headers },
(reply) => {
res.writeHead(reply.statusCode ?? 502, reply.headers);
reply.pipe(res);
},
);
upstream.on("error", () => {
if (!res.headersSent) res.writeHead(502, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Artifact unavailable");
});
req.pipe(upstream);
}
const server = createServer(async (req, res) => {
try { refreshAuth(); } catch {
res.writeHead(503, { "Content-Type": "text/plain", "Cache-Control": "no-store" }).end("Web authentication unavailable");
return;
}
const pathname = (req.url ?? "/").split("?")[0];
// Icons and the web manifest are public: the sign-in page shows the icon,
// and browsers and app installers fetch them without the session cookie.
if ((req.method === "GET" || req.method === "HEAD") && isPublicAsset(pathname)) {
const name = pathname.slice(1);
const body =
(await readFile(join(ROOT, name)).catch(() => null)) ??
(await readFile(join(HERE, name)).catch(() => null));
if (body) {
res.writeHead(200, { "Content-Type": MIME[extname(pathname)], "Cache-Control": "no-cache" });
res.end(req.method === "HEAD" ? undefined : body);
return;
}
}
if (auth) {
if (pathname === "/login" && req.method === "GET") {
if (isAuthed(req)) {
res.writeHead(302, { Location: "/" }).end();
return;
}
const next = new URL(req.url ?? "/login", "http://localhost").searchParams.get("next") ?? "/";
res
.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" })
.end(loginPage({ next: safeNext(next) }));
return;
} else if (pathname === "/login" && req.method === "POST") {
const body = await readRequestBody(req).catch(() => "");
const fields = new URLSearchParams(body);
const address = req.socket.remoteAddress ?? "";
if (loginBlocked(address)) {
res
.writeHead(429, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" })
.end(loginPage({ error: "Too many attempts. Wait 30 seconds." }));
return;
}
if (!passwordOk(fields.get("password"))) {
noteLoginFailure(address);
res
.writeHead(401, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" })
.end(loginPage({ next: safeNext(fields.get("next")), error: "Wrong password." }));
return;
}
const remember = fields.get("remember") === "on";
res.setHeader("Set-Cookie", sessionCookie(req, remember));
res.writeHead(303, { Location: safeNext(fields.get("next")) }).end();
return;
} else if (pathname === "/logout") {
peerRelay.forget(req);
res.setHeader("Set-Cookie", `${SESSION_COOKIE}=; Path=/; HttpOnly; SameSite=Strict; Max-Age=0`);
res.writeHead(303, { Location: "/login" }).end();
return;
} else if (!isAuthed(req)) {
const wantsHtml = String(req.headers.accept ?? "").includes("text/html");
if (req.method === "GET" && wantsHtml && !pathname.startsWith("/api/") && !pathname.startsWith("/peers/")) {
res.writeHead(302, { Location: `/login?next=${encodeURIComponent(req.url ?? "/")}` }).end();
return;
}
res
.writeHead(401, { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store" })
.end("Sign in required\n");
return;
}
}
if (pathname === "/api/discovery" && req.method === "GET") {
await proxyDiscovery(req, res, targetHost, targetPort);
return;
}
if (await peerRelay.api(req, res)) return;
if (peerRelay.http(req, res)) return;
if (pathname.startsWith("/artifacts/") || pathname === "/files") {
proxyArtifact(req, res);
return;
}
// Chat files go to the server as their own request, outside the websocket
// frame limit; the server refuses a request without its custom name header.
if (pathname === "/chat-uploads" && req.method === "POST") {
proxyArtifact(req, res);
return;
}
if (UPDATES && pathname.startsWith("/updates/") && (req.method === "GET" || req.method === "HEAD")) {
await serveUpdate(req, res, pathname);
return;
}
if (pathname === "/favicon.ico") {
res.writeHead(204).end(); // no icon; keeps the console free of a 404
return;
}
// SPA fallback: a page route (`/runs/440`, `/pipelines/24/5/stage/plan.js`) or a
// non-asset GET serves the shell, so a hard navigation deep-links and the client
// router opens the destination. A path that looks like an asset outside a page
// prefix (`.js`, `.css`, `.png`, …) stays an honest 404, and a malformed escape
// is handled like any other unknown path rather than rejecting the handler.
const assetLike = ASSET_EXTENSIONS.has(extname(pathname).toLowerCase());
const pageLike = PAGE_PREFIXES.has(firstSegment(pathname));
const serveShell = pageLike || !assetLike;
const target = resolvePath(req.url ?? "/");
if (target === null) {
// A malformed percent escape is an unknown page route, not a handler throw.
if (serveShell) {
await serveIndex(res).catch(() => {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
});
return;
}
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
return;
}
if (!target) {
res.writeHead(403).end("Forbidden");
return;
}
try {
let path = target;
const info = await stat(path).catch(() => null);
if (info?.isDirectory()) path = join(path, "index.html");
const body = await readFile(path);
res.writeHead(200, {
"Content-Type": MIME[extname(path)] ?? "application/octet-stream",
"Cache-Control": "no-store",
});
res.end(body);
} catch {
if (serveShell) {
await serveIndex(res).catch(() => {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
});
return;
}
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }).end("Not found");
}
});
// ---------------------------------------------------------------------------
// WebSocket proxy for /connect
// ---------------------------------------------------------------------------
const pipelinesTarget = flag(
process.argv,
"--pipelines",
process.env.PIPELINES_TARGET ?? "127.0.0.1:8080",
);
const targetPort = Number(pipelinesTarget.split(":").at(-1));
const targetHost = pipelinesTarget.slice(0, pipelinesTarget.lastIndexOf(":")) || "127.0.0.1";
function proxyUpgrade(req, clientSocket, clientHead) {
let headers;
try {
headers = withServerToken(req.headers);
} catch {
clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n");
return;
}
const upstream = net.connect(targetPort, targetHost, () => {
delete headers.origin; // see the header comment: the server forbids a mismatched Origin
headers.host = `${targetHost}:${targetPort}`;
const block = Object.entries(headers)
.filter(([, v]) => v != null)
.map(([k, v]) => `${k}: ${Array.isArray(v) ? v.join(", ") : v}`)
.join("\r\n");
upstream.write(`GET ${req.url} HTTP/1.1\r\n${block}\r\n\r\n`);
if (clientHead?.length) upstream.write(clientHead);
upstream.pipe(clientSocket);
clientSocket.pipe(upstream);
});
upstream.on("error", () => clientSocket.destroy());
clientSocket.on("error", () => upstream.destroy());
}
server.on("upgrade", (req, clientSocket, clientHead) => {
try { refreshAuth(); } catch {
clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n");
return;
}
const pathname = (req.url ?? "").split("?")[0];
if (!sameOriginUpgrade(req.headers.origin, req.headers.host)) {
clientSocket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
clientSocket.destroy();
return;
}
if (auth && !isAuthed(req)) {
clientSocket.write("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
clientSocket.destroy();
return;
}
if (peerRelay.upgrade(req, clientSocket, clientHead)) return;
if (pathname !== "/connect") {
clientSocket.destroy();
return;
}
proxyUpgrade(req, clientSocket, clientHead);
});
const port = parsePort(process.argv);
const host = flag(process.argv, "--host", "127.0.0.1");
server.listen(port, host, () => {
const shown = host === "0.0.0.0" ? "127.0.0.1" : host;
console.log(`web spike: http://${shown}:${port}/ (bound ${host}:${port})`);
console.log(`proxying /connect (ws), /artifacts/ and /files (http) -> ${targetHost}:${targetPort}`);
console.log(auth ? "auth: sign-in required" : "auth: DISABLED (tests)");
console.log(`serving ${ROOT}${UPDATES ? `; app updates from ${UPDATES}` : ""}`);
});