]> Cypherpunks repositories - gostls13.git/commitdiff
net/ip: proper ipv6 address parsing
authorMarin Petrunic <marin.petrunic@gmail.com>
Wed, 1 Feb 2023 15:58:43 +0000 (15:58 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 7 Mar 2024 01:17:29 +0000 (01:17 +0000)
Fixes #57760

Change-Id: Ic3698a18e1c80833b07e0e06bc7328d9714794c6
GitHub-Last-Rev: d185467491e2bfc9fb68e48b1193581bebd7d77f
GitHub-Pull-Request: golang/go#57761
Reviewed-on: https://go-review.googlesource.com/c/go/+/461605
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Damien Neil <dneil@google.com>

src/net/ip_test.go
src/net/netip/netip.go
src/net/netip/netip_test.go
src/net/netip/slow_test.go

index acc2310be164e90510f71a936f63b8f4a0aba4a7..11c0b752463a112d0f57b4a0c05db8fdc6bdfe68 100644 (file)
@@ -21,7 +21,6 @@ var parseIPTests = []struct {
        {"::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
        {"::ffff:7f01:0203", IPv4(127, 1, 2, 3)},
        {"0:0:0:0:0000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
-       {"0:0:0:0:000000:ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
        {"0:0:0:0::ffff:127.1.2.3", IPv4(127, 1, 2, 3)},
 
        {"2001:4860:0:2001::68", IP{0x20, 0x01, 0x48, 0x60, 0, 0, 0x20, 0x01, 0, 0, 0, 0, 0, 0, 0x00, 0x68}},
@@ -37,6 +36,10 @@ var parseIPTests = []struct {
        {"fe80::1%lo0", nil},
        {"fe80::1%911", nil},
        {"", nil},
+       //6 zeroes in one group
+       {"0:0:0:0:000000:ffff:127.1.2.3", nil},
+       //5 zeroes in one group edge case
+       {"0:0:0:0:00000:ffff:127.1.2.3", nil},
        {"a1:a2:a3:a4::b1:b2:b3:b4", nil}, // Issue 6628
        {"127.001.002.003", nil},
        {"::ffff:127.001.002.003", nil},
index 7d816b3c645cc47b59cf7e50c332db1b71e9cd8d..d709c56dfa8b479cd07ff00dc7da6218c3c22bb6 100644 (file)
@@ -251,6 +251,10 @@ func parseIPv6(in string) (Addr, error) {
                        } else {
                                break
                        }
+                       if off > 3 {
+                               //more than 4 digits in group, fail.
+                               return Addr{}, parseAddrError{in: in, msg: "each group must have 4 or less digits", at: s}
+                       }
                        if acc > math.MaxUint16 {
                                // Overflow, fail.
                                return Addr{}, parseAddrError{in: in, msg: "IPv6 field has value >=2^16", at: s}
index a4ba533343cd754c122a6a0d5c159f4e49f03d41..e75f07d8c292625a875911708c4578742fde2668 100644 (file)
@@ -274,6 +274,10 @@ func TestParseAddr(t *testing.T) {
                "fe80:1?:1",
                // IPv6 with truncated bytes after single colon.
                "fe80:",
+               // IPv6 with 5 zeros in last group
+               "0:0:0:0:0:ffff:0:00000",
+               // IPv6 with 5 zeros in one group and embedded IPv4
+               "0:0:0:0:00000:ffff:127.1.2.3",
        }
 
        for _, s := range invalidIPs {
@@ -1247,7 +1251,6 @@ func TestIs4In6(t *testing.T) {
                {mustIP("::ffff:127.1.2.3"), true, mustIP("127.1.2.3")},
                {mustIP("::ffff:7f01:0203"), true, mustIP("127.1.2.3")},
                {mustIP("0:0:0:0:0000:ffff:127.1.2.3"), true, mustIP("127.1.2.3")},
-               {mustIP("0:0:0:0:000000:ffff:127.1.2.3"), true, mustIP("127.1.2.3")},
                {mustIP("0:0:0:0::ffff:127.1.2.3"), true, mustIP("127.1.2.3")},
                {mustIP("::1"), false, mustIP("::1")},
                {mustIP("1.2.3.4"), false, mustIP("1.2.3.4")},
index d7c802516416da9a89628f23ad2c6a9368b87ceb..a05f39de74e10db9f695e36df6a3e13565d0989b 100644 (file)
@@ -182,6 +182,9 @@ func parseIPv4Slow(s string) (Addr, error) {
 // parseWord converts a 16-bit hex string into its corresponding
 // two-byte value.
 func parseWord(s string) (byte, byte, error) {
+       if(len(s) > 4) {
+               return 0, 0, fmt.Errorf("parseWord(%q): invalid word", s)
+       }
        ret, err := strconv.ParseUint(s, 16, 16)
        if err != nil {
                return 0, 0, err