1
0
mirror of https://github.com/golang/go synced 2024-09-29 14:24:32 -06:00

net: return errNoSuchHost when no entry found in /etc/hosts and order is hostLookupFiles

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: 894f066bbc
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>
This commit is contained in:
Mateusz Poliwczak 2022-11-17 08:49:32 +00:00 committed by Gopher Robot
parent 1f4394a0c9
commit 3e5c2c1556
3 changed files with 97 additions and 2 deletions

View File

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris //go:build unix
package net package net

View File

@ -551,9 +551,13 @@ func (r *Resolver) goLookupHostOrder(ctx context.Context, name string, order hos
if order == hostLookupFilesDNS || order == hostLookupFiles { if order == hostLookupFilesDNS || order == hostLookupFiles {
// Use entries from /etc/hosts if they match. // Use entries from /etc/hosts if they match.
addrs, _ = lookupStaticHost(name) addrs, _ = lookupStaticHost(name)
if len(addrs) > 0 || order == hostLookupFiles { if len(addrs) > 0 {
return return
} }
if order == hostLookupFiles {
return nil, &DNSError{Err: errNoSuchHost.Error(), Name: name, IsNotFound: true}
}
} }
ips, _, err := r.goLookupIPCNAMEOrder(ctx, "ip", name, order, conf) ips, _, err := r.goLookupIPCNAMEOrder(ctx, "ip", name, order, conf)
if err != nil { if err != nil {

View File

@ -12,7 +12,9 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"path/filepath"
"reflect" "reflect"
"runtime"
"strings" "strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -2503,3 +2505,92 @@ func TestDNSConfigNoReload(t *testing.T) {
t.Fatal(err) 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)
}
}
}