Files
causius/proxy.ts
Claude 4fe0b47672 Anonymise letters in the browser, harden headers, fix English 500s
- Anonymisation: names, addresses, AHV, IBAN, phone, e-mail, birth dates and
  labelled reference numbers are replaced with placeholders before a letter
  leaves the browser; the reader sees and can adjust every replacement and
  preview what is sent. The browser restores the values in the result.
- Photo cover tool: black out areas of a page; boxes are burnt into the
  pixels before upload.
- Anonymised export for helpers; pseudonymity for clients, assistants and
  lawyers in the helper rules (lawyer named to a client on engagement).
- Security: per-request nonce CSP (strict-dynamic, connect-src 'self',
  frame-ancestors 'none'), HSTS, nosniff, no-referrer, Permissions-Policy,
  COOP/CORP, no-store on the API, no X-Powered-By.
- Fix: unprefixed English pages were rewritten by next-intl to
  https://localhost behind the box's proxy and answered 500. Every language
  is now prefixed; / redirects by browser language.
- AGENTS.md: finish to production; security and privacy stay strict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012EJFFkKhmaVnnH8E3mHnaZ
2026-10-11 19:21:39 +02:00

65 lines
2.4 KiB
TypeScript

import createMiddleware from "next-intl/middleware";
import type { NextRequest } from "next/server";
import { routing } from "./i18n/routing";
import { contentSecurityPolicy } from "./lib/security";
const handleI18nRouting = createMiddleware(routing);
/**
* Every page request gets two things here:
*
* 1. A language. `/de/cases` renders German; `/` redirects to the reader's.
* 2. A Content-Security-Policy with a fresh nonce. Causius handles legal
* letters, so a script injected into a page could read a case straight
* out of the browser's storage. With this policy the browser runs only
* scripts this response vouched for: Next's own, carrying the nonce.
*
* Next reads the nonce from the REQUEST's CSP header and stamps it on its own
* scripts, so the policy is forwarded on the request as well as set on the
* response. next-intl builds its own response, so the request headers are
* forwarded with Next's override mechanism rather than NextResponse.next().
*/
export default function proxy(request: NextRequest) {
const nonce = btoa(crypto.randomUUID());
const csp = contentSecurityPolicy(nonce, {
dev: process.env.NODE_ENV === "development",
widget: Boolean(process.env.NEXT_PUBLIC_FC_WIDGET_TOKEN),
});
const response = handleI18nRouting(request);
const overridden = response.headers.get("x-middleware-override-headers");
const names = new Set(overridden ? overridden.split(",") : []);
if (!overridden) {
// Next replaces the request's headers with exactly the overridden list,
// so the first override has to carry every original header along.
request.headers.forEach((value, key) => {
names.add(key);
response.headers.set(`x-middleware-request-${key}`, value);
});
}
for (const [key, value] of [
["content-security-policy", csp],
["x-nonce", nonce],
] as const) {
names.add(key);
response.headers.set(`x-middleware-request-${key}`, value);
}
response.headers.set("x-middleware-override-headers", [...names].join(","));
response.headers.set("Content-Security-Policy", csp);
return response;
}
export const config = {
matcher: [
{
source: "/((?!api|_next/static|_next/image|_vercel|.*\\..*).*)",
// A prefetch is not rendered for a reader and needs no nonce.
missing: [
{ type: "header", key: "next-router-prefetch" },
{ type: "header", key: "purpose", value: "prefetch" },
],
},
],
};