From e30b5fbadbc570b3efb3047ab2e78d7b94e14e3c Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 25 Apr 2026 03:17:09 +0000 Subject: [PATCH] Reapply "Trust forwarded dashboard origins" This reverts commit 21b009c3433e50b2a47e323f8f18158581b7b701. --- middleware.ts | 39 ++++++++++++++++++- tests/middleware-origin-forwarding-check.mjs | 40 ++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/middleware-origin-forwarding-check.mjs diff --git a/middleware.ts b/middleware.ts index 6050cf1..95d3d75 100644 --- a/middleware.ts +++ b/middleware.ts @@ -34,12 +34,49 @@ 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 new URL(origin).origin === request.nextUrl.origin + return trustedRequestOrigins(request).has(new URL(origin).origin) } catch { return false } diff --git a/tests/middleware-origin-forwarding-check.mjs b/tests/middleware-origin-forwarding-check.mjs new file mode 100644 index 0000000..a2d2859 --- /dev/null +++ b/tests/middleware-origin-forwarding-check.mjs @@ -0,0 +1,40 @@ +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')