Skip to content

Commit 5fbcd4b

Browse files
TMHSDigitalclaude
andcommitted
Normalize -dns-server once at flag parse time (#459)
A bare-IP -dns-server worked for the main scan, but -axfr, -nsec-walk and the -dry-run preflight passed the raw value to the dialer, which failed with "missing port in address". Normalize the flag right after parsing (covering flag, env and config sources) and drop the per-call copies in scanOptions and the -ptr pass. Fixes #459 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a45b999 commit 5fbcd4b

3 files changed

Lines changed: 38 additions & 11 deletions

File tree

‎internal/cli/cli.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ func parseFlagsWith(args []string, applyDefaults func(*flag.FlagSet) error, stde
306306
if err != nil {
307307
return f, positionals, fs, err
308308
}
309+
// Normalize -dns-server once, whichever of flag, env or config set it,
310+
// so every path that dials it (the scan, -axfr, -nsec-walk, -ptr, the
311+
// -dry-run preflight) gets host:53 for a bare IP (#459).
312+
if normalized, err := validate.NormalizeDNSServer(f.dnsServer); err == nil {
313+
f.dnsServer = normalized
314+
}
309315
return f, positionals, fs, cfgErr
310316
}
311317

@@ -924,10 +930,6 @@ func scanOptions(f cliFlags, in *runtimeInputs, p scanPass, logf dns.Logf) scan.
924930
if p.flat {
925931
recursive, depth = false, 1
926932
}
927-
dnsServer := f.dnsServer
928-
if normalized, err := validate.NormalizeDNSServer(dnsServer); err == nil {
929-
dnsServer = normalized
930-
}
931933
return scan.Options{
932934
Config: scan.Config{
933935
Domain: p.domain,
@@ -936,7 +938,7 @@ func scanOptions(f cliFlags, in *runtimeInputs, p scanPass, logf dns.Logf) scan.
936938
RecursiveEntries: p.deepEntries,
937939
Concurrency: f.concurrency,
938940
Timeout: time.Duration(f.timeoutMs) * time.Millisecond,
939-
DNSServer: dnsServer,
941+
DNSServer: f.dnsServer,
940942
Simulate: f.testMode,
941943
HitRate: f.testHitRate,
942944
Seed: f.seed,

‎internal/cli/cli_test.go‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"bytes"
5+
"flag"
56
"io"
67
"os"
78
"path/filepath"
@@ -16,6 +17,35 @@ import (
1617
"github.com/TMHSDigital/subenum/internal/scan"
1718
)
1819

20+
// TestParseFlagsNormalizesDNSServer pins a bare-IP -dns-server, from the
21+
// command line or from a config/env default, to host:53 once at parse time, so
22+
// -axfr, -nsec-walk and the -dry-run preflight dial a full address (#459).
23+
func TestParseFlagsNormalizesDNSServer(t *testing.T) {
24+
cases := []struct {
25+
name string
26+
args []string
27+
defaults func(*flag.FlagSet) error
28+
want string
29+
}{
30+
{"flag bare IPv4", []string{"-dns-server", "127.0.0.1", "example.com"}, nil, "127.0.0.1:53"},
31+
{"flag bare IPv6", []string{"-dns-server", "::1", "example.com"}, nil, "[::1]:53"},
32+
{"flag with port", []string{"-dns-server", "127.0.0.1:5353", "example.com"}, nil, "127.0.0.1:5353"},
33+
{"flag DoT", []string{"-dns-server", "tls://dns.example", "example.com"}, nil, "tls://dns.example"},
34+
{"config default bare IP", []string{"example.com"}, func(fs *flag.FlagSet) error { return fs.Set("dns-server", "127.0.0.2") }, "127.0.0.2:53"},
35+
}
36+
for _, tc := range cases {
37+
t.Run(tc.name, func(t *testing.T) {
38+
f, _, _, err := parseFlagsWith(tc.args, tc.defaults, io.Discard)
39+
if err != nil {
40+
t.Fatalf("parseFlags: %v", err)
41+
}
42+
if f.dnsServer != tc.want {
43+
t.Errorf("dnsServer = %q, want %q", f.dnsServer, tc.want)
44+
}
45+
})
46+
}
47+
}
48+
1949
func TestParseFlagsInterspersed(t *testing.T) {
2050
cases := []struct {
2151
name string

‎internal/cli/ptr.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/TMHSDigital/subenum/internal/dns"
1414
"github.com/TMHSDigital/subenum/internal/output"
1515
"github.com/TMHSDigital/subenum/internal/scan"
16-
"github.com/TMHSDigital/subenum/internal/validate"
1716
"github.com/TMHSDigital/subenum/internal/wordlist"
1817
)
1918

@@ -106,11 +105,7 @@ func lookupPTRs(ctx context.Context, s *session, addrs []string) ptrLookups {
106105
todo = todo[:m]
107106
}
108107
timeout := time.Duration(s.f.timeoutMs) * time.Millisecond
109-
server := s.f.dnsServer
110-
if normalized, err := validate.NormalizeDNSServer(server); err == nil {
111-
server = normalized
112-
}
113-
l := dns.Lookup{Resolver: dns.NewResolver(timeout, server), Timeout: timeout}
108+
l := dns.Lookup{Resolver: dns.NewResolver(timeout, s.f.dnsServer), Timeout: timeout}
114109
defer dns.CloseIdleConnections(l.Resolver)
115110
var sent atomic.Int64
116111
ctx = dns.WithQueryCounter(dns.WithLimiter(ctx, dns.NewRateLimiter(s.f.rate)), &sent)

0 commit comments

Comments
 (0)