From dc32b4695c2e195c3380a23d2cd26e0d24004b6b Mon Sep 17 00:00:00 2001 From: David Anderson Date: Mon, 10 May 2021 13:04:32 -0700 Subject: [PATCH] util/dnsname: normalize leading dots in ToFQDN. Fixes #1888. Signed-off-by: David Anderson --- util/dnsname/dnsname.go | 5 ++++- util/dnsname/dnsname_test.go | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/util/dnsname/dnsname.go b/util/dnsname/dnsname.go index 56ee0baea..905e90f5f 100644 --- a/util/dnsname/dnsname.go +++ b/util/dnsname/dnsname.go @@ -24,13 +24,16 @@ func ToFQDN(s string) (FQDN, error) { if isValidFQDN(s) { return FQDN(s), nil } - if len(s) == 0 { + if len(s) == 0 || s == "." { return FQDN("."), nil } if s[len(s)-1] == '.' { s = s[:len(s)-1] } + if s[0] == '.' { + s = s[1:] + } if len(s) > maxNameLength { return "", fmt.Errorf("%q is too long to be a DNS name", s) } diff --git a/util/dnsname/dnsname_test.go b/util/dnsname/dnsname_test.go index 3df40dded..df3ea30b6 100644 --- a/util/dnsname/dnsname_test.go +++ b/util/dnsname/dnsname_test.go @@ -20,11 +20,12 @@ func TestFQDN(t *testing.T) { {".", ".", 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}, {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com", "", true, 0}, {strings.Repeat("aaaaa.", 60) + "com", "", true, 0}, - {".com", "", true, 0}, {"foo..com", "", true, 0}, }