Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Reject signed ports in Url::parse

NetterSilver13849307a934
diff --git a/crates/fetch/src/http.rs b/crates/fetch/src/http.rs
index c95478388..ee1141652 100644
--- a/crates/fetch/src/http.rs
+++ b/crates/fetch/src/http.rs
@@ -137,7 +137,8 @@ impl Url {
}
};
let port = match port {
- Some(p) => p.parse::<u16>().ok().filter(|p| *p != 0).ok_or_else(|| bad("bad port"))?,
+ Some(p) if p.bytes().all(|b| b.is_ascii_digit()) => p.parse::<u16>().ok().filter(|p| *p != 0).ok_or_else(|| bad("bad port"))?,
+ Some(_) => return Err(bad("bad port")),
None if tls => 443,
None => 80,
};
@@ -713,6 +714,15 @@ 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"] {