]> Cypherpunks repositories - gostls13.git/commitdiff
net: ensure that malformed domain names report a consistent error
authorRuss Cox <rsc@golang.org>
Thu, 7 Jan 2016 01:50:11 +0000 (20:50 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 8 Jan 2016 16:19:20 +0000 (16:19 +0000)
Previously it depended on whether we were using the Go resolver or the Cgo resolver.

Fixes #12421.

Change-Id: Ib162e336f30f736d7244e29d96651c3be11fc3cd
Reviewed-on: https://go-review.googlesource.com/18383
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/ip_test.go
src/net/lookup.go

index 3d95a73c097539a8b8400e0e9db1fdee7181ae82..69ef5a1ae5ccac2f8c43eccbca35af6403bfc5a1 100644 (file)
@@ -540,3 +540,48 @@ func TestIPAddrScope(t *testing.T) {
                }
        }
 }
+
+func TestLookupBadName(t *testing.T) {
+       // Check that we get the same error for invalid names regardless of lookup algorithm.
+
+       mode := ""
+       check := func(fn string, err error) {
+               if e, ok := err.(*DNSError); !ok || e.Err != "invalid domain name" {
+                       t.Errorf("%s: %s(\"!!!.local\") = %T(%v), want DNSError(invalid domain name)", mode, fn, err, err)
+               }
+       }
+
+       for i, fn := range []func() func(){forceGoDNS, forceCgoDNS} {
+               fixup := fn()
+               if fixup == nil {
+                       continue
+               }
+               mode = "netgo"
+               if i == 1 {
+                       mode = "netcgo"
+               }
+
+               _, err := LookupHost("!!!.local")
+               check("LookupHost", err)
+
+               _, err = LookupIP("!!!.local")
+               check("LookupIP", err)
+
+               _, err = LookupCNAME("!!!.local")
+               check("LookupCNAME", err)
+
+               _, _, err = LookupSRV("x", "tcp", "!!!.local")
+               check("LookupSRV", err)
+
+               _, err = LookupMX("!!!.local")
+               check("LookupMX", err)
+
+               _, err = LookupNS("!!!.local")
+               check("LookupNS", err)
+
+               _, err = LookupTXT("!!!.local")
+               check("LookupTXT", err)
+
+               fixup()
+       }
+}
index 7aa111ba929b864802b268c60140ef5185487c11..b5d77e02eaff4bf7eae8c037bd83cf8adc84a9b2 100644 (file)
@@ -33,6 +33,9 @@ func LookupHost(host string) (addrs []string, err error) {
        if ip := ParseIP(host); ip != nil {
                return []string{host}, nil
        }
+       if !isDomainName(host) {
+               return nil, &DNSError{Err: "invalid domain name", Name: host}
+       }
        return lookupHost(host)
 }
 
@@ -47,6 +50,9 @@ func LookupIP(host string) (ips []IP, err error) {
        if ip := ParseIP(host); ip != nil {
                return []IP{ip}, nil
        }
+       if !isDomainName(host) {
+               return nil, &DNSError{Err: "invalid domain name", Name: host}
+       }
        addrs, err := lookupIPMerge(host)
        if err != nil {
                return
@@ -146,6 +152,9 @@ func LookupPort(network, service string) (port int, err error) {
 // LookupHost or LookupIP directly; both take care of resolving
 // the canonical name as part of the lookup.
 func LookupCNAME(name string) (cname string, err error) {
+       if !isDomainName(name) {
+               return "", &DNSError{Err: "invalid domain name", Name: name}
+       }
        return lookupCNAME(name)
 }
 
@@ -159,21 +168,33 @@ func LookupCNAME(name string) (cname string, err error) {
 // publishing SRV records under non-standard names, if both service
 // and proto are empty strings, LookupSRV looks up name directly.
 func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error) {
+       if !isDomainName(name) {
+               return "", nil, &DNSError{Err: "invalid domain name", Name: name}
+       }
        return lookupSRV(service, proto, name)
 }
 
 // LookupMX returns the DNS MX records for the given domain name sorted by preference.
 func LookupMX(name string) (mxs []*MX, err error) {
+       if !isDomainName(name) {
+               return nil, &DNSError{Err: "invalid domain name", Name: name}
+       }
        return lookupMX(name)
 }
 
 // LookupNS returns the DNS NS records for the given domain name.
 func LookupNS(name string) (nss []*NS, err error) {
+       if !isDomainName(name) {
+               return nil, &DNSError{Err: "invalid domain name", Name: name}
+       }
        return lookupNS(name)
 }
 
 // LookupTXT returns the DNS TXT records for the given domain name.
 func LookupTXT(name string) (txts []string, err error) {
+       if !isDomainName(name) {
+               return nil, &DNSError{Err: "invalid domain name", Name: name}
+       }
        return lookupTXT(name)
 }