Skip to content

Commit e46e3b8

Browse files
committed
fix(validate): accept bare IP for -dns-server with default port 53
Signed-off-by: Indra Prasetya <11029197+prascli@users.noreply.github.com>
1 parent dd5b156 commit e46e3b8

10 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎docs/_includes/flags.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| `-depth <int>` | 1 | Max recursion depth when -recursive is set (1 = no recursion) |
1111
| `-diff <string>` | - | Previous results file (any -format); report only names added or removed since then, and exit 4 when there are changes |
1212
| `-diff-records` {% include unreleased.html flag="diff-records" %} | - | With -diff, also report names whose records changed (a re-pointed CNAME, moved addresses), compared over the -type record types; needs a json, jsonl or csv -diff file |
13-
| `-dns-server <string>` | 8.8.8.8:53 | DNS server: ip:port (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS) |
13+
| `-dns-server <string>` | 8.8.8.8:53 | DNS server: ip[:port] (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS) |
1414
| `-dry-run` {% include unreleased.html flag="dry-run" %} | - | Print the plan without scanning: candidates after dedupe and -exclude, query and duration estimates, and the resolver preflight (its only DNS query, also sent to each -r pool resolver); then exit |
1515
| `-exclude <string>` | - | Comma-separated out-of-scope names and \*.parent patterns; never queried or expanded |
1616
| `-exclude-file <string>` | - | File of out-of-scope names and \*.parent patterns, one per line (# comments allowed) |

‎internal/cli/cli.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func parseFlagsWith(args []string, applyDefaults func(*flag.FlagSet) error, stde
222222
fs.StringVar(&f.domainList, "dL", "", "File of apex domains to scan, one per line (- for stdin); replaces the <domain> argument")
223223
fs.IntVar(&f.concurrency, "t", scan.DefaultConcurrency, "Number of concurrent workers")
224224
fs.IntVar(&f.timeoutMs, "timeout", scan.DefaultTimeoutMs, "DNS lookup timeout in milliseconds")
225-
fs.StringVar(&f.dnsServer, "dns-server", DefaultDNSServer, "DNS server: ip:port (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS)")
225+
fs.StringVar(&f.dnsServer, "dns-server", DefaultDNSServer, "DNS server: ip[:port] (UDP, TCP fallback), tls://host[:853] (DNS over TLS) or https://host/path (DNS over HTTPS)")
226226
fs.BoolVar(&f.verbose, "v", false, "Enable verbose output")
227227
fs.BoolVar(&f.showVersion, "version", false, "Show version information")
228228
fs.BoolVar(&f.showProgress, "progress", true, "Show progress during scanning")
@@ -784,13 +784,17 @@ func scanOptions(f cliFlags, in *runtimeInputs, p scanPass, logf dns.Logf) scan.
784784
if p.flat {
785785
recursive, depth = false, 1
786786
}
787+
dnsServer := f.dnsServer
788+
if normalized, err := validate.NormalizeDNSServer(dnsServer); err == nil {
789+
dnsServer = normalized
790+
}
787791
return scan.Options{
788792
Config: scan.Config{
789793
Domain: p.domain,
790794
Entries: p.entries,
791795
Concurrency: f.concurrency,
792796
Timeout: time.Duration(f.timeoutMs) * time.Millisecond,
793-
DNSServer: f.dnsServer,
797+
DNSServer: dnsServer,
794798
Simulate: f.testMode,
795799
HitRate: f.testHitRate,
796800
Seed: f.seed,

‎internal/cli/mcp.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func parseMCPFlags(args []string, stderr io.Writer) (cfg mcpConfig, code int, ok
119119
var allow, sources string
120120
var timeoutMs int
121121
fs.StringVar(&allow, "allow", "", "Comma-separated domains the scan tool may enumerate, each with its subdomains. Without it only lab_scan, explain_stats and diff are offered")
122-
fs.StringVar(&cfg.dnsServer, "dns-server", DefaultDNSServer, "Resolver for live scans: ip:port, tls://host or https://host/path")
122+
fs.StringVar(&cfg.dnsServer, "dns-server", DefaultDNSServer, "Resolver for live scans: ip[:port], tls://host or https://host/path")
123123
fs.IntVar(&cfg.maxQueries, "max-queries", defaultMCPMaxQueries, "Most candidate names one tool call may test; a call can ask for fewer")
124124
fs.IntVar(&cfg.rate, "rate", defaultMCPRate, "DNS queries per second for live scans")
125125
fs.DurationVar(&cfg.maxDuration, "max-duration", defaultMCPMaxDuration, "Longest one tool call may scan")
@@ -165,6 +165,9 @@ func parseMCPFlags(args []string, stderr io.Writer) (cfg mcpConfig, code int, ok
165165
return fail("-max-calls must be at least 1, got %d", cfg.maxCalls)
166166
}
167167
cfg.timeout = time.Duration(timeoutMs) * time.Millisecond
168+
if normalized, err := validate.NormalizeDNSServer(cfg.dnsServer); err == nil {
169+
cfg.dnsServer = normalized
170+
}
168171
if err := validate.DNSServer(cfg.dnsServer); err != nil {
169172
return fail("-dns-server: %v", err)
170173
}

‎internal/scan/options.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ func (o Options) Validate() error {
8888
return fmt.Errorf("rate per resolver ({rate_per_resolver}) must be 0 (unlimited) or a positive integer, got %d", o.RatePerResolver)
8989
}
9090
if !o.Simulate {
91-
if err := validate.DNSServer(o.DNSServer); err != nil {
91+
server := o.DNSServer
92+
if normalized, err := validate.NormalizeDNSServer(server); err == nil {
93+
server = normalized
94+
}
95+
if err := validate.DNSServer(server); err != nil {
9296
return fmt.Errorf("DNS server %s: %w", o.DNSServer, err)
9397
}
9498
}

‎internal/scan/runner.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/TMHSDigital/subenum/internal/dns"
15+
"github.com/TMHSDigital/subenum/internal/validate"
1516
)
1617

1718
const (
@@ -235,6 +236,9 @@ func Run(ctx context.Context, cfg Config, events chan<- Event) {
235236
limiter = dns.NewAdjustableRateLimiter(cfg.Rate)
236237
}
237238
if cfg.Resolver == nil {
239+
if normalized, err := validate.NormalizeDNSServer(cfg.DNSServer); err == nil {
240+
cfg.DNSServer = normalized
241+
}
238242
cfg.Resolver = dns.NewResolver(cfg.Timeout, cfg.DNSServer)
239243
}
240244
// DoT and DoH connections kept for reuse are not held past the scan

‎internal/tui/form.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,9 @@ func (m *formModel) validate() (formValues, string) {
543543
if dnsServer == "" {
544544
dnsServer = validate.DefaultDNSServer
545545
}
546+
if normalized, err := validate.NormalizeDNSServer(dnsServer); err == nil {
547+
dnsServer = normalized
548+
}
546549
resolvers := path(fieldResolvers)
547550
if resolvers == wordlist.Stdin {
548551
return formValues{}, "Resolver pool from standard input (-) is not supported in the TUI; enter a file path"

‎internal/validate/validate.go‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,44 @@ var (
3838
tldRegex = regexp.MustCompile(`^([a-zA-Z]{2,}|xn--[a-zA-Z0-9-]{1,59})$`)
3939
)
4040

41+
// NormalizeDNSServer returns server with a default port of 53 appended when it
42+
// is a bare IPv4 or IPv6 literal, so `-dns-server 1.1.1.1` works the way it
43+
// does for `-r` files and for dig/dnsx. Bracketed IPv6 literals are handled
44+
// without double-bracketing. Already-complete addresses, tls:// and https://
45+
// values, and anything else are returned unchanged.
46+
func NormalizeDNSServer(server string) (string, error) {
47+
s := strings.TrimSpace(server)
48+
if s == "" {
49+
return s, fmt.Errorf("empty DNS server")
50+
}
51+
if strings.HasPrefix(s, "tls://") || strings.HasPrefix(s, "https://") || strings.Contains(s, "://") {
52+
return s, nil
53+
}
54+
if _, _, err := net.SplitHostPort(s); err == nil {
55+
return s, nil
56+
}
57+
host := s
58+
if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
59+
host = s[1 : len(s)-1]
60+
}
61+
if net.ParseIP(host) != nil {
62+
return net.JoinHostPort(host, "53"), nil
63+
}
64+
return s, fmt.Errorf("expected ip[:port], got %q", s)
65+
}
66+
4167
// DNSServer checks that server is a valid ip:port address with an IP host and a
42-
// port in the range 1-65535.
68+
// port in the range 1-65535. A bare IPv4 or IPv6 literal is accepted and
69+
// treated as port 53.
4370
//
4471
// Encrypted transports are written as URLs (#86): tls://host[:port] for DNS
4572
// over TLS (port 853 by default) and https://host/path for DNS over HTTPS.
4673
// Their host may be a name, which is resolved with the system resolver.
4774
func DNSServer(server string) error {
75+
normalized, err := NormalizeDNSServer(server)
76+
if err == nil {
77+
server = normalized
78+
}
4879
switch {
4980
case strings.HasPrefix(server, "tls://"):
5081
hostport, host, err := DoTAddress(server)
@@ -67,7 +98,7 @@ func DNSServer(server string) error {
6798
}
6899
host, portStr, err := net.SplitHostPort(server)
69100
if err != nil {
70-
return fmt.Errorf("invalid format, expected ip:port (e.g., %s), tls://host or https://host/path: %w", DefaultDNSServer, err)
101+
return fmt.Errorf("invalid format, expected ip[:port] (e.g., %s), tls://host or https://host/path: %w", DefaultDNSServer, err)
71102
}
72103
if net.ParseIP(host) == nil {
73104
return fmt.Errorf("invalid IP address: %s", host)

‎internal/validate/validate_test.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ func TestDNSServer(t *testing.T) {
1212
"1.1.1.1:53",
1313
"192.168.1.1:53",
1414
"[2001:4860:4860::8888]:53",
15+
"1.1.1.1",
16+
"2606:4700:4700::1111",
1517
}
1618

1719
invalidServers := []struct {
1820
server string
1921
reason string
2022
}{
21-
{"8.8.8.8", "missing port"},
2223
{":53", "missing IP"},
2324
{"localhost:53", "not a valid IP"},
2425
{"256.1.1.1:53", "invalid IP octet"},

‎pkg/subenum/subenum.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ func (c Config) options() (scan.Options, error) {
634634
}
635635

636636
resolver := orDefaultString(c.Resolver, DefaultResolver)
637+
if normalized, err := validate.NormalizeDNSServer(resolver); err == nil {
638+
resolver = normalized
639+
}
637640
if !c.Simulate {
638641
if err := validate.DNSServer(resolver); err != nil {
639642
return scan.Options{}, fmt.Errorf("subenum: Resolver %s: %w", resolver, err)

‎pkg/subenum/zone.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func probeZone(ctx context.Context, domain string, opts ZoneOptions, skip func(s
6464
return ZoneInfo{}, fmt.Errorf("subenum: Timeout must not be negative, got %s", opts.Timeout)
6565
}
6666
resolver := orDefaultString(opts.Resolver, DefaultResolver)
67+
if normalized, err := validate.NormalizeDNSServer(resolver); err == nil {
68+
resolver = normalized
69+
}
6770
if err := validate.DNSServer(resolver); err != nil {
6871
return ZoneInfo{}, fmt.Errorf("subenum: Resolver %s: %w", resolver, err)
6972
}

0 commit comments

Comments
 (0)