Skip to content

Commit 19d2103

Browse files
rolandshoemakergopherbot
authored andcommitted
[release-branch.go1.22] crypto/x509: properly check for IPv6 hosts in URIs
When checking URI constraints, use netip.ParseAddr, which understands zones, unlike net.ParseIP which chokes on them. This prevents zone IDs from mistakenly satisfying URI constraints. Thanks to Juho Forsén of Mattermost for reporting this issue. For golang#71156 Fixes golang#71207 Fixes CVE-2024-45341 Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/1700 Reviewed-by: Tatiana Bradley <tatianabradley@google.com> Reviewed-by: Damien Neil <dneil@google.com> Change-Id: I1d97723e0f29fcf1404fb868ba0495282da70f6e Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/1780 Reviewed-by: Roland Shoemaker <bracewell@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/643105 TryBot-Bypass: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com>
1 parent ae9996f commit 19d2103

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/crypto/x509/name_constraints_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,6 +1599,24 @@ var nameConstraintsTests = []nameConstraintsTest{
15991599
cn: "foo.bar",
16001600
},
16011601
},
1602+
1603+
// #86: URIs with IPv6 addresses with zones and ports are rejected
1604+
{
1605+
roots: []constraintsSpec{
1606+
{
1607+
ok: []string{"uri:example.com"},
1608+
},
1609+
},
1610+
intermediates: [][]constraintsSpec{
1611+
{
1612+
{},
1613+
},
1614+
},
1615+
leaf: leafSpec{
1616+
sans: []string{"uri:http://[2006:abcd::1%25.example.com]:16/"},
1617+
},
1618+
expectedError: "URI with IP",
1619+
},
16021620
}
16031621

16041622
func makeConstraintsCACert(constraints constraintsSpec, name string, key *ecdsa.PrivateKey, parent *Certificate, parentKey *ecdsa.PrivateKey) (*Certificate, error) {

src/crypto/x509/verify.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"net"
14+
"net/netip"
1415
"net/url"
1516
"reflect"
1617
"runtime"
@@ -429,8 +430,10 @@ func matchURIConstraint(uri *url.URL, constraint string) (bool, error) {
429430
}
430431
}
431432

432-
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") ||
433-
net.ParseIP(host) != nil {
433+
// netip.ParseAddr will reject the URI IPv6 literal form "[...]", so we
434+
// check if _either_ the string parses as an IP, or if it is enclosed in
435+
// square brackets.
436+
if _, err := netip.ParseAddr(host); err == nil || (strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]")) {
434437
return false, fmt.Errorf("URI with IP (%q) cannot be matched against constraints", uri.String())
435438
}
436439

0 commit comments

Comments
 (0)