Skip to content

Commit 3796df1

Browse files
committed
net/netip: don't accept ParseAddr with leading zeros
Fixes #49365 Updates #30999 Change-Id: Ic92bce01b435baf70574c65524bde82f9cee3d8d Reviewed-on: https://go-review.googlesource.com/c/go/+/361534 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]> Trust: Josh Bleecher Snyder <[email protected]> Trust: Brad Fitzpatrick <[email protected]>
1 parent c58417b commit 3796df1

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/net/netip/netip.go

+6
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,14 @@ func (err parseAddrError) Error() string {
155155
func parseIPv4(s string) (ip Addr, err error) {
156156
var fields [4]uint8
157157
var val, pos int
158+
var digLen int // number of digits in current octet
158159
for i := 0; i < len(s); i++ {
159160
if s[i] >= '0' && s[i] <= '9' {
161+
if digLen == 1 && val == 0 {
162+
return Addr{}, parseAddrError{in: s, msg: "IPv4 field has octet with leading zero"}
163+
}
160164
val = val*10 + int(s[i]) - '0'
165+
digLen++
161166
if val > 255 {
162167
return Addr{}, parseAddrError{in: s, msg: "IPv4 field has value >255"}
163168
}
@@ -175,6 +180,7 @@ func parseIPv4(s string) (ip Addr, err error) {
175180
fields[pos] = uint8(val)
176181
pos++
177182
val = 0
183+
digLen = 0
178184
} else {
179185
return Addr{}, parseAddrError{in: s, msg: "unexpected character", at: s[i:]}
180186
}

src/net/netip/netip_test.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ var (
2929

3030
func TestParseAddr(t *testing.T) {
3131
var validIPs = []struct {
32-
in string
33-
ip Addr // output of ParseAddr()
34-
str string // output of String(). If "", use in.
32+
in string
33+
ip Addr // output of ParseAddr()
34+
str string // output of String(). If "", use in.
35+
wantErr string
3536
}{
3637
// Basic zero IPv4 address.
3738
{
@@ -45,15 +46,18 @@ func TestParseAddr(t *testing.T) {
4546
},
4647
// IPv4 address in windows-style "print all the digits" form.
4748
{
48-
in: "010.000.015.001",
49-
ip: MkAddr(Mk128(0, 0xffff0a000f01), Z4),
50-
str: "10.0.15.1",
49+
in: "010.000.015.001",
50+
wantErr: `ParseAddr("010.000.015.001"): IPv4 field has octet with leading zero`,
5151
},
5252
// IPv4 address with a silly amount of leading zeros.
5353
{
54-
in: "000001.00000002.00000003.000000004",
55-
ip: MkAddr(Mk128(0, 0xffff01020304), Z4),
56-
str: "1.2.3.4",
54+
in: "000001.00000002.00000003.000000004",
55+
wantErr: `ParseAddr("000001.00000002.00000003.000000004"): IPv4 field has octet with leading zero`,
56+
},
57+
// 4-in-6 with octet with leading zero
58+
{
59+
in: "::ffff:1.2.03.4",
60+
wantErr: `ParseAddr("::ffff:1.2.03.4"): ParseAddr("1.2.03.4"): IPv4 field has octet with leading zero (at "1.2.03.4")`,
5761
},
5862
// Basic zero IPv6 address.
5963
{
@@ -121,10 +125,16 @@ func TestParseAddr(t *testing.T) {
121125
t.Run(test.in, func(t *testing.T) {
122126
got, err := ParseAddr(test.in)
123127
if err != nil {
128+
if err.Error() == test.wantErr {
129+
return
130+
}
124131
t.Fatal(err)
125132
}
133+
if test.wantErr != "" {
134+
t.Fatalf("wanted error %q; got none", test.wantErr)
135+
}
126136
if got != test.ip {
127-
t.Errorf("ParseAddr(%q) got %#v, want %#v", test.in, got, test.ip)
137+
t.Errorf("got %#v, want %#v", got, test.ip)
128138
}
129139

130140
// Check that ParseAddr is a pure function.
@@ -963,7 +973,7 @@ func TestIs4In6(t *testing.T) {
963973
{mustIP("::ffff:192.0.2.128"), true, mustIP("192.0.2.128")},
964974
{mustIP("::ffff:192.0.2.128%eth0"), true, mustIP("192.0.2.128")},
965975
{mustIP("::fffe:c000:0280"), false, mustIP("::fffe:c000:0280")},
966-
{mustIP("::ffff:127.001.002.003"), true, mustIP("127.1.2.3")},
976+
{mustIP("::ffff:127.1.2.3"), true, mustIP("127.1.2.3")},
967977
{mustIP("::ffff:7f01:0203"), true, mustIP("127.1.2.3")},
968978
{mustIP("0:0:0:0:0000:ffff:127.1.2.3"), true, mustIP("127.1.2.3")},
969979
{mustIP("0:0:0:0:000000:ffff:127.1.2.3"), true, mustIP("127.1.2.3")},

0 commit comments

Comments
 (0)