From 784cb64c99bf55807f7059255e9d213a04d288e1 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Thu, 3 Dec 2026 03:05:18 +1300 Subject: [PATCH] app/: sign-in's return address refuses control characters and anything over 4000 characters, as today's server does: /%09/evil.com decoded to /\t/evil.com, which browsers turn into //evil.com after a real sign-in (an open redirect) --- app/src/auth/sign_in.rs | 9 ++++++++- app/tests/sign_in.rs | 9 +++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/auth/sign_in.rs b/app/src/auth/sign_in.rs index 34ec97c6..27de1dd6 100644 --- a/app/src/auth/sign_in.rs +++ b/app/src/auth/sign_in.rs @@ -53,7 +53,14 @@ fn provider(forge: &str) -> laravel::Result { /// A path on this site to return to, never another site's URL. fn safe_return(to: &str) -> Option { - (to.starts_with('/') && !to.starts_with("//") && !to.contains('\\')).then(|| to.to_string()) + // Control characters are refused: browsers strip a tab or newline from a Location, so `/\t/evil.com` would + // become `//evil.com`, another site. + (to.starts_with('/') + && !to.starts_with("//") + && !to.contains('\\') + && to.len() <= 4000 + && !to.chars().any(char::is_control)) + .then(|| to.to_string()) } /// `GET /auth/{forge}`: off to the forge. diff --git a/app/tests/sign_in.rs b/app/tests/sign_in.rs index 528101d4..30e2d742 100644 --- a/app/tests/sign_in.rs +++ b/app/tests/sign_in.rs @@ -81,8 +81,13 @@ async fn an_existing_account_is_found_by_a_verified_email(app: Application) -> l async fn an_unsafe_return_address_is_ignored(app: Application) -> laravel::Result<()> { let _github = github(); let _social = social::fake().with(Provider::GITHUB, ada()); - let start = app.get("/auth/github?return_to=//evil.example/x").await; - app.get("/auth/github/callback").with_cookies_from(&start).await.assert_redirect_to("/"); + let long = format!("/{}", "a".repeat(4000)); + for to in + ["//evil.example/x", "/%09/evil.example", "/%0a/evil.example", "/%0d/evil.example", "/%5C/evil.example", &long] + { + let start = app.get(format!("/auth/github?return_to={to}")).await; + app.get("/auth/github/callback").with_cookies_from(&start).await.assert_redirect_to("/"); + } Ok(()) }