From 5be8434eec0efb47d9c440743555be92ee8088ab Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 17:19:56 -0600 Subject: [PATCH 1/2] fix(id-web): reject malformed bracketed IPv6 Host authorities (#8776) --- pkgs/id/src/web/security.rs | 51 +++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/web/security.rs b/pkgs/id/src/web/security.rs index 9717ffc98..48f4d57b1 100644 --- a/pkgs/id/src/web/security.rs +++ b/pkgs/id/src/web/security.rs @@ -89,8 +89,21 @@ pub(super) fn protect(router: axum::Router, security: WebSecurity) -> axum::Rout fn host_only(authority: &str) -> String { let a = authority.trim().to_lowercase(); if let Some(rest) = a.strip_prefix('[') { - // IPv6 literal: [::1]:3000 - return rest.split(']').next().unwrap_or("").to_owned(); + // IPv6 literal: [::1]:3000 -- validate the WHOLE bracketed authority + // before extracting the host (#8776). Only an empty tail or a + // colon + numeric port may follow the closing bracket; anything + // else (trailing host text, a non-numeric or empty port, a missing + // bracket) is malformed and must not reduce to the bracketed host, + // or the loopback allow-list would trust a mere prefix. Malformed + // authorities return the full text, which matches no allowed host. + let Some((host, tail)) = rest.split_once(']') else { + return a.clone(); + }; + let tail_ok = tail.is_empty() + || tail + .strip_prefix(':') + .is_some_and(|port| !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit())); + return if tail_ok { host.to_owned() } else { a.clone() }; } a.rsplit_once(':') .filter(|(_, port)| port.bytes().all(|b| b.is_ascii_digit())) @@ -302,6 +315,27 @@ mod tests { assert_eq!(host_only("example.com"), "example.com"); } + #[test] + fn host_only_rejects_malformed_bracketed_authorities() { + // Oracle: a bracketed authority is well-formed only as a bracketed + // host, optionally followed by a colon and a numeric port. Anything + // else must NOT reduce to the bracketed host, or the loopback + // allow-list would trust a mere prefix. + for malformed in [ + "[::1]attacker.example", + "[::1]attacker.example:3000", + "[::1]:abc", + "[::1]:", + "[::1", + "[::1]]", + ] { + assert_ne!(host_only(malformed), "::1", "{malformed}"); + } + // Valid controls still extract the host. + assert_eq!(host_only("[::1]"), "::1"); + assert_eq!(host_only("[::1]:3000"), "::1"); + } + #[test] fn ct_eq_behaves_like_eq() { assert!(ct_eq(b"abc", b"abc")); @@ -327,6 +361,19 @@ mod tests { assert_eq!(r.status(), StatusCode::MISDIRECTED_REQUEST); } + #[tokio::test] + async fn malformed_bracketed_host_is_refused() { + for host in ["[::1]attacker.example", "[::1", "[::1]:abc"] { + let r = call(app(loopback()), get_req(host).body(Body::empty()).unwrap()).await; + assert_eq!(r.status(), StatusCode::MISDIRECTED_REQUEST, "{host}"); + } + // Valid bracketed controls still pass the guard. + for host in ["[::1]", "[::1]:3000"] { + let r = call(app(loopback()), get_req(host).body(Body::empty()).unwrap()).await; + assert_eq!(r.status(), StatusCode::OK, "{host}"); + } + } + #[tokio::test] async fn extra_hosts_extend_the_loopback_allow_list() { let sec = WebSecurity::for_bind( From c72d639a22b28d98adef0f67db757237e4121fd8 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Wed, 11 Nov 2026 17:26:11 -0600 Subject: [PATCH 2/2] test(id-web): property-test the bracketed authority invariant (#8776) --- pkgs/id/src/web/security.rs | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkgs/id/src/web/security.rs b/pkgs/id/src/web/security.rs index 48f4d57b1..49d9eda83 100644 --- a/pkgs/id/src/web/security.rs +++ b/pkgs/id/src/web/security.rs @@ -336,6 +336,47 @@ mod tests { assert_eq!(host_only("[::1]:3000"), "::1"); } + use proptest::prelude::*; + + proptest! { + #![proptest_config(ProptestConfig::with_cases(128))] + + #[test] + fn bracketed_authority_collapses_only_for_valid_tails( + host in proptest::sample::select(vec!["::1", "::", "fe80::1", "2001:db8::1"]), + tail in "[!-~]{0,12}", + ) { + // Oracle (#8776): host_only of a bracketed authority returns the + // bracketed host ONLY when the tail after the closing bracket is + // empty or a colon followed by a non-empty all-digit port. Every + // other tail leaves the authority unreduced, so it can never + // match an allowed host. The fixed examples in + // host_only_rejects_malformed_bracketed_authorities are the + // regression smoke tests for this generated invariant. + let authority = format!("[{host}]{tail}"); + let valid_tail = tail.is_empty() + || tail + .strip_prefix(':') + .is_some_and(|port| { + !port.is_empty() && port.bytes().all(|b| b.is_ascii_digit()) + }); + if valid_tail { + prop_assert_eq!(host_only(&authority), host.to_owned(), "{}", authority); + } else { + prop_assert_ne!(host_only(&authority), host.to_owned(), "{}", authority); + } + } + + #[test] + fn bracketed_authority_with_numeric_port_always_extracts_host( + host in proptest::sample::select(vec!["::1", "::", "fe80::1", "2001:db8::1"]), + port in any::(), + ) { + prop_assert_eq!(host_only(&format!("[{host}]:{port}")), host.to_owned()); + prop_assert_eq!(host_only(&format!("[{host}]")), host.to_owned()); + } + } + #[test] fn ct_eq_behaves_like_eq() { assert!(ct_eq(b"abc", b"abc"));