Redirects use the address the browser used, not the server's listen address

Behind Docker or a proxy, request.url says http://0.0.0.0:3000, so the sign-in
link sent people there. originOf() reads the Host or X-Forwarded-Host header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
MangaValkandClaude Opus 5.5 committed 2026-11-17 13:56:32 +01:00
1 parent 44aa5e0933
commit c50c645cfb
8 files changed
+38 -8

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
// The address the browser used to reach broke, for redirects and callback links.
// request.url carries the server's own listen address instead (http://0.0.0.0:3000 in Docker,
// http://localhost:3000 behind a proxy), so the Host header the browser or proxy sent wins.
const HOST = /^[a-z0-9.-]+(:\d+)?$|^\[[0-9a-f:]+\](:\d+)?$/i;
export function originOf(request: Request) {
const own = new URL(request.url);
const h = request.headers;
const host = (h.get("x-forwarded-host") ?? h.get("host") ?? "").split(",")[0].trim();
if (!HOST.test(host)) return own.origin;
const proto = (h.get("x-forwarded-proto") ?? "").split(",")[0].trim();
return `${proto === "https" || proto === "http" ? proto : own.protocol.slice(0, -1)}://${host}`;
}