From 8e700311523d749f7b9ae08b83bc96c46deca4c5 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Tue, 6 Oct 2026 10:05:37 +0000 Subject: [PATCH] fix(auth): refuse sign-in destinations with control characters Browsers drop tabs and line breaks from a URL, so a callbackUrl or next of "/\t/evil.example" passed the same-site check and then opened //evil.example after signing in. The web app and the API now refuse control characters and backslashes anywhere in it. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01W42z4FbekWr33b6791svQQ --- fastapi_backend/app/domain/external_login.py | 4 +++- fastapi_backend/tests/api/test_external_login.py | 2 ++ nextjs-frontend/__tests__/externalSignIn.test.tsx | 1 + nextjs-frontend/__tests__/navigation.test.ts | 3 +++ nextjs-frontend/components/auth/external-signin-return.tsx | 3 ++- nextjs-frontend/lib/definitions.ts | 6 ++++-- 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/fastapi_backend/app/domain/external_login.py b/fastapi_backend/app/domain/external_login.py index 72e23c667..4c1a1277f 100644 --- a/fastapi_backend/app/domain/external_login.py +++ b/fastapi_backend/app/domain/external_login.py @@ -269,8 +269,10 @@ def start(db, cfg, key, origin, mode="login", account=None, next_path="/"): def _safe_next(path): + """A path on this site. Browsers drop tabs and line breaks from a URL ("/\\t/evil.example" is "//evil.example").""" path = path or "/" - return path if path.startswith("/") and not path.startswith("//") and "\\" not in path else "/" + ok = path.startswith("/") and not path.startswith("//") and "\\" not in path and not re.search(r"[\x00-\x1f\x7f]", path) + return path if ok else "/" def claim(db, state, browser): diff --git a/fastapi_backend/tests/api/test_external_login.py b/fastapi_backend/tests/api/test_external_login.py index 84766b5cf..95084e61b 100644 --- a/fastapi_backend/tests/api/test_external_login.py +++ b/fastapi_backend/tests/api/test_external_login.py @@ -162,6 +162,8 @@ def test_the_round_trip_is_tied_to_this_browser_and_used_once(app, client, new_c # where to go next is always a page of this site got = _sign_in(client, idp, "acme-sso", ADA, next_path="//evil.example/x") assert got["next"] == "/" + got = _sign_in(client, idp, "acme-sso", ADA, next_path="/\t/evil.example/x") # browsers drop the tab: //evil.example + assert got["next"] == "/" def test_a_wrong_client_secret_says_so(app, client, admin, idp, db): diff --git a/nextjs-frontend/__tests__/externalSignIn.test.tsx b/nextjs-frontend/__tests__/externalSignIn.test.tsx index a11755a2e..1b8bf658a 100644 --- a/nextjs-frontend/__tests__/externalSignIn.test.tsx +++ b/nextjs-frontend/__tests__/externalSignIn.test.tsx @@ -86,6 +86,7 @@ describe("coming back from the provider", () => { expect(safeNext("//evil.example")).toBe("/"); expect(safeNext("https://evil.example")).toBe("/"); expect(safeNext("/\\evil.example")).toBe("/"); + expect(safeNext("/\t/evil.example")).toBe("/"); expect(safeNext("/recordings?x=1")).toBe("/recordings?x=1"); }); }); diff --git a/nextjs-frontend/__tests__/navigation.test.ts b/nextjs-frontend/__tests__/navigation.test.ts index 46cc17e51..7f00825e1 100644 --- a/nextjs-frontend/__tests__/navigation.test.ts +++ b/nextjs-frontend/__tests__/navigation.test.ts @@ -37,6 +37,9 @@ describe("safeCallbackUrl", () => { ["//evil.example", "/"], ["/\\evil.example", "/"], ["https://evil.example", "/"], + ["/\t/evil.example", "/"], + ["/\n/evil.example", "/"], + ["/\r/evil.example", "/"], [null, "/"], ])("%p -> %p", (input, expected) => { expect(safeCallbackUrl(input)).toBe(expected); diff --git a/nextjs-frontend/components/auth/external-signin-return.tsx b/nextjs-frontend/components/auth/external-signin-return.tsx index b63d62c79..f19442cf9 100644 --- a/nextjs-frontend/components/auth/external-signin-return.tsx +++ b/nextjs-frontend/components/auth/external-signin-return.tsx @@ -5,10 +5,11 @@ import { useEffect, useState } from "react"; import { AuthAlert, AuthCard } from "@/components/auth/auth-card"; import { finishSignIn } from "@/components/auth/passkey-flows"; +import { safeCallbackUrl } from "@/lib/definitions"; /** A page of this site to go to next (never another site). */ export function safeNext(next: string | null | undefined, fallback = "/"): string { - return next && next.startsWith("/") && !next.startsWith("//") && !next.includes("\\") ? next : fallback; + return next && safeCallbackUrl(next) === next ? next : fallback; } function sentence(s: string): string { diff --git a/nextjs-frontend/lib/definitions.ts b/nextjs-frontend/lib/definitions.ts index c815f2d7b..0e92728ce 100644 --- a/nextjs-frontend/lib/definitions.ts +++ b/nextjs-frontend/lib/definitions.ts @@ -48,7 +48,9 @@ export type FormState = } | undefined; -/** Only same-origin paths are allowed as post-login destinations. */ +/** Only same-origin paths are allowed as post-login destinations. Browsers drop tabs and line breaks from a URL, so + * "/\t/evil.example" would become "//evil.example": control characters and backslashes are refused anywhere. */ export function safeCallbackUrl(value: unknown): string { - return typeof value === "string" && /^\/(?![/\\])/.test(value) ? value : "/"; + // eslint-disable-next-line no-control-regex + return typeof value === "string" && /^\/(?![/\\])/.test(value) && !/[\x00-\x1f\x7f\\]/.test(value) ? value : "/"; }