Files
CatoandClaude Sonnet 5.5 69944583e0 fix(security): harden redaction and the agent, make the security copy true (#20)
* fix(security): harden redaction and the agent, and make the security copy true

Redaction: titles, initials, ALL CAPS, street/PO-box forms, phone/IBAN variants,
bounded quantifiers (no quadratic backtracking), token guard.
Agent: untrusted-data wrapping, strict dates, write caps, address guard,
per-matter turn lock. Copy: deadline dates readable, detection not perfect.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QDMxjTqKNLXETMYYuapMa3

* fix(security): keep the log-content sentence

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QDMxjTqKNLXETMYYuapMa3

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-12 09:31:04 -07:00

65 lines
2.5 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|.*\\.(?:ico|png|jpe?g|svg|webp|gif|txt|xml|json|webmanifest|woff2?|map|js|css)$).*)",
// 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" },
],
},
],
};