servo / rust-url

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

Why is IP convert to Domain #929

Closed cn-kali-team closed 1 month ago

cn-kali-team commented 1 month ago

Describe the bug A clear and concise description of what the bug is. Include code snippets if possible.

url::Url::parse("socks5://user:pass@127.0.0.1:1080")

Url { scheme: "socks5", cannot_be_a_base: false, username: "user", password: Some("pass"), host: Some(Domain("127.0.0.1")), port: Some(1080), path: "/", query: None, fragment: None }

Manishearth commented 1 month ago

An IP address is a domain.

cn-kali-team commented 1 month ago

But according to the document description, it should be expected to be IPv4.

https://docs.rs/url/latest/url/struct.Url.html#method.domain

valenting commented 1 month ago

When the scheme of a URL is not special, the host will be an opaque host https://url.spec.whatwg.org/#concept-opaque-host-parser

That applies for IP addresses as well - while for special schemes, the host needs to be valid, you can define your own scheme where the host looks like an IPv4 address, but isn't.

Example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=38f255771a254e3e73f1e7bd9dd4f786

    let _ = dbg!(url::Url::parse("lotsofdots://user:pass@1.2.3.4.5.6.7.8.9.0:1080")); // succeeds
    let _ = dbg!(url::Url::parse("http://user:pass@1.2.3.4.5.6.7.8.9.0:1080")); // fails

So even though the host of a socks5 URL is supposed to be a valid IP address, the URL parser doesn't know anything about this scheme - so it's parsed as an opaque host - if you want to turn it into a domain you can process it yourself by parsing it with domain.parse::<Ipv4Addr>()