Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions internal/dns/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"sync"
"time"

"github.com/TMHSDigital/subenum/internal/validate"
)

// Pool health thresholds (#69).
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions internal/dns/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dns
import (
"context"
"reflect"
"strings"
"testing"
"time"
)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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)
Expand Down
Loading