Skip to content

Commit 8f863af

Browse files
Use net.IP.IsLoopback() to match loopback addresses
- changed the `MatchLocalhost` function in remotes/docker/registry.go - Make use of SplitHostPort to split host and port number - Added additional tests for modified code in remotes/docker/registry_test.go - Note: this does not handle mathcing of IP addresses in octal, decimal or hex format or a mix of these. Fixes: #5129 Signed-off-by: Madhav Jivrajani <[email protected]>
1 parent 2b1e913 commit 8f863af

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

remotes/docker/registry.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package docker
1818

1919
import (
20+
"net"
2021
"net/http"
22+
23+
"github.com/pkg/errors"
2124
)
2225

2326
// HostCapabilities represent the capabilities of the registry
@@ -201,12 +204,41 @@ func MatchAllHosts(string) (bool, error) {
201204

202205
// MatchLocalhost is a host match function which returns true for
203206
// localhost.
207+
//
208+
// Note: this does not handle matching of ip addresses in octal,
209+
// decimal or hex form.
204210
func MatchLocalhost(host string) (bool, error) {
205-
for _, s := range []string{"localhost", "127.0.0.1", "[::1]"} {
206-
if len(host) >= len(s) && host[0:len(s)] == s && (len(host) == len(s) || host[len(s)] == ':') {
207-
return true, nil
211+
switch {
212+
case host == "::1":
213+
return true, nil
214+
case host == "[::1]":
215+
return true, nil
216+
}
217+
h, p, err := net.SplitHostPort(host)
218+
219+
// addrError helps distinguish between errors of form
220+
// "no colon in address" and "too many colons in address".
221+
// The former is fine as the host string need not have a
222+
// port. Latter needs to be handled.
223+
addrError := &net.AddrError{
224+
Err: "missing port in address",
225+
Addr: host,
226+
}
227+
if err != nil {
228+
if err.Error() != addrError.Error() {
229+
return false, err
208230
}
231+
// host string without any port specified
232+
h = host
233+
} else if len(p) == 0 {
234+
return false, errors.New("invalid host name format")
235+
}
236+
237+
// use ipv4 dotted decimal for further checking
238+
if h == "localhost" {
239+
h = "127.0.0.1"
209240
}
210-
return host == "::1", nil
241+
ip := net.ParseIP(h)
211242

243+
return ip.IsLoopback(), nil
212244
}

remotes/docker/registry_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ func TestMatchLocalhost(t *testing.T) {
5252
match bool
5353
}{
5454
{"", false},
55-
{"127.1.1.1", false},
55+
{"127.1.1.1", true},
5656
{"127.0.0.1", true},
57+
{"127.256.0.1", false}, // test MatchLocalhost does not panic on invalid ip
58+
{"127.23.34.52", true},
5759
{"127.0.0.1:5000", true},
5860
{"registry.org", false},
61+
{"126.example.com", false},
5962
{"localhost", true},
6063
{"localhost:5000", true},
6164
{"[127:0:0:1]", false},
6265
{"[::1]", true},
66+
{"[::1]:", false}, // invalid ip
67+
{"127.0.1.1:", false}, // invalid ip
6368
{"[::1]:5000", true},
6469
{"::1", true},
6570
} {

0 commit comments

Comments
 (0)