Skip to content
Snippets Groups Projects
Commit 19d21034 authored by Roland Shoemaker's avatar Roland Shoemaker Committed by Gopher Robot
Browse files

[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 #71156
Fixes #71207
Fixes CVE-2024-45341

Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/1700


Reviewed-by: default avatarTatiana Bradley <tatianabradley@google.com>
Reviewed-by: default avatarDamien Neil <dneil@google.com>
Change-Id: I1d97723e0f29fcf1404fb868ba0495282da70f6e
Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/1780


Reviewed-by: default avatarRoland Shoemaker <bracewell@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/643105


TryBot-Bypass: Michael Knyszek <mknyszek@google.com>
Reviewed-by: default avatarMichael Pratt <mpratt@google.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
parent ae9996f9
Branches
Tags
No related merge requests found
...@@ -1599,6 +1599,24 @@ var nameConstraintsTests = []nameConstraintsTest{ ...@@ -1599,6 +1599,24 @@ var nameConstraintsTests = []nameConstraintsTest{
cn: "foo.bar", cn: "foo.bar",
}, },
}, },
// #86: URIs with IPv6 addresses with zones and ports are rejected
{
roots: []constraintsSpec{
{
ok: []string{"uri:example.com"},
},
},
intermediates: [][]constraintsSpec{
{
{},
},
},
leaf: leafSpec{
sans: []string{"uri:http://[2006:abcd::1%25.example.com]:16/"},
},
expectedError: "URI with IP",
},
} }
func makeConstraintsCACert(constraints constraintsSpec, name string, key *ecdsa.PrivateKey, parent *Certificate, parentKey *ecdsa.PrivateKey) (*Certificate, error) { func makeConstraintsCACert(constraints constraintsSpec, name string, key *ecdsa.PrivateKey, parent *Certificate, parentKey *ecdsa.PrivateKey) (*Certificate, error) {
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"net/netip"
"net/url" "net/url"
"reflect" "reflect"
"runtime" "runtime"
...@@ -429,8 +430,10 @@ func matchURIConstraint(uri *url.URL, constraint string) (bool, error) { ...@@ -429,8 +430,10 @@ func matchURIConstraint(uri *url.URL, constraint string) (bool, error) {
} }
} }
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") || // netip.ParseAddr will reject the URI IPv6 literal form "[...]", so we
net.ParseIP(host) != nil { // check if _either_ the string parses as an IP, or if it is enclosed in
// square brackets.
if _, err := netip.ParseAddr(host); err == nil || (strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]")) {
return false, fmt.Errorf("URI with IP (%q) cannot be matched against constraints", uri.String()) return false, fmt.Errorf("URI with IP (%q) cannot be matched against constraints", uri.String())
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment