diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 51f702f..102b79a 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -775,8 +775,10 @@ func scanTarget(ctx context.Context, s *session, p scanPass, emit func(scan.Even // (#116). fromRetries reports that the value came from -retries, so the // caller can print the deprecation warning. func effectiveAttempts(attempts, retries int, attemptsSrc, retriesSrc string) (n int, fromRetries bool, err error) { - attemptsSet := attempts != 0 - retriesSet := retries != 0 + // "Given" comes from the source, not the value: 0 is the flag default, and + // an explicit 0 must reach Options.Validate instead of passing as unset. + attemptsSet := attemptsSrc != sourceDefault + retriesSet := retriesSrc != sourceDefault if attemptsSet && retriesSet { switch ra, rr := sourceRank[attemptsSrc], sourceRank[retriesSrc]; { case ra > rr: diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 9d26a94..d290472 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -146,6 +146,17 @@ func TestEffectiveAttempts(t *testing.T) { if err != nil || got != 3 { t.Errorf("-retries=3: got %d, err %v; want 3, nil", got, err) } + got, err = resolve(0, 0, fl, d) + if err != nil || got != 0 { + t.Errorf("explicit -attempts=0: got %d, err %v; want 0 so Options.Validate rejects it", got, err) + } + got, err = resolve(0, 0, sourceConfig, d) + if err != nil || got != 0 { + t.Errorf("config attempts=0: got %d, err %v; want 0 so Options.Validate rejects it", got, err) + } + if _, err = resolve(0, 3, fl, fl); err == nil { + t.Error("explicit -attempts=0 with -retries=3: want the cannot-use-both error") + } _, err = resolve(5, 3, fl, fl) if err == nil { t.Error("both set: expected error, got nil") diff --git a/internal/dns/pool.go b/internal/dns/pool.go index e986c1a..d527567 100644 --- a/internal/dns/pool.go +++ b/internal/dns/pool.go @@ -8,6 +8,8 @@ import ( "strings" "sync" "time" + + "github.com/TMHSDigital/subenum/internal/validate" ) // Pool health thresholds (#69). @@ -113,6 +115,9 @@ func ParseResolverList(lines []string) ([]string, error) { if err != nil || net.ParseIP(host) == nil || port == "" { return nil, fmt.Errorf("invalid resolver %q: want ip or ip:port", line) } + if err := validate.Port(port); err != nil { + return nil, fmt.Errorf("invalid resolver %q: %w", line, err) + } if !seen[s] { seen[s] = true out = append(out, s) diff --git a/internal/dns/pool_test.go b/internal/dns/pool_test.go index f9e7451..3f3977d 100644 --- a/internal/dns/pool_test.go +++ b/internal/dns/pool_test.go @@ -3,6 +3,7 @@ package dns import ( "context" "reflect" + "strings" "testing" "time" ) @@ -21,6 +22,21 @@ func TestParseResolverList(t *testing.T) { } } +func TestParseResolverListRejectsBadPorts(t *testing.T) { + for _, line := range []string{"9.9.9.9:abc", "9.9.9.9:99999", "9.9.9.9:0", "9.9.9.9:-1"} { + _, err := ParseResolverList([]string{"1.1.1.1", line}) + if err == nil { + t.Fatalf("%q accepted; want a port error", line) + } + if !strings.Contains(err.Error(), line) { + t.Fatalf("%q: error does not name the offending line: %v", line, err) + } + if !strings.Contains(err.Error(), "1-65535") { + t.Fatalf("%q: error does not state the port range: %v", line, err) + } + } +} + func TestPoolRotatesAndBenches(t *testing.T) { p := NewPool([]string{"192.0.2.1:53", "192.0.2.2:53", "192.0.2.3:53"}, time.Second) now := time.Unix(0, 0) diff --git a/internal/validate/validate.go b/internal/validate/validate.go index f1ea12e..77a5648 100644 --- a/internal/validate/validate.go +++ b/internal/validate/validate.go @@ -55,7 +55,7 @@ func DNSServer(server string) error { return fmt.Errorf("invalid DNS-over-TLS host: %s", host) } _, portStr, _ := net.SplitHostPort(hostport) - return checkPort(portStr) + return Port(portStr) case strings.HasPrefix(server, "https://"): u, err := url.Parse(server) if err != nil || u.Host == "" { @@ -72,10 +72,11 @@ func DNSServer(server string) error { if net.ParseIP(host) == nil { return fmt.Errorf("invalid IP address: %s", host) } - return checkPort(portStr) + return Port(portStr) } -func checkPort(portStr string) error { +// Port reports whether portStr is a decimal port number in 1-65535. +func Port(portStr string) error { port, err := strconv.Atoi(portStr) if err != nil || port < 1 || port > 65535 { return fmt.Errorf("invalid port: %s (must be 1-65535)", portStr)