Skip to content

Commit 37390bc

Browse files
ldezcpu
authored andcommitted
fix: don't panic with unknown DNS question type. (#4)
* fix: don't panic with unknown DNS question type. * fix: typos. * refactor: use dns.Fqdn
1 parent 17a3d10 commit 37390bc

File tree

4 files changed

+21
-31
lines changed

4 files changed

+21
-31
lines changed

challenge-servers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ type ChallSrv struct {
6060
redirects map[string]string
6161
}
6262

63-
// mockDNSData holds mock respones for DNS A, AAAA, and CAA lookups.
63+
// mockDNSData holds mock responses for DNS A, AAAA, and CAA lookups.
6464
type mockDNSData struct {
6565
// The IPv4 address used for all A record responses that don't match a host in
6666
// aRecords.
6767
defaultIPv4 string
6868
// The IPv6 address used for all AAAA record responses that don't match a host
6969
// in aaaaRecords.
7070
defaultIPv6 string
71-
// A map of host to IPv4 addressess in string form for A record responses.
71+
// A map of host to IPv4 addresses in string form for A record responses.
7272
aRecords map[string][]string
7373
// A map of host to IPv6 addresses in string form for AAAA record responses.
7474
aaaaRecords map[string][]string

dns.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func (s *ChallSrv) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
145145
s.AddRequestEvent(DNSRequestEvent{
146146
Question: q,
147147
})
148+
148149
var answerFunc dnsAnswerFunc
149150
switch q.Qtype {
150151
case dns.TypeTXT:
@@ -155,7 +156,14 @@ func (s *ChallSrv) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
155156
answerFunc = s.aaaaAnswers
156157
case dns.TypeCAA:
157158
answerFunc = s.caaAnswers
159+
default:
160+
m.SetRcode(r, dns.RcodeNotImplemented)
161+
}
162+
163+
if answerFunc == nil {
164+
break
158165
}
166+
159167
if records := answerFunc(q); len(records) > 0 {
160168
m.Answer = append(m.Answer, records...)
161169
}

httpone.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (s *ChallSrv) AddHTTPRedirect(path, targetURL string) {
9595
s.redirects[path] = targetURL
9696
}
9797

98-
// DeletedHTTPRedirect deletes a redirect for the given path.
98+
// DeleteHTTPRedirect deletes a redirect for the given path.
9999
func (s *ChallSrv) DeleteHTTPRedirect(path string) {
100100
s.challMu.Lock()
101101
defer s.challMu.Unlock()

mockdns.go

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package challtestsrv
22

33
import (
4-
"strings"
4+
"github.com/miekg/dns"
55
)
66

77
// SetDefaultDNSIPv4 sets the default IPv4 address used for A query responses
@@ -43,9 +43,7 @@ func (s *ChallSrv) GetDefaultDNSIPv6() string {
4343
func (s *ChallSrv) AddDNSARecord(host string, addresses []string) {
4444
s.challMu.Lock()
4545
defer s.challMu.Unlock()
46-
if !strings.HasSuffix(host, ".") {
47-
host = host + "."
48-
}
46+
host = dns.Fqdn(host)
4947
s.dnsMocks.aRecords[host] = append(s.dnsMocks.aRecords[host], addresses...)
5048
}
5149

@@ -54,19 +52,15 @@ func (s *ChallSrv) AddDNSARecord(host string, addresses []string) {
5452
func (s *ChallSrv) DeleteDNSARecord(host string) {
5553
s.challMu.Lock()
5654
defer s.challMu.Unlock()
57-
if !strings.HasSuffix(host, ".") {
58-
host = host + "."
59-
}
55+
host = dns.Fqdn(host)
6056
delete(s.dnsMocks.aRecords, host)
6157
}
6258

6359
// GetDNSARecord returns a slice of IPv4 addresses (in string form) that will be
6460
// returned when querying for A records for the given host.
6561
func (s *ChallSrv) GetDNSARecord(host string) []string {
6662
s.challMu.RLock()
67-
if !strings.HasSuffix(host, ".") {
68-
host = host + "."
69-
}
63+
host = dns.Fqdn(host)
7064
defer s.challMu.RUnlock()
7165
return s.dnsMocks.aRecords[host]
7266
}
@@ -76,9 +70,7 @@ func (s *ChallSrv) GetDNSARecord(host string) []string {
7670
func (s *ChallSrv) AddDNSAAAARecord(host string, addresses []string) {
7771
s.challMu.Lock()
7872
defer s.challMu.Unlock()
79-
if !strings.HasSuffix(host, ".") {
80-
host = host + "."
81-
}
73+
host = dns.Fqdn(host)
8274
s.dnsMocks.aaaaRecords[host] = append(s.dnsMocks.aaaaRecords[host], addresses...)
8375
}
8476

@@ -87,9 +79,7 @@ func (s *ChallSrv) AddDNSAAAARecord(host string, addresses []string) {
8779
func (s *ChallSrv) DeleteDNSAAAARecord(host string) {
8880
s.challMu.Lock()
8981
defer s.challMu.Unlock()
90-
if !strings.HasSuffix(host, ".") {
91-
host = host + "."
92-
}
82+
host = dns.Fqdn(host)
9383
delete(s.dnsMocks.aaaaRecords, host)
9484
}
9585

@@ -98,9 +88,7 @@ func (s *ChallSrv) DeleteDNSAAAARecord(host string) {
9888
func (s *ChallSrv) GetDNSAAAARecord(host string) []string {
9989
s.challMu.RLock()
10090
defer s.challMu.RUnlock()
101-
if !strings.HasSuffix(host, ".") {
102-
host = host + "."
103-
}
91+
host = dns.Fqdn(host)
10492
return s.dnsMocks.aaaaRecords[host]
10593
}
10694

@@ -109,9 +97,7 @@ func (s *ChallSrv) GetDNSAAAARecord(host string) []string {
10997
func (s *ChallSrv) AddDNSCAARecord(host string, policies []MockCAAPolicy) {
11098
s.challMu.Lock()
11199
defer s.challMu.Unlock()
112-
if !strings.HasSuffix(host, ".") {
113-
host = host + "."
114-
}
100+
host = dns.Fqdn(host)
115101
s.dnsMocks.caaRecords[host] = append(s.dnsMocks.caaRecords[host], policies...)
116102
}
117103

@@ -120,9 +106,7 @@ func (s *ChallSrv) AddDNSCAARecord(host string, policies []MockCAAPolicy) {
120106
func (s *ChallSrv) DeleteDNSCAARecord(host string) {
121107
s.challMu.Lock()
122108
defer s.challMu.Unlock()
123-
if !strings.HasSuffix(host, ".") {
124-
host = host + "."
125-
}
109+
host = dns.Fqdn(host)
126110
delete(s.dnsMocks.caaRecords, host)
127111
}
128112

@@ -131,8 +115,6 @@ func (s *ChallSrv) DeleteDNSCAARecord(host string) {
131115
func (s *ChallSrv) GetDNSCAARecord(host string) []MockCAAPolicy {
132116
s.challMu.RLock()
133117
defer s.challMu.RUnlock()
134-
if !strings.HasSuffix(host, ".") {
135-
host = host + "."
136-
}
118+
host = dns.Fqdn(host)
137119
return s.dnsMocks.caaRecords[host]
138120
}

0 commit comments

Comments
 (0)