net/dns: require space after nameserver/search parsing resolv.conf

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
(cherry picked from commit d10cefdb9b)
This commit is contained in:
Josh Bleecher Snyder 2021-11-23 15:02:20 -08:00 committed by Brad Fitzpatrick
parent 21b1a44cd6
commit e399b0bec8
2 changed files with 26 additions and 4 deletions

View File

@ -56,8 +56,11 @@ func readResolv(r io.Reader) (config OSConfig, err error) {
} }
if strings.HasPrefix(line, "nameserver") { if strings.HasPrefix(line, "nameserver") {
nameserver := strings.TrimPrefix(line, "nameserver") s := strings.TrimPrefix(line, "nameserver")
nameserver = strings.TrimSpace(nameserver) nameserver := strings.TrimSpace(s)
if len(nameserver) == len(s) {
return OSConfig{}, fmt.Errorf("missing space after \"nameserver\" in %q", line)
}
ip, err := netaddr.ParseIP(nameserver) ip, err := netaddr.ParseIP(nameserver)
if err != nil { if err != nil {
return OSConfig{}, err return OSConfig{}, err
@ -67,8 +70,12 @@ func readResolv(r io.Reader) (config OSConfig, err error) {
} }
if strings.HasPrefix(line, "search") { if strings.HasPrefix(line, "search") {
domain := strings.TrimPrefix(line, "search") s := strings.TrimPrefix(line, "search")
domain = strings.TrimSpace(domain) domain := strings.TrimSpace(s)
if len(domain) == len(s) {
// No leading space?!
return OSConfig{}, fmt.Errorf("missing space after \"domain\" in %q", line)
}
fqdn, err := dnsname.ToFQDN(domain) fqdn, err := dnsname.ToFQDN(domain)
if err != nil { if err != nil {
return OSConfig{}, fmt.Errorf("parsing search domains %q: %w", line, err) return OSConfig{}, fmt.Errorf("parsing search domains %q: %w", line, err)

View File

@ -169,7 +169,22 @@ func TestReadResolve(t *testing.T) {
}, },
}, },
{in: `nameserver #192.168.0.100`, wantErr: true}, {in: `nameserver #192.168.0.100`, wantErr: true},
{in: `nameserver`, wantErr: true},
{in: `# nameserver 192.168.0.100`, want: OSConfig{}}, {in: `# nameserver 192.168.0.100`, want: OSConfig{}},
{in: `nameserver192.168.0.100`, wantErr: true},
{in: `search tailsacle.com`,
want: OSConfig{
SearchDomains: []dnsname.FQDN{"tailsacle.com."},
},
},
{in: `search tailsacle.com # typo`,
want: OSConfig{
SearchDomains: []dnsname.FQDN{"tailsacle.com."},
},
},
{in: `searchtailsacle.com`, wantErr: true},
{in: `search`, wantErr: true},
} }
for _, test := range tests { for _, test := range tests {