mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-24 02:31:18 -08:00
Two fixes for DNS names from a malicious control server, from Ben Carman's security review: dnsname.ToFQDN now rejects names containing whitespace or control characters. ToFQDN previously checked label lengths only, so a search domain like "evil.com\nnameserver 6.6.6.6" passed validation and was written verbatim into /etc/resolv.conf by the direct DNS manager, where the injected line became a real nameserver. The same shape existed on Windows, where a CRLF in an ExtraRecords name injected lines into the hosts file. This is deliberately not RFC 1123 hostname validation (see the existing comment about issue 2024): labels may still contain any byte that isn't whitespace or a control character. dnsConfigForNetmap now drops search domains and split-DNS route suffixes that ToFQDN rejects. It previously logged the error but appended the zero FQDN anyway, and FQDN.WithoutTrailingDot panics on the empty FQDN when OS DNS config is written, so one over-long domain from control crashed tailscaled on every netmap until control sent a valid one. Thanks to Ben Carman for the report! Updates tailscale/corp#48187 Reported-by: Ben Carman Change-Id: I8de40cafacfb66e2b863f097794c42f3ca5c2da9 Signed-off-by: Brad Fitzpatrick <[email protected]>
366 lines
9.9 KiB
Go
366 lines
9.9 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package dnsname
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFQDN(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want FQDN
|
|
wantErr bool
|
|
wantLabels int
|
|
}{
|
|
{"", ".", false, 0},
|
|
{".", ".", false, 0},
|
|
{"foo.com", "foo.com.", false, 2},
|
|
{"foo.com.", "foo.com.", false, 2},
|
|
{".foo.com.", "foo.com.", false, 2},
|
|
{".foo.com", "foo.com.", false, 2},
|
|
{"com", "com.", false, 1},
|
|
{"www.tailscale.com", "www.tailscale.com.", false, 3},
|
|
{"_ssh._tcp.tailscale.com", "_ssh._tcp.tailscale.com.", false, 4},
|
|
{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com", "", true, 0},
|
|
{strings.Repeat("aaaaa.", 60) + "com", "", true, 0},
|
|
{"foo..com", "", true, 0},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.in, func(t *testing.T) {
|
|
got, err := ToFQDN(test.in)
|
|
if got != test.want {
|
|
t.Errorf("ToFQDN(%q) got %q, want %q", test.in, got, test.want)
|
|
}
|
|
if (err != nil) != test.wantErr {
|
|
t.Errorf("ToFQDN(%q) err %v, wantErr=%v", test.in, err, test.wantErr)
|
|
}
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
gotDot := got.WithTrailingDot()
|
|
if gotDot != string(test.want) {
|
|
t.Errorf("ToFQDN(%q).WithTrailingDot() got %q, want %q", test.in, gotDot, test.want)
|
|
}
|
|
gotNoDot := got.WithoutTrailingDot()
|
|
wantNoDot := string(test.want)[:len(test.want)-1]
|
|
if gotNoDot != wantNoDot {
|
|
t.Errorf("ToFQDN(%q).WithoutTrailingDot() got %q, want %q", test.in, gotNoDot, wantNoDot)
|
|
}
|
|
|
|
if gotLabels := got.NumLabels(); gotLabels != test.wantLabels {
|
|
t.Errorf("ToFQDN(%q).NumLabels() got %v, want %v", test.in, gotLabels, test.wantLabels)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestToFQDNRejectsLineUnsafeCharacters is a regression test for names from
|
|
// a malicious control server. Search domains and hosts entries are written
|
|
// verbatim into line-oriented OS configuration files like resolv.conf and
|
|
// hosts, so a name containing a newline or carriage return could inject new
|
|
// directives into those files. ToFQDN must reject such names.
|
|
func TestToFQDNRejectsLineUnsafeCharacters(t *testing.T) {
|
|
for _, s := range []string{
|
|
"foo\nbar.com",
|
|
"foo\rbar.com",
|
|
"foo bar.com",
|
|
"foo\tbar.com",
|
|
"foo\x00bar.com",
|
|
"foo\x1fbar.com",
|
|
"foo\x7fbar.com",
|
|
"evil.com\nnameserver 6.6.6.6",
|
|
"evil.com\r\nnameserver 6.6.6.6",
|
|
} {
|
|
if _, err := ToFQDN(s); err == nil {
|
|
t.Errorf("ToFQDN(%q) = nil error, want error", s)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFQDNTooLong(t *testing.T) {
|
|
// RFC 1035 says a dns name has a max size of 255 octets, and is represented as labels of len+ASCII chars so
|
|
// example.com
|
|
// is represented as
|
|
// 7example3com0
|
|
// which is to say that if we have a trailing dot then the dots cancel out all the len bytes except the first and
|
|
// we can accept 254 chars.
|
|
|
|
// This name is max length
|
|
name := "aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaa.example.com."
|
|
if len(name) != 254 {
|
|
t.Fatalf("name should be 254 chars including trailing . (len is %d)", len(name))
|
|
}
|
|
got, err := ToFQDN(name)
|
|
if err != nil {
|
|
t.Fatalf("want: no error, got: %v", err)
|
|
}
|
|
if string(got) != name {
|
|
t.Fatalf("want: %s, got: %s", name, got)
|
|
}
|
|
|
|
// This name is too long
|
|
name = "x" + name
|
|
got, err = ToFQDN(name)
|
|
if got != "" {
|
|
t.Fatalf("want: \"\", got: %s", got)
|
|
}
|
|
if err == nil || !strings.HasSuffix(err.Error(), "is too long to be a DNS name") {
|
|
t.Fatalf("want: error to end with \"is too long to be a DNS name\", got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestFQDNContains(t *testing.T) {
|
|
tests := []struct {
|
|
a, b string
|
|
want bool
|
|
}{
|
|
{"", "", true},
|
|
{"", "foo.com", true},
|
|
{"foo.com", "", false},
|
|
{"tailscale.com", "www.tailscale.com", true},
|
|
{"www.tailscale.com", "tailscale.com", false},
|
|
{"scale.com", "tailscale.com", false},
|
|
{"foo.com", "foo.com", true},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.a+"_"+test.b, func(t *testing.T) {
|
|
a, err := ToFQDN(test.a)
|
|
if err != nil {
|
|
t.Fatalf("ToFQDN(%q): %v", test.a, err)
|
|
}
|
|
b, err := ToFQDN(test.b)
|
|
if err != nil {
|
|
t.Fatalf("ToFQDN(%q): %v", test.b, err)
|
|
}
|
|
|
|
if got := a.Contains(b); got != test.want {
|
|
t.Errorf("ToFQDN(%q).Contains(%q) got %v, want %v", a, b, got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestFQDNParent(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want FQDN
|
|
}{
|
|
{"", ""},
|
|
{".", ""},
|
|
{"com.", ""},
|
|
{"foo.com.", "com."},
|
|
{"www.foo.com.", "foo.com."},
|
|
{"a.b.c.d.", "b.c.d."},
|
|
{"sub.node.tailnet.ts.net.", "node.tailnet.ts.net."},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.in, func(t *testing.T) {
|
|
in, err := ToFQDN(test.in)
|
|
if err != nil {
|
|
t.Fatalf("ToFQDN(%q): %v", test.in, err)
|
|
}
|
|
got := in.Parent()
|
|
if got != test.want {
|
|
t.Errorf("ToFQDN(%q).Parent() = %q, want %q", test.in, got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSanitizeLabel(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{"empty", "", ""},
|
|
{"space", " ", ""},
|
|
{"upper", "OBERON", "oberon"},
|
|
{"mixed", "Avery's iPhone 4(SE)", "averys-iphone-4se"},
|
|
{"dotted", "mon.ipn.dev", "mon-ipn-dev"},
|
|
{"email", "[email protected]", "admin-example-com"},
|
|
{"boundary", ".bound.ary.", "bound-ary"},
|
|
{"bad_trailing", "a-", "a"},
|
|
{"bad_leading", "-a", "a"},
|
|
{"bad_both", "-a-", "a"},
|
|
{
|
|
"overlong",
|
|
strings.Repeat("test.", 20),
|
|
"test-test-test-test-test-test-test-test-test-test-test-test-tes",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := SanitizeLabel(tt.in)
|
|
if got != tt.want {
|
|
t.Errorf("want %q; got %q", tt.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTrimCommonSuffixes(t *testing.T) {
|
|
tests := []struct {
|
|
hostname string
|
|
want string
|
|
}{
|
|
{"computer.local", "computer"},
|
|
{"computer.localdomain", "computer"},
|
|
{"computer.lan", "computer"},
|
|
{"computer.mynetwork", "computer.mynetwork"},
|
|
}
|
|
for _, tt := range tests {
|
|
got := TrimCommonSuffixes(tt.hostname)
|
|
if got != tt.want {
|
|
t.Errorf("TrimCommonSuffixes(%q) = %q; want %q", tt.hostname, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHasSuffix(t *testing.T) {
|
|
tests := []struct {
|
|
name, suffix string
|
|
want bool
|
|
}{
|
|
{"foo.com", "com", true},
|
|
{"foo.com.", "com", true},
|
|
{"foo.com.", "com.", true},
|
|
{"foo.com", ".com", true},
|
|
|
|
{"", "", false},
|
|
{"foo.com.", "", false},
|
|
{"foo.com.", "o.com", false},
|
|
}
|
|
for _, tt := range tests {
|
|
got := HasSuffix(tt.name, tt.suffix)
|
|
if got != tt.want {
|
|
t.Errorf("HasSuffix(%q, %q) = %v; want %v", tt.name, tt.suffix, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTrimSuffix(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
suffix string
|
|
want string
|
|
}{
|
|
{"foo.magicdnssuffix.", "magicdnssuffix", "foo"},
|
|
{"foo.magicdnssuffix", "magicdnssuffix", "foo"},
|
|
{"foo.magicdnssuffix", ".magicdnssuffix", "foo"},
|
|
{"foo.anothersuffix", "magicdnssuffix", "foo.anothersuffix"},
|
|
{"foo.anothersuffix.", "magicdnssuffix", "foo.anothersuffix"},
|
|
{"a.b.c.d", "c.d", "a.b"},
|
|
{"name.", "foo", "name"},
|
|
}
|
|
for _, tt := range tests {
|
|
got := TrimSuffix(tt.name, tt.suffix)
|
|
if got != tt.want {
|
|
t.Errorf("TrimSuffix(%q, %q) = %q; want %q", tt.name, tt.suffix, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidLabel(t *testing.T) {
|
|
tests := []struct {
|
|
label string
|
|
wantErr string
|
|
}{
|
|
{"", "empty DNS label"},
|
|
{"example", ""},
|
|
{" example", `must start with a letter or number`},
|
|
{"example ", `must end with a letter or number`},
|
|
{strings.Repeat("a", 63), ""},
|
|
{strings.Repeat("a", 64), `is too long, max length is 63 bytes`},
|
|
{"what🤦xx", "contains invalid character"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.label, func(t *testing.T) {
|
|
err := ValidLabel(test.label)
|
|
if (err == nil) != (test.wantErr == "") || (err != nil && !strings.Contains(err.Error(), test.wantErr)) {
|
|
t.Fatalf("ValidLabel(%s)=%v; expected %v", test.label, err, test.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidLabelLike(t *testing.T) {
|
|
tests := []struct {
|
|
labelLike string
|
|
maxLength int
|
|
wantErr string
|
|
}{
|
|
{"", 63, "empty DNS label"},
|
|
{"example", 63, ""},
|
|
{" example", 63, `must start with a letter or number`},
|
|
{"example ", 63, `must end with a letter or number`},
|
|
{strings.Repeat("a", 63), 63, ""},
|
|
{strings.Repeat("a", 64), 63, `is too long, max length is 63 bytes`},
|
|
{strings.Repeat("a", 65), 64, `is too long, max length is 64 bytes`},
|
|
{"what🤦xx", 63, "contains invalid character"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.labelLike, func(t *testing.T) {
|
|
err := ValidLabelLike(test.labelLike, test.maxLength)
|
|
if (err == nil) != (test.wantErr == "") || (err != nil && !strings.Contains(err.Error(), test.wantErr)) {
|
|
t.Fatalf("ValidLabelLike(%s)=%v; expected %v", test.labelLike, err, test.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestValidHostname(t *testing.T) {
|
|
tests := []struct {
|
|
hostname string
|
|
wantErr string
|
|
}{
|
|
{"example", ""},
|
|
{"example.com", ""},
|
|
{" example", `contains invalid character`},
|
|
{"example-.com", `must end with a letter or number`},
|
|
{strings.Repeat("a", 63), ""},
|
|
{strings.Repeat("a", 64), `is too long, max length is 63 bytes`},
|
|
{strings.Repeat(strings.Repeat("a", 63)+".", 4), "is too long to be a DNS name"},
|
|
{"www.what🤦lol.example.com", "contains invalid character"},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.hostname, func(t *testing.T) {
|
|
err := ValidHostname(test.hostname)
|
|
if (err == nil) != (test.wantErr == "") || (err != nil && !strings.Contains(err.Error(), test.wantErr)) {
|
|
t.Fatalf("ValidHostname(%s)=%v; expected %v", test.hostname, err, test.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
var sinkFQDN FQDN
|
|
|
|
func BenchmarkToFQDN(b *testing.B) {
|
|
tests := []string{
|
|
"www.tailscale.com.",
|
|
"www.tailscale.com",
|
|
".www.tailscale.com",
|
|
"_ssh._tcp.www.tailscale.com.",
|
|
"_ssh._tcp.www.tailscale.com",
|
|
}
|
|
|
|
for _, test := range tests {
|
|
b.Run(test, func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for range b.N {
|
|
sinkFQDN, _ = ToFQDN(test)
|
|
}
|
|
})
|
|
}
|
|
}
|