Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion apps/web/src/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -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");

Expand Down
17 changes: 16 additions & 1 deletion apps/web/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/<token>.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)/).*)",
],
};
Loading