// Browser-facing hygiene for the proxy: request origin checks, the post-login // redirect target, the session cookie's name and the security headers. import { sameOriginUpgrade } from "./proxySecurity.js"; export const SESSION_COOKIE = "pipelines_session"; /** Over https the cookie is host-locked (Secure, Path=/, no Domain). */ export const HOST_SESSION_COOKIE = "__Host-pipelines_session"; /** Whether the browser reached the proxy over https (Tailscale serve). */ export function isHttps(req) { const forwarded = String(req.headers["x-forwarded-proto"] ?? "").split(",")[0].trim().toLowerCase(); return forwarded === "https" || Boolean(req.socket?.encrypted); } /** Whether the request comes from a browser (they send fetch metadata or an * Origin on a form POST); the native app, curl and peer proxies send neither. */ export function isBrowserRequest(req) { return Boolean(req.headers["sec-fetch-site"] || req.headers.origin); } /** Browsers over https get the host-locked `__Host-` cookie. Native clients * keep the plain name (still Secure over https): installed app builds from * before the `__Host-` cookie only read that name, and a native client has no * other site sharing its cookie jar, which is what `__Host-` guards against. */ export function sessionCookieName(req) { return isHttps(req) && isBrowserRequest(req) ? HOST_SESSION_COOKIE : SESSION_COOKIE; } /** * Whether a state-changing request comes from this origin. Browsers send * Origin on every POST; a native client (the app, curl, a peer proxy) sends * none and has no ambient cookies to abuse. `Origin: null` and a cross-site * Sec-Fetch-Site are refused. */ export function sameOriginRequest(req) { const origin = req.headers.origin; const site = req.headers["sec-fetch-site"]; if (site && !["same-origin", "none"].includes(String(site))) return false; if (origin === undefined) return true; return sameOriginUpgrade(origin, req.headers.host); } /** * The local path to return to after signing in: one "/" followed by anything * but "/" or "\" (which browsers treat as another host), no backslashes or * control characters anywhere, else "/". */ export function safeNext(value) { const next = String(value ?? ""); if (next.length > 2048 || !/^\/(?![/\\])/.test(next) || /[\\\u0000-\u001f\u007f]/.test(next)) return "/"; return next; } /** The proxy's own pages (sign-in, sign-out) are never framed and run no script. */ export const PAGE_POLICY = "default-src 'none'; img-src 'self'; style-src 'unsafe-inline'; form-action 'self'; " + "frame-ancestors 'none'; base-uri 'none'"; /** * Headers every response starts with. The app frames only its own documents * (sandboxed srcdoc HTML previews), so same-origin framing stays allowed; a * response that sets its own value (for example a sandboxing CSP) overrides * these at writeHead. HSTS only on https. */ export function applySecurityHeaders(req, res) { res.setHeader("X-Content-Type-Options", "nosniff"); res.setHeader("Referrer-Policy", "same-origin"); res.setHeader("X-Frame-Options", "SAMEORIGIN"); res.setHeader("Content-Security-Policy", "frame-ancestors 'self'"); if (isHttps(req)) res.setHeader("Strict-Transport-Security", "max-age=31536000"); } /** Marks a response as one of the proxy's own pages. */ export function pageHeaders(extra = {}) { return { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store", "Content-Security-Policy": PAGE_POLICY, "X-Frame-Options": "DENY", ...extra, }; }