// 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] // [--allow loopback,tailnet[,lan]] [--lan-plain-login] // // 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 { createReadStream } 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 { AuthStore, verifyPassword } from "./src/authStore.js"; import { LoginLimiter, clientKey } from "./src/loginLimiter.js"; import { PASSWORD_RULE } from "./src/passwordPolicy.js"; import { SessionSockets } from "./src/sessionSockets.js"; import { HOST_SESSION_COOKIE, SESSION_COOKIE, applySecurityHeaders, isHttps, pageHeaders, safeNext, sameOriginRequest, sessionCookieName, } from "./src/webSecurity.js"; import { PeerRelay, proxyDiscovery } from "./src/peerRelay.js"; import { accessPolicy, PLAIN_LAN_REFUSAL } from "./src/listenPolicy.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")); // Which networks may connect (src/listenPolicy.js). const access = accessPolicy(process.argv); 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 store = AUTH_DISABLED ? null : new AuthStore(AUTH_FILE); function loadAuth(fatal = true) { if (AUTH_DISABLED) return null; try { return store.load(); } 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(); // Every upgraded browser socket remembers its session token, so a sign-out, // a password change or an expiry closes it (on the event, and on a timer). const sockets = new SessionSockets(); function sweepSockets() { sockets.sweep((token) => { try { return store.check(token) !== null; } catch { return false; } }); } /** Reloads the credential; a new password or secret closes every socket. */ function refreshAuth() { const previous = auth; try { auth = loadAuth(false); } catch (error) { // Fail closed: an unusable credential file ends every open connection. sockets.sweep(() => false); throw error; } if (previous && auth && (previous.secret !== auth.secret || previous.generation !== auth.generation)) sweepSockets(); } const SWEEP_MS = Math.max(200, Number(process.env.PIPELINES_WEB_SWEEP_MS) || 5000); if (!AUTH_DISABLED) { setInterval(() => { try { refreshAuth(); sweepSockets(); } catch {} }, SWEEP_MS).unref(); } 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 ""; } /** The request's session token (the https cookie name first). */ function sessionValue(req) { return cookieValue(req, HOST_SESSION_COOKIE) || cookieValue(req, SESSION_COOKIE); } const SESSION_LONG_SECONDS = 30 * 24 * 60 * 60; const SESSION_SHORT_SECONDS = 12 * 60 * 60; /** The request's live session, or null. */ function session(req) { if (!auth) return null; try { return store.check(sessionValue(req)); } catch { return null; } } /** * The derived id the server keys step-up elevation by (AuthStore.sessionId), * or "" without a live session. Forwarded on /connect and POST /files only. */ function elevationSession(req) { if (!auth) return ""; try { return store.sessionId(sessionValue(req)); } catch { return ""; } } function isAuthed(req) { return !auth || session(req) !== null; } const peerRelay = new PeerRelay((req) => AUTH_DISABLED ? "test-auth-disabled" : sessionValue(req)); // Password checks are scrypt (32 MiB each); a few run at once and the rest // are answered 429 rather than queued without bound. const VERIFY_LIMIT = 4; let verifying = 0; async function passwordOk(candidate) { if (!auth) return true; const value = String(candidate ?? ""); if (value.length > 4096) return false; verifying++; try { return await verifyPassword(auth.hash, value); } finally { verifying--; } } 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 { token } = store.issue(remember ? SESSION_LONG_SECONDS : SESSION_SHORT_SECONDS); const bits = [`${sessionCookieName(req)}=${token}`, "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 forwards the scheme; then the cookie is // Secure and host-locked (__Host-). A plain-http LAN visit keeps the plain name. if (isHttps(req)) bits.push("Secure"); return bits.join("; "); } /** Set-Cookie values that clear both session cookie names. */ function clearedCookies(req) { const cleared = [`${SESSION_COOKIE}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0`]; if (isHttps(req)) cleared.push(`${HOST_SESSION_COOKIE}=; Path=/; HttpOnly; SameSite=Lax; Secure; Max-Age=0`); return cleared; } /** Ends one session: server-side revocation, its relayed peers and sockets. */ function signOut(req) { const token = sessionValue(req); peerRelay.forget(req); try { store.revoke(token); } catch {} sweepSockets(); } const limiter = new LoginLimiter(); function logoutPage() { return ` Pipelines · sign out

Sign out

Sign this browser out of Pipelines.

`; } function loginPage({ next = "/", error = "" } = {}) { return ` Pipelines · sign in

Pipelines

Sign in to reach this server.

${error ? `

${error}

` : ""}

Remembered sessions last 30 days; otherwise the sign-in ends when the browser closes.

