From e399b0bec82ed8ee7e0232f9694e7a57b5388287 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 23 Nov 2021 15:02:20 -0800 Subject: [PATCH] net/dns: require space after nameserver/search parsing resolv.conf Signed-off-by: Josh Bleecher Snyder (cherry picked from commit d10cefdb9bc761ec3ca7726a87a19d8a60325572) --- net/dns/direct.go | 15 +++++++++++---- net/dns/direct_test.go | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/net/dns/direct.go b/net/dns/direct.go index d74e2bc89..388eda68f 100644 --- a/net/dns/direct.go +++ b/net/dns/direct.go @@ -56,8 +56,11 @@ func readResolv(r io.Reader) (config OSConfig, err error) { } if strings.HasPrefix(line, "nameserver") { - nameserver := strings.TrimPrefix(line, "nameserver") - nameserver = strings.TrimSpace(nameserver) + s := strings.TrimPrefix(line, "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) if err != nil { return OSConfig{}, err @@ -67,8 +70,12 @@ func readResolv(r io.Reader) (config OSConfig, err error) { } if strings.HasPrefix(line, "search") { - domain := strings.TrimPrefix(line, "search") - domain = strings.TrimSpace(domain) + s := strings.TrimPrefix(line, "search") + 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) if err != nil { return OSConfig{}, fmt.Errorf("parsing search domains %q: %w", line, err) diff --git a/net/dns/direct_test.go b/net/dns/direct_test.go index 6cd8ff8c5..93ce261aa 100644 --- a/net/dns/direct_test.go +++ b/net/dns/direct_test.go @@ -169,7 +169,22 @@ func TestReadResolve(t *testing.T) { }, }, {in: `nameserver #192.168.0.100`, wantErr: true}, + {in: `nameserver`, wantErr: true}, {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 {