Hash the web password and revoke sessions server-side
Store a scrypt hash (migrating a plaintext password without changing it), sign sessions with a generation that ./password and --revoke-all rotate, make logout a same-origin POST that revokes the token, add sign out everywhere, and close proxied websockets whose session ends. Enforce a strength rule for new passwords, back off logins per client and server-wide, check Origin on login, tighten next, add security headers and a __Host- cookie over https.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
// 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);
|
||||
}
|
||||
|
||||
export function sessionCookieName(req) {
|
||||
return isHttps(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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user