Skip to content

Commit 3cc6149

Browse files
committed
cli/file_store: Preserve IPv6 URL normalization after Go change
Add a fallback for unbracketed IPv6 literals to preserve behavior. Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
1 parent 55b8888 commit 3cc6149

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

cli/config/credentials/file_store.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,48 @@ func ConvertToHostname(maybeURL string) string {
117117
}
118118
return net.JoinHostPort(u.Hostname(), u.Port())
119119
}
120+
121+
if hostName := hostFromURLFallback(stripped); hostName != "" {
122+
return hostName
123+
}
120124
}
121125
hostName, _, _ := strings.Cut(stripped, "/")
122126
return hostName
123127
}
128+
129+
// hostFromURLFallback extracts a host from scheme URLs that net/url rejects.
130+
// Go rejects unbracketed IPv6 literals in URL hosts since
131+
// https://github.com/golang/go/commit/0c28789bd7dfc55099cac86a3212dda0d6c091f6
132+
func hostFromURLFallback(maybeURL string) string {
133+
_, rest, ok := strings.Cut(maybeURL, "://")
134+
if !ok {
135+
return ""
136+
}
137+
138+
hostName, _, _ := strings.Cut(rest, "/")
139+
if hostName == "" {
140+
return ""
141+
}
142+
143+
if strings.Count(hostName, ":") > 1 && !strings.HasPrefix(hostName, "[") {
144+
portStart := strings.LastIndex(hostName, ":")
145+
addr, port := hostName[:portStart], hostName[portStart+1:]
146+
if addr != "" && isPort(port) {
147+
return net.JoinHostPort(addr, port)
148+
}
149+
}
150+
151+
return hostName
152+
}
153+
154+
func isPort(port string) bool {
155+
if port == "" {
156+
return false
157+
}
158+
for _, r := range port {
159+
if r < '0' || r > '9' {
160+
return false
161+
}
162+
}
163+
return true
164+
}

0 commit comments

Comments
 (0)