Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(ui): anchor the auth middleware's path exclusions (#251)

Rufusb90c611d0889
diff --git a/packages/ui/scripts/middleware-matcher.test.mjs b/packages/ui/scripts/middleware-matcher.test.mjs
new file mode 100644
index 00000000..63b4a2c6
--- /dev/null
+++ b/packages/ui/scripts/middleware-matcher.test.mjs
@@ -0,0 +1,67 @@
+// Which paths the auth middleware skips.
+//
+// `config.matcher` in src/middleware.ts is a negative lookahead: every entry
+// in it is a path the session check never sees. An entry with no terminator
+// excludes every path that merely *starts with* it — `api/auth` used to skip
+// `/api/authorizations`, and `favicon.ico` skipped `/favicon.icoX` and (the dot
+// being unescaped) `/faviconXico` — so a future route with such a prefix would
+// be served with no session check at all.
+//
+// The matcher is not a plain RegExp: Next rewrites it through its vendored
+// path-to-regexp, which is where escaping goes wrong (`\\.` in the TS source is
+// right; `\\\\.` would match a literal backslash). So this compiles the string
+// with Next's own functions — the ones that write middleware-manifest.json and
+// match requests against it — instead of re-implementing them. middleware.ts
+// itself can't be imported here (`@/` aliases, next-auth), and Next only reads
+// a literal matcher anyway, so the literal is read from the source.
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { createRequire } from "node:module";
+import test from "node:test";
+
+const require = createRequire(import.meta.url);
+const { getMiddlewareMatchers } = require("next/dist/build/analysis/get-page-static-info.js");
+const { getMiddlewareRouteMatcher } = require("next/dist/shared/lib/router/utils/middleware-route-matcher.js");
+
+const source = readFileSync(new URL("../src/middleware.ts", import.meta.url), "utf8");
+const literal = source.match(/matcher:\s*\[\s*("(?:[^"\\]|\\.)*")\s*,?\s*\]/);
+assert.ok(literal, "could not find a single string literal in middleware.ts config.matcher");
+
+// next.config.ts sets neither basePath nor i18n, the only config this reads.
+const matcher = getMiddlewareRouteMatcher(getMiddlewareMatchers([JSON.parse(literal[1])], {}));
+const runsMiddleware = (pathname) => matcher(pathname, { headers: {}, cookies: {} }, {});
+
+test("Auth.js routes, static assets and the sign-in page skip the middleware", () => {
+ for (const path of [
+ "/api/auth",
+ "/api/auth/",
+ "/api/auth/session",
+ "/api/auth/providers",
+ "/api/auth/callback/google",
+ "/favicon.ico",
+ "/_next/static/chunks/main.js",
+ "/signin",
+ ]) {
+ assert.equal(runsMiddleware(path), false, path);
+ }
+});
+
+test("a path that only starts with an excluded entry is still gated", () => {
+ for (const path of [
+ "/api/authX",
+ "/api/authorizations",
+ "/api/auth-anything",
+ "/favicon.icoX",
+ "/faviconXico",
+ "/favicon.ico/x",
+ "/signin-help",
+ ]) {
+ assert.equal(runsMiddleware(path), true, path);
+ }
+});
+
+test("ordinary pages and API routes are gated", () => {
+ for (const path of ["/", "/settings", "/api/backend/health"]) {
+ assert.equal(runsMiddleware(path), true, path);
+ }
+});
diff --git a/packages/ui/src/middleware.ts b/packages/ui/src/middleware.ts
index 5b729c0a..ace88f50 100644
--- a/packages/ui/src/middleware.ts
+++ b/packages/ui/src/middleware.ts
@@ -11,6 +11,11 @@ export { auth as middleware } from "@/auth";
// Exclude Auth.js's own routes, Next internals, static assets, and exactly
// `/signin` (with optional trailing slash). Using `signin/?` rather than the
// looser `signin` keeps unrelated paths like `/signin-help` gated.
+// An entry with no terminator would exclude every path that merely starts
+// with it, so Auth.js's routes end at a segment boundary and the favicon is an
+// exact match: a future `/api/authorizations` or `/favicon.icoX` stays gated.
+// Checked against Next's own matcher compiler in
+// scripts/middleware-matcher.test.mjs.
export const config = {
- matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico|signin/?$).*)"],
+ matcher: ["/((?!api/auth(?:/|$)|_next/static|_next/image|favicon\\.ico$|signin/?$).*)"],
};