Skip to content

Commit

Permalink
smx509: properly check for IPv6 hosts in URIs #296
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Jan 17, 2025
1 parent cb51b36 commit 50a5e49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 17 additions & 0 deletions smx509/name_constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,23 @@ var nameConstraintsTests = []nameConstraintsTest{
leaf: leafSpec{sans: []string{"dns:.example.com"}},
expectedError: "cannot parse dnsName \".example.com\"",
},
// #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) {
Expand Down
13 changes: 8 additions & 5 deletions smx509/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"net"
"net/netip"
"net/url"
"reflect"
"runtime"
Expand Down Expand Up @@ -260,7 +261,7 @@ func domainToReverseLabels(domain string) (reverseLabels []string, ok bool) {
// domain is prefixed with an empty label, append an empty
// string to reverseLabels to indicate this.
reverseLabels = append(reverseLabels, "")
}
}
}
}

Expand Down Expand Up @@ -324,8 +325,10 @@ func matchURIConstraint(uri *url.URL, constraint string) (bool, error) {
}
}

if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") ||
net.ParseIP(host) != nil {
// netip.ParseAddr will reject the URI IPv6 literal form "[...]", so we
// 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())
}

Expand Down Expand Up @@ -784,7 +787,7 @@ func (c *Certificate) buildChains(currentChain []*Certificate, sigChecks *int, o
)

considerCandidate := func(certType int, candidate potentialParent) {
if candidate.cert.PublicKey == nil ||alreadyInChain(candidate.cert, currentChain) {
if candidate.cert.PublicKey == nil || alreadyInChain(candidate.cert, currentChain) {
return
}

Expand Down Expand Up @@ -823,7 +826,7 @@ func (c *Certificate) buildChains(currentChain []*Certificate, sigChecks *int, o
return
}
}

switch certType {
case rootCertificate:
chains = append(chains, appendToFreshChain(currentChain, candidate.cert))
Expand Down

0 comments on commit 50a5e49

Please sign in to comment.