Skip to content

Commit 186f535

Browse files
committed
fix(dns): Improve FCrDNS error handling and tests
The `VerifyFCrDNS` function previously ignored errors returned from reverse DNS lookups. This could lead to incorrect passes when a DNS failure (other than a simple 'not found') occurred. This change ensures that any error from a reverse lookup will cause the FCrDNS check to fail. The test suite for FCrDNS has been updated to reflect this change. The mock DNS lookups now simulate both 'not found' errors and other generic DNS errors. The test cases have been updated to ensure that the function behaves correctly in both scenarios, resolving a situation where two test cases were effectively duplicates.
1 parent 418234f commit 186f535

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

internal/dns/dns.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ func (d *Dns) VerifyFCrDNS(addr string, pattern *string) bool {
107107
}
108108
slog.Debug("DNS: performing FCrDNS lookup", "addr", addr, "pattern", patternVal)
109109

110-
var names []string
111-
if names, _ = d.ReverseDNS(addr); len(names) == 0 {
110+
names, err := d.ReverseDNS(addr)
111+
if err != nil {
112+
return false
113+
}
114+
if len(names) == 0 {
112115
return pattern == nil // If no pattern specified, check is passed
113116
}
114117

internal/dns/dns_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func mockLookupAddr(addr string) ([]string, error) {
3030
return []string{"resolver1.opendns.com."}, nil
3131
case "9.9.9.9":
3232
return nil, &net.DNSError{Err: "no such host", Name: "9.9.9.9", IsNotFound: true}
33+
case "1.2.3.4":
34+
return nil, errors.New("unknown error")
3335
default:
3436
return nil, &net.DNSError{Err: "no such host", Name: addr, IsNotFound: true}
3537
}
@@ -250,13 +252,14 @@ func TestDns_VerifyFCrDNS(t *testing.T) {
250252
// Cases without pattern
251253
{"valid no pattern", "8.8.8.8", nil, true},
252254
{"valid partial no pattern", "1.1.1.1", nil, true},
253-
{"invalid no pattern", "9.9.9.9", nil, false},
254-
{"reverse lookup fails no pattern", "1.2.3.4", nil, false},
255+
{"not found no pattern", "9.9.9.9", nil, true},
256+
{"unknown error no pattern", "1.2.3.4", nil, false},
255257

256258
// Cases with pattern
257259
{"valid match", "8.8.8.8", p(`.*\.google$`), true},
258260
{"valid no match", "8.8.8.8", p(`\.com$`), false},
259-
{"reverse lookup fails with pattern", "9.9.9.9", p(".*"), false},
261+
{"not found with pattern", "9.9.9.9", p(".*"), false},
262+
{"unknown error with pattern", "1.2.3.4", p(".*"), false},
260263
{"invalid pattern", "8.8.8.8", p(`[`), false},
261264
}
262265
for _, tt := range tests {

0 commit comments

Comments
 (0)