From b6e655c8146fcb2e95cad3357be7102d964ccb5e Mon Sep 17 00:00:00 2001 From: Florian Date: Mon, 16 Nov 2026 10:29:48 +0100 Subject: [PATCH] fix(web): apply the content security policy to every page path The proxy skipped any path ending in an image or font extension and any path starting with the letters of a static prefix, so a page under such a path was served without a policy. The exclusions are now anchored to the real static locations. --- apps/web/src/proxy.test.ts | 44 +++++++++++++++++++++++++++++++++++++- apps/web/src/proxy.ts | 17 ++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/apps/web/src/proxy.test.ts b/apps/web/src/proxy.test.ts index 218058109..3e9a8f3a2 100644 --- a/apps/web/src/proxy.test.ts +++ b/apps/web/src/proxy.test.ts @@ -1,6 +1,48 @@ +import { AsyncLocalStorage } from "node:async_hooks"; import { NextRequest } from "next/server"; import { describe, expect, it } from "vitest"; -import { proxy } from "./proxy"; +import { config, proxy } from "./proxy"; + +// Next's matcher helper pulls in server modules that expect this global, which +// the Next server installs at startup. Set it before loading the helper. +(globalThis as { AsyncLocalStorage?: unknown }).AsyncLocalStorage ??= AsyncLocalStorage; +const { unstable_doesMiddlewareMatch } = await import("next/experimental/testing/server"); +const runsFor = (url: string) => unstable_doesMiddlewareMatch({ config, url }); + +describe("proxy matcher", () => { + it.each([ + "/", + "/s/abc", + // Page routes whose path happens to end in an asset extension. + "/s/abc.png", + "/nope.png", + "/admin/x.svg", + // Page routes that only share a prefix with an excluded location. + "/apikeys", + "/iconsfoo", + "/_nextfoo", + "/styles", + "/manifest.webmanifest", + "/osm-logo.svg", + ])("runs for %s", (url) => { + expect(runsFor(url)).toBe(true); + }); + + it.each([ + "/_next/static/chunks/a.js", + "/_next/image", + "/api", + "/api/v1/x", + "/favicon.ico", + "/sw.js", + "/icons/app/icon-192.png", + "/fonts/MaterialIcons-Regular.woff2", + "/styles/sprite.json", + "/attribution/tdx.png", + ])("skips %s", (url) => { + expect(runsFor(url)).toBe(false); + }); +}); const request = () => new NextRequest("https://openmapx.test/directions?to=1"); diff --git a/apps/web/src/proxy.ts b/apps/web/src/proxy.ts index 6e3dc8438..7125eafb7 100644 --- a/apps/web/src/proxy.ts +++ b/apps/web/src/proxy.ts @@ -34,8 +34,23 @@ export function proxy(request: NextRequest) { return response; } +/** + * Every exclusion below names a real static location, anchored so that a page + * route which merely shares a prefix (`/apikeys`) or ends in a file extension + * (`/s/.png`, a not-found `/x.png`) still gets the policy. Nothing is + * excluded by extension alone: a new page route is covered unless someone adds + * it here on purpose. + * + * - `_next/`, `api/`: framework assets and the backend. + * - `favicon.ico`, `sw.js`: exact files. The service worker must stay out, + * because a CSP header on a worker script applies to the worker itself. + * - `icons/`, `fonts/`, `styles/`, `attribution/`: `public/` asset folders. + * + * Other `public/` files (the root SVG logos, `runtime/`) still pass through + * here; a policy header on an image, font or JSON response is inert. + */ export const config = { matcher: [ - "/((?!_next|api|favicon.ico|icons|sw\\.js|manifest\\.json|.*\\.png$|.*\\.jpg$|.*\\.svg$|.*\\.webp$|.*\\.ico$|.*\\.woff2?$|.*\\.ttf$|.*\\.otf$).*)", + "/((?!(?:_next|api)(?:/|$)|(?:favicon\\.ico|sw\\.js)$|(?:icons|fonts|styles|attribution)/).*)", ], };