Skip to content

net: inconsistent behaviour with IPv4-mapped IPv6 CIDRs #51906

@adam-p

Description

@adam-p

What version of Go are you using (go version)?

$ go version
go version go1.18 windows/amd64

Same behaviour on playground.

Does this issue reproduce with the latest release?

Yes.

What did you do?

https://go.dev/play/p/tohTC3rXoZt

	cidrString := "::ffff:4.4.4.4/64"
	fmt.Println(cidrString)
	_, cidr, _ := net.ParseCIDR(cidrString)
	fmt.Println(cidr)              // == ::/64, seems like it could be the problem
	ip := net.ParseIP("::ffff:4.4.4.4")
	fmt.Println(ip)
	fmt.Println(cidr.Contains(ip)) // == false, seems wrong

	ip = net.ParseIP("4.4.4.4")
	fmt.Println(ip)
	fmt.Println(cidr.Contains(ip)) // == false, seems maybe wrong

	fmt.Println("---")

	cidrString = "::ffff:4.4.4.4/124"
	fmt.Println(cidrString)
	_, cidr, _ = net.ParseCIDR(cidrString)
	fmt.Println(cidr)
	ip = net.ParseIP("::ffff:4.4.4.4")
	fmt.Println(ip)
	fmt.Println(cidr.Contains(ip)) // == true, seems right

	ip = net.ParseIP("4.4.4.4")
	fmt.Println(ip)
	fmt.Println(cidr.Contains(ip)) // == true, seems right

What did you expect to see?

That ::ffff:4.4.4.4/64 would contain ::ffff:4.4.4.4 and 4.4.4.4 (or at least one of them, like netip.Prefix).

What did you see instead?

It doesn't.

My guess is that the problem is that ::ffff:4.4.4.4/64 is turning into the IPv6 CIDR ::/64, but ::ffff:4.4.4.4 is turning into the IPv4 4.4.4.4. And the v4 IP will never be contained in the v6 CIDR. In contrast, ::ffff:4.4.4.4/124 turns into a v4 CIDR and contains the IPs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions