From 21b009c3433e50b2a47e323f8f18158581b7b701 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 25 Apr 2026 03:16:31 +0000 Subject: [PATCH] Revert "Trust forwarded dashboard origins" This reverts commit 4d89d8138a8e56e3bf22be7acfbb7f817c2ba6e2. --- middleware.ts | 39 +------------------ tests/middleware-origin-forwarding-check.mjs | 40 -------------------- 2 files changed, 1 insertion(+), 78 deletions(-) delete mode 100644 tests/middleware-origin-forwarding-check.mjs diff --git a/middleware.ts b/middleware.ts index 95d3d75..6050cf1 100644 --- a/middleware.ts +++ b/middleware.ts @@ -34,49 +34,12 @@ function isStateChangingMethod(method: string) { return !["GET", "HEAD", "OPTIONS"].includes(method.toUpperCase()) } -function firstForwardedValue(value: string | null) { - return value?.split(",")[0]?.trim() || null -} - -function originFromHost(host: string | null, protocol: string) { - if (!host) return null - try { - const normalizedProtocol = protocol.endsWith(":") ? protocol : `${protocol}:` - return new URL(`${normalizedProtocol}//${host}`).origin - } catch { - return null - } -} - -function publicBaseOrigin() { - if (!process.env.PUBLIC_BASE_URL) return null - try { - return new URL(process.env.PUBLIC_BASE_URL).origin - } catch { - return null - } -} - -function trustedRequestOrigins(request: NextRequest) { - const forwardedProto = firstForwardedValue(request.headers.get("x-forwarded-proto")) - const forwardedHost = firstForwardedValue(request.headers.get("x-forwarded-host")) - const host = firstForwardedValue(request.headers.get("host")) - const protocol = forwardedProto || request.nextUrl.protocol || "http:" - - return new Set([ - request.nextUrl.origin, - originFromHost(host, protocol), - originFromHost(forwardedHost, protocol), - publicBaseOrigin(), - ].filter((origin): origin is string => Boolean(origin))) -} - function hasTrustedOrigin(request: NextRequest) { const origin = request.headers.get("origin") if (!origin) return true try { - return trustedRequestOrigins(request).has(new URL(origin).origin) + return new URL(origin).origin === request.nextUrl.origin } catch { return false } diff --git a/tests/middleware-origin-forwarding-check.mjs b/tests/middleware-origin-forwarding-check.mjs deleted file mode 100644 index a2d2859..0000000 --- a/tests/middleware-origin-forwarding-check.mjs +++ /dev/null @@ -1,40 +0,0 @@ -import assert from 'node:assert/strict' -import { readFile } from 'node:fs/promises' -import path from 'node:path' - -const root = process.cwd() -const middlewarePath = path.join(root, 'middleware.ts') -const middlewareSource = await readFile(middlewarePath, 'utf8') - -assert.match( - middlewareSource, - /function trustedRequestOrigins\(request: NextRequest\)/, - 'middleware must collect trusted request origins for state-changing requests' -) -assert.match( - middlewareSource, - /request\.headers\.get\("x-forwarded-host"\)/, - 'origin check must trust the forwarded public host behind a reverse proxy' -) -assert.match( - middlewareSource, - /request\.headers\.get\("x-forwarded-proto"\)/, - 'origin check must preserve the forwarded public protocol behind a reverse proxy' -) -assert.match( - middlewareSource, - /process\.env\.PUBLIC_BASE_URL/, - 'origin check should support an explicitly configured public base URL' -) -assert.match( - middlewareSource, - /trustedRequestOrigins\(request\)\.has\(new URL\(origin\)\.origin\)/, - 'origin check must compare browser Origin against the trusted origin set' -) -assert.doesNotMatch( - middlewareSource, - /new URL\(origin\)\.origin === request\.nextUrl\.origin/, - 'origin check must not only compare against the internal Next request origin' -) - -console.log('middleware-origin-forwarding-check: PASS forwarded origin trust assertions')