servo / rust-url

URL parser for Rust
https://docs.rs/url/
Apache License 2.0
1.32k stars 330 forks source link

host:port string with no scheme parses invalidly #613

Open treebly opened 4 years ago

treebly commented 4 years ago

Trying to parse a string such as a.local.com:3000 should fail, as it does not have a scheme. However, it is allowed to parse, and when .scheme() is called on the resulting Url struct. a.local.com is returned as the scheme. This can cause unexpected behavior and allow invalid URLs to slip through as valid.

This seems to be related to this issue.

Code Sample:

use url::Url;
fn main() {
    let url = Url::parse("a.local.com:3000").unwrap();
    println!("{}, scheme: {}", url.to_string(), url.scheme());
}

Output: a.local.com:3000, scheme: a.local.com

Plecra commented 4 years ago

According to the spec, a.local.com is actually a valid scheme, making a.local.com:3000 a valid Url. Maybe the documentation could make it clearer that HTTP headers and user inputs will have to be normalized based on the appropriate heuristics.

roustem commented 4 years ago

Go seems to handle it in a similar way: https://play.golang.org/p/VI4EFKVIUA2