]> Cypherpunks repositories - gostls13.git/commitdiff
net: return errNoSuchHost when no entry found in /etc/hosts and order is hostLookupFiles
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Thu, 17 Nov 2022 08:49:32 +0000 (08:49 +0000)
committerGopher Robot <gobot@golang.org>
Thu, 17 Nov 2022 21:42:39 +0000 (21:42 +0000)
When /etc/nsswitch.conf lists: "hosts: files" then LookupHost returns two nils when no entry inside /etc/hosts is found.

Change-Id: I96d68a079dfe009655c84cf0e697ce19a5bb6698
GitHub-Last-Rev: 894f066bbcc7c975f1975bd0d1dcb5726f590bc5
GitHub-Pull-Request: golang/go#56747
Reviewed-on: https://go-review.googlesource.com/c/go/+/450875
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/net/conf_test.go
src/net/dnsclient_unix.go
src/net/dnsclient_unix_test.go

index 9228b34a39d33976d5347eb395e1fd657609c1be..3736709295fd0578384eb993c4f5457b9552fc38 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
+//go:build unix
 
 package net
 
index 88f8d34e1a5c10c472eae1b6d4cbb69d8bdd1b46..20da8f197031f0df42c937a451a468f65380140d 100644 (file)
@@ -551,9 +551,13 @@ func (r *Resolver) goLookupHostOrder(ctx context.Context, name string, order hos
        if order == hostLookupFilesDNS || order == hostLookupFiles {
                // Use entries from /etc/hosts if they match.
                addrs, _ = lookupStaticHost(name)
-               if len(addrs) > 0 || order == hostLookupFiles {
+               if len(addrs) > 0 {
                        return
                }
+
+               if order == hostLookupFiles {
+                       return nil, &DNSError{Err: errNoSuchHost.Error(), Name: name, IsNotFound: true}
+               }
        }
        ips, _, err := r.goLookupIPCNAMEOrder(ctx, "ip", name, order, conf)
        if err != nil {
index 2a15845ea10f251fa426ebd22b3c822954d4459e..3ba19eb813a1518030bfb72fc482a3ebc3f63a54 100644 (file)
@@ -12,7 +12,9 @@ import (
        "fmt"
        "os"
        "path"
+       "path/filepath"
        "reflect"
+       "runtime"
        "strings"
        "sync"
        "sync/atomic"
@@ -2503,3 +2505,92 @@ func TestDNSConfigNoReload(t *testing.T) {
                t.Fatal(err)
        }
 }
+
+func TestLookupOrderFilesNoSuchHost(t *testing.T) {
+       defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
+       if runtime.GOOS != "openbsd" {
+               defer setSystemNSS(getSystemNSS(), 0)
+               setSystemNSS(nssStr(t, "hosts: files"), time.Hour)
+       }
+
+       conf, err := newResolvConfTest()
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer conf.teardown()
+
+       resolvConf := dnsConfig{servers: defaultNS}
+       if runtime.GOOS == "openbsd" {
+               // Set error to ErrNotExist, so that the hostLookupOrder
+               // returns hostLookupFiles for openbsd.
+               resolvConf.err = os.ErrNotExist
+       }
+
+       if !conf.forceUpdateConf(&resolvConf, time.Now().Add(time.Hour)) {
+               t.Fatal("failed to update resolv config")
+       }
+
+       tmpFile := filepath.Join(t.TempDir(), "hosts")
+       if err := os.WriteFile(tmpFile, []byte{}, 0660); err != nil {
+               t.Fatal(err)
+       }
+       testHookHostsPath = tmpFile
+
+       const testName = "test.invalid"
+
+       order, _ := systemConf().hostLookupOrder(DefaultResolver, testName)
+       if order != hostLookupFiles {
+               // skip test for systems which do not return hostLookupFiles
+               t.Skipf("hostLookupOrder did not return hostLookupFiles")
+       }
+
+       var lookupTests = []struct {
+               name   string
+               lookup func(name string) error
+       }{
+               {
+                       name: "Host",
+                       lookup: func(name string) error {
+                               _, err = DefaultResolver.LookupHost(context.Background(), name)
+                               return err
+                       },
+               },
+               {
+                       name: "IP",
+                       lookup: func(name string) error {
+                               _, err = DefaultResolver.LookupIP(context.Background(), "ip", name)
+                               return err
+                       },
+               },
+               {
+                       name: "IPAddr",
+                       lookup: func(name string) error {
+                               _, err = DefaultResolver.LookupIPAddr(context.Background(), name)
+                               return err
+                       },
+               },
+               {
+                       name: "NetIP",
+                       lookup: func(name string) error {
+                               _, err = DefaultResolver.LookupNetIP(context.Background(), "ip", name)
+                               return err
+                       },
+               },
+       }
+
+       for _, v := range lookupTests {
+               err := v.lookup(testName)
+
+               if err == nil {
+                       t.Errorf("Lookup%v: unexpected success", v.name)
+                       continue
+               }
+
+               expectedErr := DNSError{Err: errNoSuchHost.Error(), Name: testName, IsNotFound: true}
+               var dnsErr *DNSError
+               errors.As(err, &dnsErr)
+               if dnsErr == nil || *dnsErr != expectedErr {
+                       t.Errorf("Lookup%v: unexpected error: %v", v.name, err)
+               }
+       }
+}