This does a less restrictive (and more correct) check on the IP address, and also allows the fully qualified "localhost.".
This commit is contained in:
parent
bea3c01772
commit
7872bfa173
@ -1560,9 +1560,14 @@ func addressIsLocalhost(addr string) bool {
|
|||||||
host = addr
|
host = addr
|
||||||
}
|
}
|
||||||
switch strings.ToLower(host) {
|
switch strings.ToLower(host) {
|
||||||
case "127.0.0.1", "::1", "localhost":
|
case "localhost", "localhost.":
|
||||||
return true
|
return true
|
||||||
default:
|
default:
|
||||||
return false
|
ip := net.ParseIP(host)
|
||||||
|
if ip == nil {
|
||||||
|
// not an IP address
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return ip.IsLoopback()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -842,16 +842,24 @@ func TestAddressIsLocalhost(t *testing.T) {
|
|||||||
// These are all valid localhost addresses
|
// These are all valid localhost addresses
|
||||||
{"localhost", true},
|
{"localhost", true},
|
||||||
{"LOCALHOST", true},
|
{"LOCALHOST", true},
|
||||||
|
{"localhost.", true},
|
||||||
{"::1", true},
|
{"::1", true},
|
||||||
{"127.0.0.1", true},
|
{"127.0.0.1", true},
|
||||||
|
{"127.23.45.56", true},
|
||||||
{"localhost:8080", true},
|
{"localhost:8080", true},
|
||||||
{"LOCALHOST:8000", true},
|
{"LOCALHOST:8000", true},
|
||||||
|
{"localhost.:8080", true},
|
||||||
{"[::1]:8080", true},
|
{"[::1]:8080", true},
|
||||||
{"127.0.0.1:8080", true},
|
{"127.0.0.1:8080", true},
|
||||||
|
{"127.23.45.56:8080", true},
|
||||||
|
|
||||||
// These are all non-localhost addresses
|
// These are all non-localhost addresses
|
||||||
{"example.com", false},
|
{"example.com", false},
|
||||||
{"example.com:8080", false},
|
{"example.com:8080", false},
|
||||||
|
{"localhost.com", false},
|
||||||
|
{"localhost.com:8080", false},
|
||||||
|
{"www.localhost", false},
|
||||||
|
{"www.localhost:8080", false},
|
||||||
{"192.0.2.10", false},
|
{"192.0.2.10", false},
|
||||||
{"192.0.2.10:8080", false},
|
{"192.0.2.10:8080", false},
|
||||||
{"0.0.0.0", false},
|
{"0.0.0.0", false},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user