Skip to content

Commit f83bbaf

Browse files
mauri870bradfitz
authored andcommitted
net/netip: allow only valid prefix digits in ParsePrefix
The prefix bits for a call to ParsePrefix are passed raw to strconv.Atoi, this means that it can accept +- signs as well as leading zeroes, which are not allowed prefix values following RFC 4632 Section 3.1 and RFC 4291 Section 2.3. Validate non-digit characters as well as leading zeroes and return an error accordingly. Fixes golang#63850 Change-Id: I412a7e1cecc6ee9ea1582d4b04cb40d79ee714f1 GitHub-Last-Rev: 462d97f GitHub-Pull-Request: golang#63859 Reviewed-on: https://go-review.googlesource.com/c/go/+/538860 Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 8a360d6 commit f83bbaf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/net/netip/netip.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,12 @@ func ParsePrefix(s string) (Prefix, error) {
13091309
}
13101310

13111311
bitsStr := s[i+1:]
1312+
1313+
// strconv.Atoi accepts a leading sign and leading zeroes, but we don't want that.
1314+
if len(bitsStr) > 1 && (bitsStr[0] < '1' || bitsStr[0] > '9') {
1315+
return Prefix{}, errors.New("netip.ParsePrefix(" + strconv.Quote(s) + "): bad bits after slash: " + strconv.Quote(bitsStr))
1316+
}
1317+
13121318
bits, err := strconv.Atoi(bitsStr)
13131319
if err != nil {
13141320
return Prefix{}, errors.New("netip.ParsePrefix(" + strconv.Quote(s) + "): bad bits after slash: " + strconv.Quote(bitsStr))

src/net/netip/netip_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ func TestParsePrefixError(t *testing.T) {
14561456
},
14571457
{
14581458
prefix: "1.1.1.0/-1",
1459-
errstr: "out of range",
1459+
errstr: "bad bits",
14601460
},
14611461
{
14621462
prefix: "1.1.1.0/33",
@@ -1475,6 +1475,22 @@ func TestParsePrefixError(t *testing.T) {
14751475
prefix: "2001:db8::%a/32",
14761476
errstr: "zones cannot be present",
14771477
},
1478+
{
1479+
prefix: "1.1.1.0/+32",
1480+
errstr: "bad bits",
1481+
},
1482+
{
1483+
prefix: "1.1.1.0/-32",
1484+
errstr: "bad bits",
1485+
},
1486+
{
1487+
prefix: "1.1.1.0/032",
1488+
errstr: "bad bits",
1489+
},
1490+
{
1491+
prefix: "1.1.1.0/0032",
1492+
errstr: "bad bits",
1493+
},
14781494
}
14791495
for _, test := range tests {
14801496
t.Run(test.prefix, func(t *testing.T) {

0 commit comments

Comments
 (0)