From 02224df6cb51563b4cade129399dc2b197a4bee9 Mon Sep 17 00:00:00 2001 From: Daniel Voshart <205798369+voshart@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:48:09 -0500 Subject: [PATCH 1/5] Import LightCraft HTTP URL parser --- crates/http-url/src/lib.rs | 126 +++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 crates/http-url/src/lib.rs diff --git a/crates/http-url/src/lib.rs b/crates/http-url/src/lib.rs new file mode 100644 index 000000000..3babf1b13 --- /dev/null +++ b/crates/http-url/src/lib.rs @@ -0,0 +1,126 @@ +/// A parsed `http://` or `https://` URL. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Url { + pub tls: bool, + pub host: String, + pub port: u16, + /// Path and query, starting with `/`. + pub path: String, +} + +impl Url { + pub fn parse(s: &str) -> Result { + let bad = |why: &str| HttpError::BadUrl(format!("{s}: {why}")); + let s = s.trim(); + if s.chars().any(|c| c.is_control() || c == ' ') { + return Err(bad("contains spaces or control characters")); + } + let (tls, rest) = if let Some(r) = s.strip_prefix("https://") { + (true, r) + } else if let Some(r) = s.strip_prefix("http://") { + (false, r) + } else { + return Err(bad("only http:// and https:// are supported")); + }; + let rest = rest.split('#').next().unwrap_or_default(); + let (authority, path) = match rest.find(['/', '?']) { + Some(i) => (rest.get(..i).unwrap_or_default(), rest.get(i..).unwrap_or_default()), + None => (rest, ""), + }; + let path = if path.starts_with('/') { path.to_string() } else { format!("/{path}") }; + if authority.contains('@') { + return Err(bad("user names and passwords in URLs are not supported")); + } + let (host, port) = if let Some(v6) = authority.strip_prefix('[') { + let end = v6.find(']').ok_or_else(|| bad("unterminated IPv6 address"))?; + let host = v6.get(..end).unwrap_or_default(); + let after = v6.get(end + 1..).unwrap_or_default(); + (host.to_string(), after.strip_prefix(':')) + } else { + match authority.rsplit_once(':') { + Some((h, p)) => (h.to_string(), Some(p)), + None => (authority.to_string(), None), + } + }; + let port = match port { + Some(p) => p.parse::().ok().filter(|p| *p != 0).ok_or_else(|| bad("bad port"))?, + None if tls => 443, + None => 80, + }; + if host.is_empty() || !host.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '.' | ':' | '_')) { + return Err(bad("bad host name")); + } + Ok(Url { tls, host: host.to_ascii_lowercase(), port, path }) + } + + /// `Location` of a redirect, resolved against this 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); + } + // another scheme (`ftp:`, `file:`, `data:`…) + if let Some((scheme, _)) = l.split_once(':') + && !scheme.is_empty() + && scheme.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')) + { + return Err(HttpError::BadUrl(format!("redirect to an unsupported location `{scheme}:`"))); + } + let scheme = if self.tls { "https" } else { "http" }; + let host = if self.host.contains(':') { format!("[{}]", self.host) } else { self.host.clone() }; + let base = format!("{scheme}://{host}:{}", self.port); + if let Some(rest) = l.strip_prefix("//") { + return Url::parse(&format!("{scheme}://{rest}")); + } + if l.starts_with('/') { + return Url::parse(&format!("{base}{l}")); + } + // relative to the current directory + let dir = self.path.split('?').next().unwrap_or("/"); + let dir = dir.rsplit_once('/').map_or("", |(d, _)| d); + Url::parse(&format!("{base}{dir}/{l}")) + } + + fn host_header(&self) -> String { + let host = if self.host.contains(':') { format!("[{}]", self.host) } else { self.host.clone() }; + let default = if self.tls { 443 } else { 80 }; + if self.port == default { host } else { format!("{host}:{}", self.port) } + } + + /// The URL without its query (for messages: a query may hold a signed token). + pub fn display(&self) -> String { + let scheme = if self.tls { "https" } else { "http" }; + format!("{scheme}://{}{}", self.host_header(), self.path.split('?').next().unwrap_or("/")) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_urls() { + let u = Url::parse("https://cdn.example.com/models/sam3/model.safetensors?sig=1#x").unwrap(); + assert_eq!((u.tls, u.host.as_str(), u.port, u.path.as_str()), (true, "cdn.example.com", 443, "/models/sam3/model.safetensors?sig=1")); + assert_eq!(u.display(), "https://cdn.example.com/models/sam3/model.safetensors"); + let u = Url::parse("http://127.0.0.1:8080").unwrap(); + assert_eq!((u.tls, u.port, u.path.as_str()), (false, 8080, "/")); + let u = Url::parse("http://[::1]:9/a").unwrap(); + assert_eq!((u.host.as_str(), u.port), ("::1", 9)); + for bad in ["ftp://x/y", "https://", "https://a b/", "https://u:p@h/", "http://h:0/", "http://h:99999/", "https://h/\r\nX: y", "file:///etc"] + { + assert!(Url::parse(bad).is_err(), "{bad}"); + } + } + + #[test] + fn resolves_redirects() { + let u = Url::parse("https://a.example/x/y/file?q").unwrap(); + assert_eq!(u.join("https://b.example/z").unwrap().host, "b.example"); + assert_eq!(u.join("/root").unwrap().path, "/root"); + 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()); + } + +} From d577b23c9c5b796161d6eec275dd9afd8163a24d Mon Sep 17 00:00:00 2001 From: Daniel Voshart <205798369+voshart@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:53:07 -0500 Subject: [PATCH 2/5] Preserve bounded URLs and query-safe error messages --- 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 3babf1b13..bae96da5b 100644 --- a/crates/http-url/src/lib.rs +++ b/crates/http-url/src/lib.rs @@ -10,7 +10,10 @@ pub struct Url { impl Url { pub fn parse(s: &str) -> Result { - let bad = |why: &str| HttpError::BadUrl(format!("{s}: {why}")); + let bad = |why: &str| HttpError::BadUrl(why.to_string()); + if s.len() > 16 * 1024 { + return Err(bad("URL exceeds 16 KiB")); + } let s = s.trim(); if s.chars().any(|c| c.is_control() || c == ' ') { return Err(bad("contains spaces or control characters")); @@ -98,6 +101,12 @@ impl Url { mod tests { use super::*; + #[test] + fn rejects_oversized_and_control_character_urls() { + assert!(Url::parse(&format!("https://example.com/{}", "x".repeat(16 * 1024))).is_err()); + assert!(Url::parse("https://example.com/x?secret=abc\r\nInjected: 1").is_err()); + } + #[test] fn parses_urls() { let u = Url::parse("https://cdn.example.com/models/sam3/model.safetensors?sig=1#x").unwrap(); 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 3/5] 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); } } From 837e4d365a3ce56ac21b2fc95ebbaeeee8629b35 Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:48:09 -0500 Subject: [PATCH 4/5] Preserve URL normalization and malformed authority regressions --- crates/http-url/src/lib.rs | 112 +++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 4 deletions(-) diff --git a/crates/http-url/src/lib.rs b/crates/http-url/src/lib.rs index 5c80f3c4c..5bde118a3 100644 --- a/crates/http-url/src/lib.rs +++ b/crates/http-url/src/lib.rs @@ -8,6 +8,43 @@ pub struct Url { pub path: String, } +/// Remove `.` and `..` segments from the path part of `path` (RFC 3986 section 5.2.4); the query is kept as is. +fn remove_dot_segments(path: &str) -> String { + let (p, query) = match path.find('?') { + Some(i) => (path.get(..i).unwrap_or_default(), path.get(i..).unwrap_or_default()), + None => (path, ""), + }; + let mut out: Vec<&str> = Vec::new(); + let mut segments = p.split('/').skip(1).peekable(); + let mut trailing_slash = false; + while let Some(seg) = segments.next() { + let last = segments.peek().is_none(); + match seg { + "." => trailing_slash = last, + ".." => { + out.pop(); + trailing_slash = last; + } + _ => { + out.push(seg); + trailing_slash = false; + } + } + } + let mut result = format!("/{}", out.join("/")); + if trailing_slash && !result.ends_with('/') { + result.push('/'); + } + result.push_str(query); + result +} + +/// `s` without the leading `prefix`, matched ASCII case-insensitively (RFC 3986 §3.1). +fn strip_scheme<'a>(s: &'a str, prefix: &str) -> Option<&'a str> { + let head = s.get(..prefix.len())?; + head.eq_ignore_ascii_case(prefix).then(|| s.get(prefix.len()..).unwrap_or_default()) +} + impl Url { pub fn parse(s: &str) -> Result { let bad = |why: &str| HttpError::BadUrl(why.to_string()); @@ -18,9 +55,9 @@ impl Url { if s.chars().any(|c| c.is_control() || c == ' ') { return Err(bad("contains spaces or control characters")); } - let (tls, rest) = if let Some(r) = s.strip_prefix("https://") { + let (tls, rest) = if let Some(r) = strip_scheme(s, "https://") { (true, r) - } else if let Some(r) = s.strip_prefix("http://") { + } else if let Some(r) = strip_scheme(s, "http://") { (false, r) } else { return Err(bad("only http:// and https:// are supported")); @@ -31,6 +68,7 @@ impl Url { None => (rest, ""), }; let path = if path.starts_with('/') { path.to_string() } else { format!("/{path}") }; + let path = remove_dot_segments(&path); if authority.contains('@') { return Err(bad("user names and passwords in URLs are not supported")); } @@ -38,6 +76,9 @@ impl Url { let end = v6.find(']').ok_or_else(|| bad("unterminated IPv6 address"))?; let host = v6.get(..end).unwrap_or_default(); let after = v6.get(end + 1..).unwrap_or_default(); + if !after.is_empty() && !after.starts_with(':') { + return Err(bad("unexpected text after IPv6 address")); + } (host.to_string(), after.strip_prefix(':')) } else { match authority.rsplit_once(':') { @@ -46,7 +87,8 @@ impl Url { } }; let port = match port { - Some(p) => p.parse::().ok().filter(|p| *p != 0).ok_or_else(|| bad("bad port"))?, + Some(p) if p.bytes().all(|b| b.is_ascii_digit()) => p.parse::().ok().filter(|p| *p != 0).ok_or_else(|| bad("bad port"))?, + Some(_) => return Err(bad("bad port")), None if tls => 443, None => 80, }; @@ -59,7 +101,7 @@ impl Url { /// `Location` of a redirect, resolved against this URL. pub fn join(&self, location: &str) -> Result { let l = location.trim(); - if l.starts_with("http://") || l.starts_with("https://") { + if strip_scheme(l, "http://").is_some() || strip_scheme(l, "https://").is_some() { let url = Url::parse(l)?; if self.tls && !url.tls { return Err(HttpError::BadUrl("redirect from https to http refused".into())); @@ -82,8 +124,16 @@ impl Url { if l.starts_with('/') { return Url::parse(&format!("{base}{l}")); } + // empty or fragment-only: the same document + if l.is_empty() || l.starts_with('#') { + return Ok(self.clone()); + } // relative to the current directory let dir = self.path.split('?').next().unwrap_or("/"); + // a query-only reference keeps the whole path, file name included (RFC 3986 §5.2.2) + if l.starts_with('?') { + return Url::parse(&format!("{base}{dir}{l}")); + } let dir = dir.rsplit_once('/').map_or("", |(d, _)| d); Url::parse(&format!("{base}{dir}/{l}")) } @@ -105,12 +155,46 @@ impl Url { mod tests { use super::*; + #[test] + fn fragment_only_or_empty_location_keeps_file_name_and_query() { + let base = Url::parse("https://a.example/x/y/file?q").unwrap(); + for l in ["#top", "", " "] { + let u = base.join(l).unwrap(); + assert_eq!((u.tls, u.host.as_str(), u.port, u.path.as_str()), (true, "a.example", 443, "/x/y/file?q"), "{l:?}"); + } + } + #[test] fn rejects_oversized_and_control_character_urls() { assert!(Url::parse(&format!("https://example.com/{}", "x".repeat(16 * 1024))).is_err()); assert!(Url::parse("https://example.com/x?secret=abc\r\nInjected: 1").is_err()); } + #[test] + fn join_query_only_keeps_file_name() { + let u = Url::parse("https://a.example/x/y/file?q=1").unwrap(); + assert_eq!(u.join("?page=2").unwrap().path, "/x/y/file?page=2"); + } + + #[test] + fn join_removes_dot_segments() { + let base = Url::parse("https://a.example/x/y/file").unwrap(); + for (loc, want) in + [("../z", "/x/z"), ("./z", "/x/y/z"), ("../../../z", "/z"), ("/a/./b/../c", "/a/c"), ("../z?q=1", "/x/z?q=1"), ("..", "/x/")] + { + assert_eq!(base.join(loc).unwrap().path, want, "{loc}"); + } + } + + #[test] + fn url_scheme_is_matched_case_insensitively() { + let u = Url::parse("HTTPS://A.example/z").unwrap(); + assert_eq!((u.tls, u.host.as_str(), u.port, u.path.as_str()), (true, "a.example", 443, "/z")); + assert!(Url::parse("Http://a.example/").is_ok()); + let j = Url::parse("https://a.example/start").unwrap().join("HTTPS://cdn.example/file").unwrap(); + assert_eq!((j.tls, j.host.as_str(), j.path.as_str()), (true, "cdn.example", "/file")); + } + #[test] fn parses_urls() { let u = Url::parse("https://cdn.example.com/models/sam3/model.safetensors?sig=1#x").unwrap(); @@ -126,6 +210,26 @@ mod tests { } } + #[test] + fn rejects_signed_ports() { + for bad in ["http://example.com:+80/x", "http://example.com:-80/x", "http://[::1]:+80/x"] { + assert!(Url::parse(bad).is_err(), "{bad}"); + } + assert!(Url::parse("http://a.test/").unwrap().join("http://example.com:+80/x").is_err()); + assert_eq!(Url::parse("http://example.com:80/x").unwrap().port, 80); + } + + #[test] + fn rejects_text_after_a_bracketed_ipv6_host() { + for bad in ["http://[::1]junk/x", "https://[::1]junk/x", "http://[::1]8080/x", "http://[::1]8080"] { + assert!(matches!(Url::parse(bad), Err(HttpError::BadUrl(_))), "{bad}"); + } + let u = Url::parse("http://[::1]:8080/x").unwrap(); + assert_eq!((u.host.as_str(), u.port), ("::1", 8080)); + let u = Url::parse("http://[::1]/x").unwrap(); + assert_eq!((u.host.as_str(), u.port), ("::1", 80)); + } + #[test] fn resolves_redirects() { let u = Url::parse("https://a.example/x/y/file?q").unwrap(); From c247a085be0b2db5456f1f9178024d6a12e4cc57 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 16:50:07 -0500 Subject: [PATCH 5/5] Expose dependency-free HTTP URL syntax with bounded redirect input --- Cargo.lock | 4 ++++ Cargo.toml | 1 + crates/http-url/Cargo.toml | 11 +++++++++ crates/http-url/README.md | 25 ++++++++++++++++++++ crates/http-url/src/lib.rs | 47 ++++++++++++++++++++++++++++++-------- 5 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 crates/http-url/Cargo.toml create mode 100644 crates/http-url/README.md diff --git a/Cargo.lock b/Cargo.lock index b3325f790..b36240d6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1879,6 +1879,10 @@ dependencies = [ "proptest", ] +[[package]] +name = "craft-http-url" +version = "0.1.0" + [[package]] name = "craft-i18n" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0c9729e00..1b409247e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-http-url = { path = "crates/http-url" } craft-smime = { path = "crates/smime" } craft-ber = { path = "crates/ber" } craft-proto = { path = "crates/proto" } diff --git a/crates/http-url/Cargo.toml b/crates/http-url/Cargo.toml new file mode 100644 index 000000000..f23d9bbc7 --- /dev/null +++ b/crates/http-url/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "craft-http-url" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license = "Apache-2.0" +repository.workspace = true +description = "Bounded HTTP URL parsing and redirect resolution without network access" + +[lints] +workspace = true diff --git a/crates/http-url/README.md b/crates/http-url/README.md new file mode 100644 index 000000000..cc3dc1bf5 --- /dev/null +++ b/crates/http-url/README.md @@ -0,0 +1,25 @@ +# craft-http-url + +HTTP/HTTPS URL parsing, dot-segment removal, relative redirects, HTTPS downgrade rejection, +and query-redacted display. No dependencies or network operations. Parsing rejects inputs +larger than 16 KiB and redirects are bounded before concatenation. + +This is not an SSRF filter, DNS resolver or HTTP client. Local/private hosts are allowed. +Applications keep destination permission, authentication, TLS, request/response limits and +redirect-count policy. Public fields preserve the original API; manually constructed values +must not be treated as validated URLs. + +## Attribution + +Extracted from LightCraft `crates/fetch/src/http.rs` at +`2dcb258ba40738230a07484a95eb27dc84ed66fe` (Apache-2.0). +Original parser: Daniel Voshart, `7fa7506db46d69d609e85e39b338f33786ed0293`. +HTTPS downgrade protection: MarekSpodBiedry, `4a91a1c218bf3d0ac49cc73ebb60621a21ae55e2`. +URL normalization, scheme, fragment and authority fixes: NetterSilver, including +`edc369565eef92161b56b8f1e39ef53629c05489`, +`78f0e6e650444886069f4d4ae4e6593f221b98d4`, +`cee93496587d31c514aaeced3e6b8027cfdb41a0`, +`dee5f7910ad8c24cf28bab528f8d007550af2b23`, +`c3a5d9b7f1f837dc14c01942e3d0293ecbf36345` and +`13849307a9343504339d68b086f1a32cdcbbb457`. +Source-author commits are retained separately in this extraction. diff --git a/crates/http-url/src/lib.rs b/crates/http-url/src/lib.rs index 5bde118a3..1e3e5875f 100644 --- a/crates/http-url/src/lib.rs +++ b/crates/http-url/src/lib.rs @@ -1,3 +1,24 @@ +//! Bounded HTTP/HTTPS URL parsing and redirect resolution, extracted from LightCraft. +//! +//! This is syntax handling, not a network permission or SSRF policy. It permits local/private +//! hosts and performs no DNS, socket or TLS operations. Callers own destination allowlists, +//! credential forwarding, redirect counts, connection deadlines and response limits. +//! Only parsed values have validated fields; constructing public fields bypasses parsing. + +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +/// A URL or redirect cannot be represented by this bounded HTTP URL syntax. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct UrlError(pub String); + +impl std::fmt::Display for UrlError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} +impl std::error::Error for UrlError {} + /// A parsed `http://` or `https://` URL. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Url { @@ -46,8 +67,8 @@ fn strip_scheme<'a>(s: &'a str, prefix: &str) -> Option<&'a str> { } impl Url { - pub fn parse(s: &str) -> Result { - let bad = |why: &str| HttpError::BadUrl(why.to_string()); + pub fn parse(s: &str) -> Result { + let bad = |why: &str| UrlError(why.to_string()); if s.len() > 16 * 1024 { return Err(bad("URL exceeds 16 KiB")); } @@ -99,12 +120,16 @@ impl Url { } /// `Location` of a redirect, resolved against this URL. - pub fn join(&self, location: &str) -> Result { + pub fn join(&self, location: &str) -> Result { + // Reject before concatenating a hostile Location with the base URL. + if location.len() > 16 * 1024 || self.host.len().saturating_add(self.path.len()) > 16 * 1024 { + return Err(UrlError("URL exceeds 16 KiB".into())); + } let l = location.trim(); if strip_scheme(l, "http://").is_some() || strip_scheme(l, "https://").is_some() { let url = Url::parse(l)?; if self.tls && !url.tls { - return Err(HttpError::BadUrl("redirect from https to http refused".into())); + return Err(UrlError("redirect from https to http refused".into())); } return Ok(url); } @@ -113,7 +138,7 @@ impl Url { && !scheme.is_empty() && scheme.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')) { - return Err(HttpError::BadUrl(format!("redirect to an unsupported location `{scheme}:`"))); + return Err(UrlError(format!("redirect to an unsupported location `{scheme}:`"))); } let scheme = if self.tls { "https" } else { "http" }; let host = if self.host.contains(':') { format!("[{}]", self.host) } else { self.host.clone() }; @@ -138,7 +163,8 @@ impl Url { Url::parse(&format!("{base}{dir}/{l}")) } - fn host_header(&self) -> String { + /// Authority formatted for an HTTP Host field. + pub fn host_header(&self) -> String { let host = if self.host.contains(':') { format!("[{}]", self.host) } else { self.host.clone() }; let default = if self.tls { 443 } else { 80 }; if self.port == default { host } else { format!("{host}:{}", self.port) } @@ -153,7 +179,8 @@ impl Url { #[cfg(test)] mod tests { - use super::*; + #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + use super::{Url, UrlError}; #[test] fn fragment_only_or_empty_location_keeps_file_name_and_query() { @@ -166,6 +193,9 @@ mod tests { #[test] fn rejects_oversized_and_control_character_urls() { + let base = Url::parse("https://example.com/path").unwrap(); + assert!(base.join(&format!("#{}", "x".repeat(16 * 1024))).is_err()); + assert!(base.join(&"x".repeat(16 * 1024 + 1)).is_err()); assert!(Url::parse(&format!("https://example.com/{}", "x".repeat(16 * 1024))).is_err()); assert!(Url::parse("https://example.com/x?secret=abc\r\nInjected: 1").is_err()); } @@ -222,7 +252,7 @@ mod tests { #[test] fn rejects_text_after_a_bracketed_ipv6_host() { for bad in ["http://[::1]junk/x", "https://[::1]junk/x", "http://[::1]8080/x", "http://[::1]8080"] { - assert!(matches!(Url::parse(bad), Err(HttpError::BadUrl(_))), "{bad}"); + assert!(matches!(Url::parse(bad), Err(UrlError(_))), "{bad}"); } let u = Url::parse("http://[::1]:8080/x").unwrap(); assert_eq!((u.host.as_str(), u.port), ("::1", 8080)); @@ -244,5 +274,4 @@ mod tests { assert!(!plain.join("http://127.0.0.1:9/b").unwrap().tls); assert!(plain.join("https://b.example/z").unwrap().tls); } - }