Skip to content

Commit 7923172

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 0f3b0b6 commit 7923172

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); 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
@@ -223,7 +223,7 @@ func parseFlagsWith(args []string, applyDefaults func(*flag.FlagSet) error, stde
223223
fs.StringVar(&f.domainList, "dL", "", "File of apex domains to scan, one per line (- for stdin); replaces the <domain> argument")
224224
fs.IntVar(&f.concurrency, "t", scan.DefaultConcurrency, "Number of concurrent workers")
225225
fs.IntVar(&f.timeoutMs, "timeout", scan.DefaultTimeoutMs, "DNS lookup timeout in milliseconds")
226-
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)")
226+
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)")
227227
fs.BoolVar(&f.verbose, "v", false, "Enable verbose output")
228228
fs.BoolVar(&f.showVersion, "version", false, "Show version information")
229229
fs.BoolVar(&f.showProgress, "progress", true, "Show progress during scanning")
@@ -735,13 +735,17 @@ func scanOptions(f cliFlags, in *runtimeInputs, p scanPass, logf dns.Logf) scan.
735735
if p.flat {
736736
recursive, depth = false, 1
737737
}
738+
dnsServer := f.dnsServer
739+
if normalized, err := validate.NormalizeDNSServer(dnsServer); err == nil {
740+
dnsServer = normalized
741+
}
738742
return scan.Options{
739743
Config: scan.Config{
740744
Domain: p.domain,
741745
Entries: p.entries,
742746
Concurrency: f.concurrency,
743747
Timeout: time.Duration(f.timeoutMs) * time.Millisecond,
744-
DNSServer: f.dnsServer,
748+
DNSServer: dnsServer,
745749
Simulate: f.testMode,
746750
HitRate: f.testHitRate,
747751
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
@@ -106,7 +106,11 @@ func (o Options) Validate() error {
106106
return fmt.Errorf("rate per resolver ({rate_per_resolver}) must be 0 (unlimited) or a positive integer, got %d", o.RatePerResolver)
107107
}
108108
if !o.Simulate {
109-
if err := validate.DNSServer(o.DNSServer); err != nil {
109+
server := o.DNSServer
110+
if normalized, err := validate.NormalizeDNSServer(server); err == nil {
111+
server = normalized
112+
}
113+
if err := validate.DNSServer(server); err != nil {
110114
return fmt.Errorf("DNS server %s: %w", o.DNSServer, err)
111115
}
112116
}

‎internal/scan/runner.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/TMHSDigital/subenum/internal/dns"
17+
"github.com/TMHSDigital/subenum/internal/validate"
1718
)
1819

1920
const (
@@ -686,6 +687,9 @@ func Run(ctx context.Context, cfg Config, events chan<- Event) {
686687
limiter = dns.NewAdjustableRateLimiter(cfg.Rate)
687688
}
688689
if cfg.Resolver == nil {
690+
if normalized, err := validate.NormalizeDNSServer(cfg.DNSServer); err == nil {
691+
cfg.DNSServer = normalized
692+
}
689693
cfg.Resolver = dns.NewResolver(cfg.Timeout, cfg.DNSServer)
690694
}
691695
// 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
@@ -606,6 +606,9 @@ func (c Config) options() (scan.Options, error) {
606606
}
607607

608608
resolver := orDefaultString(c.Resolver, DefaultResolver)
609+
if normalized, err := validate.NormalizeDNSServer(resolver); err == nil {
610+
resolver = normalized
611+
}
609612
if !c.Simulate {
610613
if err := validate.DNSServer(resolver); err != nil {
611614
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
@@ -58,6 +58,9 @@ func ProbeZone(ctx context.Context, domain string, opts ZoneOptions) (ZoneInfo,
5858
return ZoneInfo{}, fmt.Errorf("subenum: Timeout must not be negative, got %s", opts.Timeout)
5959
}
6060
resolver := orDefaultString(opts.Resolver, DefaultResolver)
61+
if normalized, err := validate.NormalizeDNSServer(resolver); err == nil {
62+
resolver = normalized
63+
}
6164
if err := validate.DNSServer(resolver); err != nil {
6265
return ZoneInfo{}, fmt.Errorf("subenum: Resolver %s: %w", resolver, err)
6366
}

0 commit comments

Comments
 (0)