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); } - }