Harden the login flow: new session at sign-in, signed ID tokens, same-origin requests, headers
From an audit of the Authentik login, run against a fake OIDC provider: - Session fixation: the session id wasn't changed at sign-in, so one planted beforehand (from another page on the domain) became signed in as the victim. It is regenerated now. - Requests from another origin: SameSite=Lax doesn't stop another app on the same parent domain, and a body-less POST (stop a guest, rotate a token) needs no preflight. Every POST/PUT/PATCH/DELETE, and GET /auth/logout, is refused when Sec-Fetch-Site (or, for older browsers, Origin) says it came from elsewhere. The agent endpoint is exempt: it uses a token. - ID token signature: openid-client checks a token's claims but, as OIDC allows for a token from the token endpoint, not its signature, so the login rested on HTTPS to Authentik alone. The signature is now verified against the provider's published keys (jose, now a declared dependency). - SESSION_SECRET: production refuses to start without one (the fallback is in the source code) and warns when it is the .env.example placeholder or short. - Response headers: frame-ancestors 'none', X-Frame-Options, nosniff, Referrer-Policy; the framework name is no longer sent. - A failed sign-in shows a plain page and is audited (session / login_failed, with address and reason, no tokens); sign-in is limited to 20 attempts a minute per address so the audit log and session store can't be flooded. - A failed OIDC discovery (provider down at the first sign-in) was remembered until restart; it is retried now. Tests run the real app against the fake provider; switching each fix off in turn makes the matching tests fail. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
73b86d88de
commit
2dac273b1d
12 files changed
+681
-18
No files matched your search
@@ -0,0 +1,32 @@
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
|
||||
/**
|
||||
* A small fixed-window limiter, per client address. It exists for the sign-in endpoints, which anyone can reach without an
|
||||
* account: each visit to /auth/login starts a session, and each failed callback is written to the audit log, so without a
|
||||
* limit one script could fill the disk and the log. Kept in memory — a restart clears it, which is fine for this.
|
||||
*/
|
||||
export function rateLimit(options: { windowMs: number; max: number; message: string }) {
|
||||
const windows = new Map<string, { count: number; resetAt: number }>();
|
||||
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
const now = Date.now();
|
||||
const key = req.ip ?? "unknown";
|
||||
|
||||
if (windows.size > 10_000) {
|
||||
for (const [k, w] of windows) if (w.resetAt <= now) windows.delete(k);
|
||||
}
|
||||
|
||||
let entry = windows.get(key);
|
||||
if (!entry || entry.resetAt <= now) {
|
||||
entry = { count: 0, resetAt: now + options.windowMs };
|
||||
windows.set(key, entry);
|
||||
}
|
||||
entry.count++;
|
||||
|
||||
if (entry.count > options.max) {
|
||||
res.setHeader("Retry-After", String(Math.ceil((entry.resetAt - now) / 1000)));
|
||||
return res.status(429).type("text/plain").send(options.message);
|
||||
}
|
||||
next();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
import { env } from "../env.js";
|
||||
|
||||
/**
|
||||
* Refuses a request that changes something when it was made by a page on another origin.
|
||||
*
|
||||
* The session cookie is SameSite=Lax, which stops *other sites* from sending it. It does not stop another app on the same
|
||||
* parent domain (dockge.home.lan against hm.home.lan is "same site"), and a POST with no body — stop a guest, restart a
|
||||
* container, rotate a token — needs no preflight and does exactly what it says. So the server asks the browser where the request
|
||||
* came from.
|
||||
*
|
||||
* Modern browsers say so in Sec-Fetch-Site, and a page can't change it. For anything older, a browser always sends Origin
|
||||
* with a cross-origin POST, so a request whose Origin isn't this app is refused. A request with neither header isn't from a
|
||||
* browser (curl, a script) and has no ambient cookie to abuse, so it passes.
|
||||
*
|
||||
* Covered: every POST, PUT, PATCH and DELETE, and GET /auth/logout (signing someone out from another page is a
|
||||
* nuisance, and it also ends their Authentik session). Not covered: the OIDC callback, which is a GET that
|
||||
* legitimately arrives from the identity provider, and the agent endpoint, which uses a token and is mounted ahead of this.
|
||||
*/
|
||||
export function sameOriginGuard(req: Request, res: Response, next: NextFunction) {
|
||||
const changesSomething = !["GET", "HEAD", "OPTIONS"].includes(req.method) || (req.method === "GET" && req.path === "/auth/logout");
|
||||
if (!changesSomething) return next();
|
||||
|
||||
const site = req.get("sec-fetch-site");
|
||||
if (site !== undefined) {
|
||||
if (site === "same-origin" || site === "none") return next();
|
||||
return refuse(res);
|
||||
}
|
||||
|
||||
const origin = req.get("origin");
|
||||
if (origin === undefined) return next();
|
||||
try {
|
||||
const host = new URL(origin).host;
|
||||
const own = [req.get("host"), req.get("x-forwarded-host"), new URL(env.appBaseUrl).host];
|
||||
if (own.includes(host)) return next();
|
||||
} catch {
|
||||
// an Origin that isn't a URL ("null", from a sandboxed frame) is not ours either
|
||||
}
|
||||
return refuse(res);
|
||||
}
|
||||
|
||||
function refuse(res: Response) {
|
||||
return res.status(403).json({ error: "cross_origin_request", message: "This request came from a different origin than the app, so it was refused." });
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
|
||||
/**
|
||||
* Headers every response carries. Deliberately small: a full Content-Security-Policy would have to be tuned to the UI's own
|
||||
* styles and scripts, and a wrong one breaks the app.
|
||||
*
|
||||
* - frame-ancestors 'none' / X-Frame-Options: nobody can put this app in a frame on another site and have someone click
|
||||
* through it (the old header is for browsers that don't read the CSP one).
|
||||
* - nosniff: a response is never reinterpreted as a different kind of file.
|
||||
* - Referrer-Policy: the address of a page here isn't sent along when someone follows a link to another site — admin links
|
||||
* and integration pages are exactly such links.
|
||||
*
|
||||
* No Strict-Transport-Security: whether the app is only ever reached over HTTPS is for the reverse proxy in front of it to say.
|
||||
*/
|
||||
export function securityHeaders(_req: Request, res: Response, next: NextFunction) {
|
||||
res.setHeader("Content-Security-Policy", "frame-ancestors 'none'");
|
||||
res.setHeader("X-Frame-Options", "DENY");
|
||||
res.setHeader("X-Content-Type-Options", "nosniff");
|
||||
res.setHeader("Referrer-Policy", "same-origin");
|
||||
next();
|
||||
}
|
||||
Reference in new issue
Block a user