aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGravatar Md Sahil <85174511+MdSahil-oss@users.noreply.github.com> 2022-07-06 23:20:45 +0530
committerGravatar GitHub <noreply@github.com> 2022-07-06 13:50:45 -0400
commita6078ddba3d193f2a8fda57d490aa6b65922159b (patch)
tree1b2cf824864073865ab8a0cd39a7f9ef63113130 /core
parenta35dcd7a7eb8952cb10f514cb8995b8e07eb5439 (diff)
downloadcoredns-a6078ddba3d193f2a8fda57d490aa6b65922159b.tar.gz
coredns-a6078ddba3d193f2a8fda57d490aa6b65922159b.tar.zst
coredns-a6078ddba3d193f2a8fda57d490aa6b65922159b.zip
Warn when domain names are not in RFC1035 preferred syntax (#5414)
Warn when domain names are not in RFC1035 preferred syntax Signed-off-by: Md Sahil <Mohdssahil1@gmail.com>
Diffstat (limited to 'core')
-rw-r--r--core/dnsserver/onstartup.go17
-rw-r--r--core/dnsserver/onstartup_test.go40
2 files changed, 57 insertions, 0 deletions
diff --git a/core/dnsserver/onstartup.go b/core/dnsserver/onstartup.go
index 113bf15e5..b016f1ee5 100644
--- a/core/dnsserver/onstartup.go
+++ b/core/dnsserver/onstartup.go
@@ -2,9 +2,22 @@ package dnsserver
import (
"fmt"
+ "regexp"
"sort"
+
+ "github.com/coredns/coredns/plugin/pkg/dnsutil"
)
+// checkZoneSyntax() checks whether the given string match 1035 Preferred Syntax or not.
+// The root zone, and all reverse zones always return true even though they technically don't meet 1035 Preferred Syntax
+func checkZoneSyntax(zone string) bool {
+ if zone == "." || dnsutil.IsReverse(zone) != 0 {
+ return true
+ }
+ regex1035PreferredSyntax, _ := regexp.MatchString(`^(([A-Za-z]([A-Za-z0-9-]*[A-Za-z0-9])?)\.)+$`, zone)
+ return regex1035PreferredSyntax
+}
+
// startUpZones creates the text that we show when starting up:
// grpc://example.com.:1055
// example.com.:1053 on 127.0.0.1
@@ -13,6 +26,7 @@ func startUpZones(protocol, addr string, zones map[string]*Config) string {
keys := make([]string, len(zones))
i := 0
+
for k := range zones {
keys[i] = k
i++
@@ -20,6 +34,9 @@ func startUpZones(protocol, addr string, zones map[string]*Config) string {
sort.Strings(keys)
for _, zone := range keys {
+ if !checkZoneSyntax(zone) {
+ s += fmt.Sprintf("Warning: Domain %q does not follow RFC1035 preferred syntax\n", zone)
+ }
// split addr into protocol, IP and Port
_, ip, port, err := SplitProtocolHostPort(addr)
diff --git a/core/dnsserver/onstartup_test.go b/core/dnsserver/onstartup_test.go
new file mode 100644
index 000000000..031613cb8
--- /dev/null
+++ b/core/dnsserver/onstartup_test.go
@@ -0,0 +1,40 @@
+package dnsserver
+
+import (
+ "testing"
+)
+
+func TestRegex1035PrefSyntax(t *testing.T) {
+
+ testCases := []struct {
+ zone string
+ expected bool
+ }{
+ {zone: ".", expected: true},
+ {zone: "example.com.", expected: true},
+ {zone: "example.", expected: true},
+ {zone: "example123.", expected: true},
+ {zone: "example123.com.", expected: true},
+ {zone: "abc-123.com.", expected: true},
+ {zone: "an-example.com.", expected: true},
+ {zone: "a.example.com.", expected: true},
+ {zone: "1.0.0.2.ip6.arpa.", expected: true},
+ {zone: "0.10.in-addr.arpa.", expected: true},
+ {zone: "example", expected: false},
+ {zone: "example:.", expected: false},
+ {zone: "-example.com.", expected: false},
+ {zone: ".example.com.", expected: false},
+ {zone: "1.example.com", expected: false},
+ {zone: "abc.123-xyz.", expected: false},
+ {zone: "example-?&^%$.com.", expected: false},
+ {zone: "abc-.example.com.", expected: false},
+ {zone: "abc-%$.example.com.", expected: false},
+ {zone: "123-abc.example.com.", expected: false},
+ }
+
+ for _, testCase := range testCases {
+ if checkZoneSyntax(testCase.zone) != testCase.expected {
+ t.Errorf("Expected %v for %q", testCase.expected, testCase.zone)
+ }
+ }
+}