`; } 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( 'Pipelines' + '' + "

The web app is not built yet

" + "

On the server, start Docker and run ./install --web, then reload.

", ); return; } // A deep URL such as /runs/440 would otherwise resolve the shell's relative // `./src/app.js` against /runs/. A root keeps the shell's own asset URLs // absolute without changing the address bar (and without editing index.html). const body = html.replace(//i, '\n '); 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; // An upload is gated by the session's elevation on the server. const upload = req.method === "POST" && (req.url ?? "").split("?")[0] === "/files"; try { headers = withServerToken(req.headers, undefined, upload ? elevationSession(req) : ""); } 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) => { applySecurityHeaders(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 (access.plainLanBlocked(req)) { res.writeHead(403, { "Content-Type": "text/plain; charset=utf-8", "Cache-Control": "no-store" }).end(PLAIN_LAN_REFUSAL); return; } if (!auth && (pathname === "/api/session" || pathname === "/api/sessions/revoke-all")) { res.writeHead(404, { "Content-Type": "application/json", "Cache-Control": "no-store" }).end('{"error":"sign-in is disabled"}'); 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, pageHeaders()).end(loginPage({ next: safeNext(next) })); return; } else if (pathname === "/login" && req.method === "POST") { // Login CSRF: a cross-site form must not sign the browser in to this server. if (!sameOriginRequest(req)) { req.resume(); res.writeHead(403, pageHeaders()).end(loginPage({ error: "Sign-in refused: the form came from another site." })); return; } const body = await readRequestBody(req).catch(() => ""); const fields = new URLSearchParams(body); const key = clientKey(req); const wait = limiter.blocked(key); if (wait > 0 || verifying >= VERIFY_LIMIT) { const seconds = Math.max(1, Math.ceil(wait / 1000)); res .writeHead(429, pageHeaders({ "Retry-After": String(seconds) })) .end(loginPage({ next: safeNext(fields.get("next")), error: `Too many attempts. Wait ${seconds} seconds.` })); return; } if (!(await passwordOk(fields.get("password")))) { limiter.fail(key); res .writeHead(401, pageHeaders()) .end(loginPage({ next: safeNext(fields.get("next")), error: "Wrong password." })); return; } limiter.succeed(key); 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" && req.method === "POST") { // Signing out is state-changing: POST from this origin only. if (!sameOriginRequest(req)) { req.resume(); res.writeHead(403, pageHeaders()).end(logoutPage()); return; } req.resume(); signOut(req); res.setHeader("Set-Cookie", clearedCookies(req)); res.writeHead(303, { Location: "/login" }).end(); return; } else if (pathname === "/logout") { // A link (or a cross-site page) only reaches a confirmation button. res.writeHead(200, pageHeaders()).end(logoutPage()); 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; } else if (pathname === "/api/session" && req.method === "GET") { const current = session(req); res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-store" }).end( JSON.stringify({ weak_password: auth.weak === true, password_rule: PASSWORD_RULE, expires_at: new Date(current.expires * 1000).toISOString(), }), ); return; } else if (pathname === "/api/sessions/revoke-all" && req.method === "POST") { req.resume(); if (!sameOriginRequest(req)) { res.writeHead(403, { "Content-Type": "application/json", "Cache-Control": "no-store" }).end('{"error":"cross-site request refused"}'); return; } peerRelay.forget(req); store.revokeAll(); sweepSockets(); res.setHeader("Set-Cookie", clearedCookies(req)); res.writeHead(200, { "Content-Type": "application/json", "Cache-Control": "no-store" }).end('{"revoked":true}'); 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 { // The server keys step-up elevation by this session, so a reconnect of // the same sign-in stays elevated. headers = withServerToken(req.headers, undefined, elevationSession(req)); } 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 (access.plainLanBlocked(req) || !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; } // Closed again when this session is signed out, revoked or expires. if (auth) sockets.track(clientSocket, sessionValue(req)); if (peerRelay.upgrade(req, clientSocket, clientHead)) return; if (pathname !== "/connect") { clientSocket.destroy(); return; } proxyUpgrade(req, clientSocket, clientHead); }); // Connections from a network the policy does not admit are dropped before // any HTTP is read. server.on("connection", (socket) => { if (!access.admits(socket)) socket.destroy(); }); const port = parsePort(process.argv); let host = flag(process.argv, "--host", "127.0.0.1"); // "::" also reaches a tailnet IPv6 address; a host without IPv6 falls back. function noIPv6(error) { if (host !== "::" || !["EAFNOSUPPORT", "EADDRNOTAVAIL"].includes(error.code)) throw error; host = "0.0.0.0"; server.listen(port, host); } server.once("error", noIPv6); server.listen(port, host, () => { server.off("error", noIPv6); const shown = host === "0.0.0.0" || host === "::" ? "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(access.describe()); console.log(`serving ${ROOT}${UPDATES ? `; app updates from ${UPDATES}` : ""}`); });