Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::net::Ipv6Addr::from_str accepts "::f:f:f:f:f:f:f:f" but rejects "f:f:f:f:f:f:f:f::" #46263

Closed
l4AgXc opened this issue Nov 25, 2017 · 2 comments
Labels
C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@l4AgXc
Copy link

l4AgXc commented Nov 25, 2017

When all eight segments are present in an IPv6 address specification, std::net::Ipv6Addr::from_str allows :: anywhere, except at the very end.

Here is a test program:

use std::net;
fn main() {
    let tests = [
        "::f:f:f:f:f:f:f:f",
        "f::f:f:f:f:f:f:f",
        "f:f::f:f:f:f:f:f",
        "f:f:f::f:f:f:f:f",
        "f:f:f:f::f:f:f:f",
        "f:f:f:f:f::f:f:f",
        "f:f:f:f:f:f::f:f",
        "f:f:f:f:f:f:f::f",
        "f:f:f:f:f:f:f:f::",
    ];
    for test in tests.iter() {
        match test.parse::<net::Ipv6Addr>() {
            Ok(addr) => println!("{:?}\t{}", test, addr),
            Err(err) => println!("{:?}\t{}", test, err),
        }
    }
}

The output is:

"::f:f:f:f:f:f:f:f"	f:f:f:f:f:f:f:f
"f::f:f:f:f:f:f:f"	f:f:f:f:f:f:f:f
"f:f::f:f:f:f:f:f"	f:f:f:f:f:f:f:f
"f:f:f::f:f:f:f:f"	f:f:f:f:f:f:f:f
"f:f:f:f::f:f:f:f"	f:f:f:f:f:f:f:f
"f:f:f:f:f::f:f:f"	f:f:f:f:f:f:f:f
"f:f:f:f:f:f::f:f"	f:f:f:f:f:f:f:f
"f:f:f:f:f:f:f::f"	f:f:f:f:f:f:f:f
"f:f:f:f:f:f:f:f::"	invalid IP address syntax

Meta

rustc 1.21.0
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.21.0
LLVM version: 4.0
@kennytm kennytm added C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 25, 2017
@l4AgXc
Copy link
Author

l4AgXc commented Nov 25, 2017

The grammar in RFC 3986 (URI syntax) says that all these cases should be rejected; i.e., :: is not allowed to expand to nothing.

IPv6address =                            6( h16 ":" ) ls32
            /                       "::" 5( h16 ":" ) ls32
            / [               h16 ] "::" 4( h16 ":" ) ls32
            / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
            / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
            / [ *3( h16 ":" ) h16 ] "::"    h16 ":"   ls32
            / [ *4( h16 ":" ) h16 ] "::"              ls32
            / [ *5( h16 ":" ) h16 ] "::"              h16
            / [ *6( h16 ":" ) h16 ] "::"

ls32        = ( h16 ":" h16 ) / IPv4address
            ; least-significant 32 bits of address

h16         = 1*4HEXDIG
            ; 16 bits of address represented in hexadecimal

@kennytm
Copy link
Member

kennytm commented Nov 25, 2017

I think all of them should be rejected because according to rfc4291 §2.2,

The use of "::" indicates one or more groups of 16 bits of zeros.

At least both Python3's ipaddress.ip_address function and Go's net.ParseIP function doesn't recognize f:f:f:f::f:f:f:f.

varkor added a commit to varkor/rust that referenced this issue Dec 11, 2017
kennytm added a commit to kennytm/rust that referenced this issue Dec 20, 2017
Reject superfluous `::` in IPv6 addresses

Fixes rust-lang#46263.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants