Skip to content

Commit 98836ec

Browse files
authored
fix(cli): reject malformed resolver ports and an explicit -attempts 0 (#339)
ParseResolverList accepted any port SplitHostPort returned, so -r entries such as 9.9.9.9:abc, 9.9.9.9:99999 or 9.9.9.9:0 parsed fine and then failed every lookup at dial time; a list of only those died in the reliability guard instead of at input. Validate the port with the same 1-65535 rule -dns-server uses (now exported as validate.Port) and name the offending line. effectiveAttempts decided "given" from the value, so an explicit -attempts 0 (flag, env or config) was read as unset and ran with one attempt silently, while -attempts -1 was rejected. Decide from the source instead, so 0 reaches Options.Validate like any other out-of-range value and -attempts 0 -retries 3 reports the cannot-use-both error.
1 parent 8999003 commit 98836ec

5 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎internal/cli/cli.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,10 @@ func scanTarget(ctx context.Context, s *session, p scanPass, emit func(scan.Even
781781
// (#116). fromRetries reports that the value came from -retries, so the
782782
// caller can print the deprecation warning.
783783
func effectiveAttempts(attempts, retries int, attemptsSrc, retriesSrc string) (n int, fromRetries bool, err error) {
784-
attemptsSet := attempts != 0
785-
retriesSet := retries != 0
784+
// "Given" comes from the source, not the value: 0 is the flag default, and
785+
// an explicit 0 must reach Options.Validate instead of passing as unset.
786+
attemptsSet := attemptsSrc != sourceDefault
787+
retriesSet := retriesSrc != sourceDefault
786788
if attemptsSet && retriesSet {
787789
switch ra, rr := sourceRank[attemptsSrc], sourceRank[retriesSrc]; {
788790
case ra > rr:

‎internal/cli/cli_test.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ func TestEffectiveAttempts(t *testing.T) {
146146
if err != nil || got != 3 {
147147
t.Errorf("-retries=3: got %d, err %v; want 3, nil", got, err)
148148
}
149+
got, err = resolve(0, 0, fl, d)
150+
if err != nil || got != 0 {
151+
t.Errorf("explicit -attempts=0: got %d, err %v; want 0 so Options.Validate rejects it", got, err)
152+
}
153+
got, err = resolve(0, 0, sourceConfig, d)
154+
if err != nil || got != 0 {
155+
t.Errorf("config attempts=0: got %d, err %v; want 0 so Options.Validate rejects it", got, err)
156+
}
157+
if _, err = resolve(0, 3, fl, fl); err == nil {
158+
t.Error("explicit -attempts=0 with -retries=3: want the cannot-use-both error")
159+
}
149160
_, err = resolve(5, 3, fl, fl)
150161
if err == nil {
151162
t.Error("both set: expected error, got nil")

‎internal/dns/pool.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99
"sync"
1010
"time"
11+
12+
"github.com/TMHSDigital/subenum/internal/validate"
1113
)
1214

1315
// Pool health thresholds (#69).
@@ -113,6 +115,9 @@ func ParseResolverList(lines []string) ([]string, error) {
113115
if err != nil || net.ParseIP(host) == nil || port == "" {
114116
return nil, fmt.Errorf("invalid resolver %q: want ip or ip:port", line)
115117
}
118+
if err := validate.Port(port); err != nil {
119+
return nil, fmt.Errorf("invalid resolver %q: %w", line, err)
120+
}
116121
if !seen[s] {
117122
seen[s] = true
118123
out = append(out, s)

‎internal/dns/pool_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dns
33
import (
44
"context"
55
"reflect"
6+
"strings"
67
"testing"
78
"time"
89
)
@@ -21,6 +22,21 @@ func TestParseResolverList(t *testing.T) {
2122
}
2223
}
2324

25+
func TestParseResolverListRejectsBadPorts(t *testing.T) {
26+
for _, line := range []string{"9.9.9.9:abc", "9.9.9.9:99999", "9.9.9.9:0", "9.9.9.9:-1"} {
27+
_, err := ParseResolverList([]string{"1.1.1.1", line})
28+
if err == nil {
29+
t.Fatalf("%q accepted; want a port error", line)
30+
}
31+
if !strings.Contains(err.Error(), line) {
32+
t.Fatalf("%q: error does not name the offending line: %v", line, err)
33+
}
34+
if !strings.Contains(err.Error(), "1-65535") {
35+
t.Fatalf("%q: error does not state the port range: %v", line, err)
36+
}
37+
}
38+
}
39+
2440
func TestPoolRotatesAndBenches(t *testing.T) {
2541
p := NewPool([]string{"192.0.2.1:53", "192.0.2.2:53", "192.0.2.3:53"}, time.Second)
2642
now := time.Unix(0, 0)

‎internal/validate/validate.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func DNSServer(server string) error {
5555
return fmt.Errorf("invalid DNS-over-TLS host: %s", host)
5656
}
5757
_, portStr, _ := net.SplitHostPort(hostport)
58-
return checkPort(portStr)
58+
return Port(portStr)
5959
case strings.HasPrefix(server, "https://"):
6060
u, err := url.Parse(server)
6161
if err != nil || u.Host == "" {
@@ -72,10 +72,11 @@ func DNSServer(server string) error {
7272
if net.ParseIP(host) == nil {
7373
return fmt.Errorf("invalid IP address: %s", host)
7474
}
75-
return checkPort(portStr)
75+
return Port(portStr)
7676
}
7777

78-
func checkPort(portStr string) error {
78+
// Port reports whether portStr is a decimal port number in 1-65535.
79+
func Port(portStr string) error {
7980
port, err := strconv.Atoi(portStr)
8081
if err != nil || port < 1 || port > 65535 {
8182
return fmt.Errorf("invalid port: %s (must be 1-65535)", portStr)

0 commit comments

Comments
 (0)