From 07fa210956f57ae31c09515c49fc3e59120a03b6 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:53:07 -0500 Subject: [PATCH] Preserve HTTPS redirect downgrade rejection --- crates/http-url/src/lib.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/http-url/src/lib.rs b/crates/http-url/src/lib.rs index bae96da5b..5c80f3c4c 100644 --- a/crates/http-url/src/lib.rs +++ b/crates/http-url/src/lib.rs @@ -60,7 +60,11 @@ impl Url { pub fn join(&self, location: &str) -> Result { let l = location.trim(); if l.starts_with("http://") || l.starts_with("https://") { - return Url::parse(l); + let url = Url::parse(l)?; + if self.tls && !url.tls { + return Err(HttpError::BadUrl("redirect from https to http refused".into())); + } + return Ok(url); } // another scheme (`ftp:`, `file:`, `data:`…) if let Some((scheme, _)) = l.split_once(':') @@ -130,6 +134,11 @@ mod tests { assert_eq!(u.join("other").unwrap().path, "/x/y/other"); assert_eq!(u.join("//c.example/p").unwrap().host, "c.example"); assert!(u.join("ftp://nope").is_err()); + // no downgrade from https to http, however the redirect is written + assert!(u.join("http://a.example/z").is_err()); + let plain = Url::parse("http://127.0.0.1:8080/a").unwrap(); + assert!(!plain.join("http://127.0.0.1:9/b").unwrap().tls); + assert!(plain.join("https://b.example/z").unwrap().tls); } }