Files
pipelines/web/serve.mjs
T

495 lines
19 KiB
JavaScript

// Zero-dependency static file server + WebSocket proxy for the spike.
// node serve.mjs [--port 8090] [--host 0.0.0.0] [--pipelines 127.0.0.1:8080]
//
// Serves this directory, proxies GET/Upgrade /connect and GET/HEAD /artifacts/ to
// the Pipelines server, so the page, the WebSocket and artifact files share one
// origin. The API and its artifacts live on the server's port; the page is served
// from another, so artifact URLs resolved against the page would 404 without this.
//
// 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 because the
// server has no authentication and needs none.
//
// 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 } from "node:crypto";
import { 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";
const ROOT = resolve(fileURLToPath(new URL(".", import.meta.url)));
// ---------------------------------------------------------------------------
// 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() {
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.
}
console.error(
`web auth: refusing to start without ${AUTH_FILE} (or PIPELINES_WEB_AUTH=disabled for tests)`,
);
process.exit(1);
}
const auth = loadAuth();
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) {
return `${expires}.${createHmac("sha256", auth.secret).update(String(expires)).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 [expires, signature] = value.split(".");
if (!expires || !signature) return false;
const expected = createHmac("sha256", auth.secret).update(expires).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));
}
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",
};
// 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;
}
/** 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");
// 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) {
const headers = { ...req.headers };
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) => {
const pathname = (req.url ?? "/").split("?")[0];
if (auth) {
// Brand assets are public so the sign-in page itself can show the app icon.
if (
req.method === "GET" &&
(pathname === "/favicon.svg" || pathname === "/apple-touch-icon.png")
) {
// fall through to the static handler below
} else 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") {
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) {
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.startsWith("/artifacts/")) {
proxyArtifact(req, res);
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) {
const upstream = net.connect(targetPort, targetHost, () => {
const headers = { ...req.headers };
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) => {
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 (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) and /artifacts/ (http) -> ${targetHost}:${targetPort}`);
console.log(auth ? "auth: sign-in required" : "auth: DISABLED (tests)");
});