servo / rust-url

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

Invalid IPv4 but parsing success #892

Closed merc closed 9 months ago

merc commented 9 months ago

Description: Hi, I have a question, when attempt to parse an invalid "domain" in URL the result become valid IPv4:

println!("{:#?}", Url::parse("http://50.500/path"));

Result:

Ok(
    Url {
        scheme: "http",
        cannot_be_a_base: false,
        username: "",
        password: None,
        host: Some(
            Ipv4(
                50.0.1.244,
            ),
        ),
        port: None,
        path: "/path",
        query: None,
        fragment: None,
    },
)

Is this intended?

valenting commented 9 months ago

Unfortunately this is intended behaviour. The reference parser agrees. I say unfortunately because the expansion of numbers into IP addresses is a legacy from decades ago, and it often comes up as being confusing. The URL standard steps to parse and expand the number into an IP address is https://url.spec.whatwg.org/#concept-ipv4-parser

This issue affects other URL parsers as well. See this post on this exact behaviour in curl: https://daniel.haxx.se/blog/2021/04/19/curl-those-funny-ipv4-addresses/