Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolver/dns: error if target ends with a colon instead of assuming the default port #2150

Merged
merged 3 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions resolver/dns/dns_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ const (
)

var (
errMissingAddr = errors.New("missing address")
errMissingAddr = errors.New("dns resolver: missing address")

// Addresses ending with a colon that is supposed to be the separator
// between host and port is not allowed. E.g. "::" is a valid address as
// it is an IPv6 address (host only) and "[::]:" is invalid as it ends with
// a colon as the host and port separator
errEndsWithColon = errors.New("dns resolver: missing port after port-separator colon")
)

// NewBuilder creates a dnsBuilder which is used to factory DNS resolvers.
Expand Down Expand Up @@ -297,7 +303,6 @@ func formatIP(addr string) (addrIP string, ok bool) {
// target: "ipv4-host:80" returns host: "ipv4-host", port: "80"
// target: "[ipv6-host]" returns host: "ipv6-host", port: "443"
// target: ":80" returns host: "localhost", port: "80"
// target: ":" returns host: "localhost", port: "443"
func parseTarget(target string) (host, port string, err error) {
if target == "" {
return "", "", errMissingAddr
Expand All @@ -307,15 +312,15 @@ func parseTarget(target string) (host, port string, err error) {
return target, defaultPort, nil
}
if host, port, err = net.SplitHostPort(target); err == nil {
if port == "" {
// If the port field is empty (target ends with colon), e.g. "[::1]:", this is an error.
return "", "", errEndsWithColon
}
// target has port, i.e ipv4-host:port, [ipv6-host]:port, host-name:port
if host == "" {
// Keep consistent with net.Dial(): If the host is empty, as in ":80", the local system is assumed.
host = "localhost"
}
if port == "" {
// If the port field is empty(target ends with colon), e.g. "[::1]:", defaultPort is used.
port = defaultPort
}
return host, port, nil
}
if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil {
Expand Down
6 changes: 4 additions & 2 deletions resolver/dns/dns_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ func testIPResolver(t *testing.T) {
{"127.0.0.1:12345", []resolver.Address{{Addr: "127.0.0.1:12345"}}},
{"::1", []resolver.Address{{Addr: "[::1]" + colonDefaultPort}}},
{"[::1]:12345", []resolver.Address{{Addr: "[::1]:12345"}}},
{"[::1]:", []resolver.Address{{Addr: "[::1]:443"}}},
{"[::1]", []resolver.Address{{Addr: "[::1]:443"}}},
{"2001:db8:85a3::8a2e:370:7334", []resolver.Address{{Addr: "[2001:db8:85a3::8a2e:370:7334]" + colonDefaultPort}}},
{"[2001:db8:85a3::8a2e:370:7334]", []resolver.Address{{Addr: "[2001:db8:85a3::8a2e:370:7334]" + colonDefaultPort}}},
{"[2001:db8:85a3::8a2e:370:7334]:12345", []resolver.Address{{Addr: "[2001:db8:85a3::8a2e:370:7334]:12345"}}},
Expand Down Expand Up @@ -867,6 +867,7 @@ func TestResolveFunc(t *testing.T) {
{"www.google.com", nil},
{"foo.bar:12345", nil},
{"127.0.0.1", nil},
{"::", nil},
{"127.0.0.1:12345", nil},
{"[::1]:80", nil},
{"[2001:db8:a0b:12f0::1]:21", nil},
Expand All @@ -875,7 +876,8 @@ func TestResolveFunc(t *testing.T) {
{"[fe80::1%lo0]:80", nil},
{"golang.org:http", nil},
{"[2001:db8::1]:http", nil},
{":", nil},
{"[2001:db8::1]:", errEndsWithColon},
{":", errEndsWithColon},
{"", errMissingAddr},
{"[2001:db8:a0b:12f0::1", errForInvalidTarget},
}
Expand Down