Skip to content

Commit 2ba2129

Browse files
committed
Scope IPv6 literal handling to challenge lookups
1 parent 68acab0 commit 2ba2129

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

certificates.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,6 @@ func isInternalIP(addr string) bool {
651651
func hostOnly(hostport string) string {
652652
host, _, err := net.SplitHostPort(hostport)
653653
if err != nil {
654-
if len(hostport) > 2 && hostport[0] == '[' && hostport[len(hostport)-1] == ']' {
655-
return hostport[1 : len(hostport)-1]
656-
}
657654
return hostport // OK; probably had no port to begin with
658655
}
659656
return host

httphandlers.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ func (am *ACMEIssuer) HandleHTTPChallenge(w http.ResponseWriter, r *http.Request
6464
return am.distributedHTTPChallengeSolver(w, r)
6565
}
6666

67+
func challengeIdentifierHost(hostport string) string {
68+
host := hostOnly(hostport)
69+
if inner, ok := strings.CutPrefix(host, "["); ok {
70+
if inner, ok := strings.CutSuffix(inner, "]"); ok {
71+
return inner
72+
}
73+
}
74+
return host
75+
}
76+
6777
// distributedHTTPChallengeSolver checks to see if this challenge
6878
// request was initiated by this or another instance which uses the
6979
// same storage as am does, and attempts to complete the challenge for
@@ -72,7 +82,7 @@ func (am *ACMEIssuer) distributedHTTPChallengeSolver(w http.ResponseWriter, r *h
7282
if am == nil {
7383
return false
7484
}
75-
host := hostOnly(r.Host)
85+
host := challengeIdentifierHost(r.Host)
7686
chalInfo, distributed, err := am.config.getACMEChallengeInfo(r.Context(), host, !am.DisableDistributedSolvers)
7787
if err != nil {
7888
if am.DisableDistributedSolvers {
@@ -178,7 +188,7 @@ func allBase64URL(s string) bool {
178188
func solveHTTPChallenge(logger *zap.Logger, w http.ResponseWriter, r *http.Request, challenge acme.Challenge, distributed bool) bool {
179189
challengeReqPath := challenge.HTTP01ResourcePath()
180190
if r.URL.Path == challengeReqPath &&
181-
strings.EqualFold(hostOnly(r.Host), challenge.Identifier.Value) && // mitigate DNS rebinding attacks
191+
strings.EqualFold(challengeIdentifierHost(r.Host), challenge.Identifier.Value) && // mitigate DNS rebinding attacks
182192
r.Method == http.MethodGet {
183193
w.Header().Add("Content-Type", "text/plain")
184194
w.Write([]byte(challenge.KeyAuthorization))
@@ -249,7 +259,7 @@ func (iss *ZeroSSLIssuer) distributedHTTPValidationAnswer(w http.ResponseWriter,
249259
if logger == nil {
250260
logger = zap.NewNop()
251261
}
252-
host := hostOnly(r.Host)
262+
host := challengeIdentifierHost(r.Host)
253263
valInfo, distributed, err := iss.getDistributedValidationInfo(r.Context(), host)
254264
if err != nil {
255265
logger.Warn("looking up info for HTTP validation",

0 commit comments

Comments
 (0)