From 7413c833348f2e9be3dc2fc68b37171d9aa17b8e Mon Sep 17 00:00:00 2001 From: Shane Kerr Date: Thu, 26 May 2022 15:06:08 +0200 Subject: [PATCH] Disallow names that start with '.' in IsDomainName() (#1376) * Disallow names that start with '.' in IsDomainName() * Also update packDomain() --- defaults.go | 5 +++++ labels_test.go | 5 ++++- msg.go | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/defaults.go b/defaults.go index d47b0b1f..f2cdbf43 100644 --- a/defaults.go +++ b/defaults.go @@ -218,6 +218,11 @@ func IsDomainName(s string) (labels int, ok bool) { wasDot = false case '.': + if i == 0 && len(s) > 1 { + // leading dots are not legal except for the root zone + return labels, false + } + if wasDot { // two dots back to back is not legal return labels, false diff --git a/labels_test.go b/labels_test.go index 3e672fec..dbe9a229 100644 --- a/labels_test.go +++ b/labels_test.go @@ -176,7 +176,10 @@ func TestIsDomainName(t *testing.T) { lab int } names := map[string]*ret{ - "..": {false, 1}, + ".": {true, 1}, + "..": {false, 0}, + "double-dot..test": {false, 1}, + ".leading-dot.test": {false, 0}, "@.": {true, 1}, "www.example.com": {true, 3}, "www.e%ample.com": {true, 3}, diff --git a/msg.go b/msg.go index 60a2e8c3..89ebb64a 100644 --- a/msg.go +++ b/msg.go @@ -265,6 +265,11 @@ loop: wasDot = false case '.': + if i == 0 && len(s) > 1 { + // leading dots are not legal except for the root zone + return len(msg), ErrRdata + } + if wasDot { // two dots back to back is not legal return len(msg), ErrRdata