feat: apply status page CSP to custom domains

Detect custom domains by comparing the request host to the app host and apply
the tightened CSP there too, not just on the public path. Run the middleware
before express.static so static-served root documents get the header, and keep
helmet after static so its default-src does not block custom domain API calls.
This commit is contained in:
Buco7854 committed 2026-06-24 18:21:16 +00:00
1 parent ffbf223288
commit a4cc322a55
3 files changed
+51 -20

No files matched your search

@@ -1,9 +1,10 @@
import type { NextFunction, Request, Response } from "express";
import { normalizeStatusPageDomain } from "@/utils/statusPageDomain.js";
const PUBLIC_STATUS_PAGE_DOCUMENT_PREFIX = "/status/public";
// Browsers enforce the intersection of all CSP headers, so this only tightens the
// public status page on top of the global helmet policy: it blocks external images,
// Browsers enforce the intersection of all CSP headers, so this only tightens
// the status page on top of the global helmet policy, blocking external images,
// fonts, and stylesheets from custom CSS while keeping the app's Google Fonts.
const STATUS_PAGE_CSP = [
"img-src 'self' data:",
@@ -11,9 +12,16 @@ const STATUS_PAGE_CSP = [
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
].join("; ");
export const statusPageDocumentCsp = (req: Request, res: Response, next: NextFunction) => {
if (req.path.startsWith(PUBLIC_STATUS_PAGE_DOCUMENT_PREFIX)) {
res.append("Content-Security-Policy", STATUS_PAGE_CSP);
}
return next();
// A status page document is served on the public path or on a custom domain
// (any host other than the app's own). Mirrors the client isCustomDomainHost.
export const createStatusPageDocumentCsp = (clientHost: string) => {
const appHostname = normalizeStatusPageDomain(clientHost);
return (req: Request, res: Response, next: NextFunction) => {
const onCustomDomain = appHostname !== null && normalizeStatusPageDomain(req.hostname) !== appHostname;
if (req.path.startsWith(PUBLIC_STATUS_PAGE_DOCUMENT_PREFIX) || onCustomDomain) {
res.append("Content-Security-Policy", STATUS_PAGE_CSP);
}
return next();
};
};