net/dns/resolvconffile: fix handling of multiple search domains

Fixes #6875

Change-Id: I57eb9312c9a1c81792ce2b5a0a0f254213b05df2
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
(cherry picked from commit ea70aa3d98)
This commit is contained in:
Brad Fitzpatrick 2023-01-02 19:55:57 -08:00 committed by Denton Gentry
parent e32d5af73f
commit 48d5b7f89c
No known key found for this signature in database
2 changed files with 42 additions and 8 deletions

View File

@ -83,17 +83,26 @@ func Parse(r io.Reader) (*Config, error) {
}
if s, ok := strs.CutPrefix(line, "search"); ok {
domain := strings.TrimSpace(s)
if len(domain) == len(s) {
domains := strings.TrimSpace(s)
if len(domains) == len(s) {
// No leading space?!
return nil, fmt.Errorf("missing space after \"domain\" in %q", line)
return nil, fmt.Errorf("missing space after \"search\" in %q", line)
}
fqdn, err := dnsname.ToFQDN(domain)
if err != nil {
return nil, fmt.Errorf("parsing search domains %q: %w", line, err)
for len(domains) > 0 {
domain := domains
i := strings.IndexAny(domain, " \t")
if i != -1 {
domain = domain[:i]
domains = strings.TrimSpace(domains[i+1:])
} else {
domains = ""
}
fqdn, err := dnsname.ToFQDN(domain)
if err != nil {
return nil, fmt.Errorf("parsing search domain %q in %q: %w", domain, line, err)
}
config.SearchDomains = append(config.SearchDomains, fqdn)
}
config.SearchDomains = append(config.SearchDomains, fqdn)
continue
}
}
return config, nil

View File

@ -57,6 +57,31 @@ func TestParse(t *testing.T) {
},
{in: `searchtailsacle.com`, wantErr: true},
{in: `search`, wantErr: true},
// Issue 6875: there can be multiple search domains, and even if they're
// over 253 bytes long total.
{
in: "search search-01.example search-02.example search-03.example search-04.example search-05.example search-06.example search-07.example search-08.example search-09.example search-10.example search-11.example search-12.example search-13.example search-14.example search-15.example\n",
want: &Config{
SearchDomains: []dnsname.FQDN{
"search-01.example.",
"search-02.example.",
"search-03.example.",
"search-04.example.",
"search-05.example.",
"search-06.example.",
"search-07.example.",
"search-08.example.",
"search-09.example.",
"search-10.example.",
"search-11.example.",
"search-12.example.",
"search-13.example.",
"search-14.example.",
"search-15.example.",
},
},
},
}
for _, tt := range tests {