From 6a473be5491b3d7572142feee049512d47379e59 Mon Sep 17 00:00:00 2001 From: Dennis Westermann Date: Sat, 7 Nov 2026 20:44:45 +0100 Subject: [PATCH] fix(auth): keep callback redirects on the public domain --- app/app/auth/callback/route.ts | 21 +++++----- app/lib/auth-callback.ts | 20 ++++++++++ app/tests/auth-callback.test.ts | 71 +++++++++++++++++++++++++++++++++ docs/DEPLOYMENT.md | 2 +- docs/VALIDATION.md | 8 ++++ 5 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 app/lib/auth-callback.ts create mode 100644 app/tests/auth-callback.test.ts diff --git a/app/app/auth/callback/route.ts b/app/app/auth/callback/route.ts index 87fd050..48840f8 100644 --- a/app/app/auth/callback/route.ts +++ b/app/app/auth/callback/route.ts @@ -1,14 +1,15 @@ -import { NextResponse } from "next/server"; import { config } from "@/lib/config"; +import { handleAuthCallback } from "@/lib/auth-callback"; import { serverClient } from "@/lib/supabase/server"; + export async function GET(request: Request) { - const url = new URL(request.url); - const code = url.searchParams.get("code"); - if (config.mode === "supabase" && code) { - const client = await serverClient(); - const { error } = await client.auth.exchangeCodeForSession(code); - if (!error) - return NextResponse.redirect(new URL("/onboarding", url.origin)); - } - return NextResponse.redirect(new URL("/invite?auth_error=1", url.origin)); + return handleAuthCallback( + request, + config.mode === "supabase" + ? async (code) => { + const client = await serverClient(); + return client.auth.exchangeCodeForSession(code); + } + : undefined, + ); } diff --git a/app/lib/auth-callback.ts b/app/lib/auth-callback.ts new file mode 100644 index 0000000..c5057ca --- /dev/null +++ b/app/lib/auth-callback.ts @@ -0,0 +1,20 @@ +import { NextResponse } from "next/server"; + +type ExchangeCode = (code: string) => Promise<{ error: unknown }>; + +export async function handleAuthCallback( + request: Request, + exchangeCode?: ExchangeCode, +) { + const code = new URL(request.url).searchParams.get("code"); + let location = "/invite?auth_error=1"; + if (code && exchangeCode) { + const { error } = await exchangeCode(code); + if (!error) location = "/onboarding"; + } + // Keep redirects on the browser's origin, even behind an internal proxy host. + return new NextResponse(null, { + status: 307, + headers: { Location: location, "Cache-Control": "private, no-store" }, + }); +} diff --git a/app/tests/auth-callback.test.ts b/app/tests/auth-callback.test.ts new file mode 100644 index 0000000..9f56e65 --- /dev/null +++ b/app/tests/auth-callback.test.ts @@ -0,0 +1,71 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { handleAuthCallback } from "../lib/auth-callback"; + +function assertRedirect(response: Response, location: string) { + assert.equal(response.status, 307); + assert.equal(response.headers.get("Location"), location); + assert.equal(response.headers.get("Cache-Control"), "private, no-store"); +} + +test("successful code exchange redirects locally despite the internal request origin", async () => { + const exchanged: string[] = []; + const response = await handleAuthCallback( + new Request("https://localhost:3218/auth/callback?code=valid-code"), + async (code) => { + exchanged.push(code); + return { error: null }; + }, + ); + assert.deepEqual(exchanged, ["valid-code"]); + assertRedirect(response, "/onboarding"); + for (const origin of ["https://humans-only.de", "https://ho.nm-forum.de"]) + assert.equal( + new URL(response.headers.get("Location")!, origin).href, + `${origin}/onboarding`, + ); +}); + +test("rejected or expired code returns to the local invitation page", async () => { + const response = await handleAuthCallback( + new Request("https://localhost:3218/auth/callback?code=expired-code"), + async () => ({ error: new Error("Code expired") }), + ); + assertRedirect(response, "/invite?auth_error=1"); +}); + +test("missing and empty codes never attempt an exchange", async () => { + for (const query of ["", "?code="]) { + const response = await handleAuthCallback( + new Request(`https://localhost:3218/auth/callback${query}`), + async () => assert.fail("No code should be exchanged"), + ); + assertRedirect(response, "/invite?auth_error=1"); + } +}); + +test("demo mode without a code exchanger cannot establish a live session", async () => { + const response = await handleAuthCallback( + new Request("https://localhost:3218/auth/callback?code=unused-code"), + ); + assertRedirect(response, "/invite?auth_error=1"); +}); + +test("caller-supplied hosts, forwarded headers and next parameters cannot change either destination", async () => { + for (const error of [null, new Error("Invalid code")]) { + const response = await handleAuthCallback( + new Request( + "https://attacker.example/auth/callback?code=test-code&next=https://attacker.example/collect", + { + headers: { + Host: "attacker.example", + "X-Forwarded-Host": "attacker.example", + "X-Forwarded-Proto": "http", + }, + }, + ), + async () => ({ error }), + ); + assertRedirect(response, error ? "/invite?auth_error=1" : "/onboarding"); + } +}); diff --git a/docs/DEPLOYMENT.md b/docs/DEPLOYMENT.md index ab1b6bc..362d8f7 100644 --- a/docs/DEPLOYMENT.md +++ b/docs/DEPLOYMENT.md @@ -32,7 +32,7 @@ npm run build npm start ``` -Hinter eigenem HTTPS-Reverse-Proxy betreiben. `npm start` verwendet den vollständigen Build im Checkout einschließlich `public/` und `.next/`. Keine Legacy-Clerk-/Prisma-Startskripte starten. +Hinter eigenem HTTPS-Reverse-Proxy betreiben. Der Auth-Callback antwortet mit festen relativen `Location`-Pfaden (`/onboarding` bzw. `/invite?auth_error=1`), damit der Browser auf seiner öffentlichen Domain bleibt. Interne Request-Hosts und weitergereichte Host-Header bestimmen diese Ziele nicht. `npm start` verwendet den vollständigen Build im Checkout einschließlich `public/` und `.next/`. Keine Legacy-Clerk-/Prisma-Startskripte starten. Manifest, Icons und Service Worker werden unter `/` bereitgestellt. `/sw.js` darf nicht langfristig gecacht werden; dafür sind Header gesetzt. Authentifizierte Seiten erhalten im Supabase-Modus `Cache-Control: private, no-store`. Der Service Worker cached nur `/offline.html`, keine App-Seiten, Supabase-Responses oder privaten Medien. diff --git a/docs/VALIDATION.md b/docs/VALIDATION.md index 1f72207..8f17c79 100644 --- a/docs/VALIDATION.md +++ b/docs/VALIDATION.md @@ -1,5 +1,13 @@ # Validierung — Humans Only 2.0 +## Callback-Regression 2026-11-07 + +Der Benutzer bestätigt den Empfang der Anmeldemail. Supabase protokolliert den erfolgreichen PKCE-Codeaustausch; die anschließende Weiterleitung führt jedoch zu `https://localhost:3218/onboarding`. Auch der Aufruf ohne Code ist auf beiden öffentlichen Domains reproduzierbar betroffen: HTTP 307 mit `Location: https://localhost:3218/invite?auth_error=1`. + +Ursache ist die Ableitung des Redirect-Ziels aus `request.url`, dessen Origin hinter dem Reverse-Proxy die interne Serveradresse enthält. Die Korrektur verwendet feste relative Ziele für Erfolgs- und Fehlerfall, behält HTTP 307 und den vorhandenen Session-/Cookie-Austausch bei und setzt `Cache-Control: private, no-store`. Es werden keine Host-Header oder frei wählbaren Rücksprungziele vertraut. + +Fünf neue Regressionstests prüfen den erfolgreichen Austausch bei interner Origin, abgelaufene Codes, fehlende/leere Codes, Demo-Modus sowie manipulierte Host-/Forwarded-Header und `next`-Parameter. `npm test` (15/15), `npm run typecheck`, `npm run lint` und `git diff --check` sind erfolgreich. Lokale Prüfungen: 15/15 Tests (davon fünf Callback-Regressionen), Typecheck, Lint und Produktionsbuild erfolgreich. Die öffentliche Abnahme wird im Deploymentprotokoll dieses Fixes festgehalten. Release-Auswirkung: **patch**; keine Versionsänderung ohne separaten Release-Auftrag. + ## Live-Deployment 2026-11-07 Release v2.0.0 / `2a62c42270a7132ecac630f61cf8934f5ff79f1f` wurde auf dem bestehenden Server aktiviert. Beide öffentlichen Domains liefern die neue App über HTTPS. Der Produktionsbuild verwendet das tatsächliche dedizierte Supabase-Projekt, keinen Demo-Modus. Der folgende historische lokale Abnahmebericht bleibt unverändert als Nachweis erhalten.