Skip to content

fix(cli): reject malformed resolver ports and an explicit -attempts 0 - #339

Merged
TMHSDigital merged 1 commit into
TMHSDigital:mainfrom
bfs2021:fix/resolver-port-attempts
Nov 15, 2026
Merged

TMHSDigital merged 1 commit into
TMHSDigital:mainfrom
bfs2021:fix/resolver-port-attempts

Conversation

@bfs2021

@bfs2021 bfs2021 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Problem

Two input-validation gaps from #318, both rejected on one side of the boundary and accepted on the other:

  • A -r file entry with a non-numeric or out-of-range port (9.9.9.9:abc, 9.9.9.9:99999, 9.9.9.9:0, 9.9.9.9:-1) is accepted, then fails every lookup at dial time. A list with no good member dies inside the reliability guard ("likely resolver rate-limiting") instead of at input.
  • An explicit -attempts 0 (flag, SUBENUM_ATTEMPTS=0 or config) runs with one attempt and no message, while -attempts -1 is rejected by Options.Validate. Fixes Resolver list accepts invalid ports; -attempts 0 is silently treated as 1 #318.

Root cause

ParseResolverList (internal/dns/pool.go) checked host and port for emptiness but never the port range — unlike the -dns-server path, which goes through validate.DNSServer's 1-65535 check.

effectiveAttempts (internal/cli/cli.go) used the flag value as the "given" marker: attemptsSet := attempts != 0. Since 0 is the flag default, an explicit 0 was indistinguishable from "not passed" and short-circuited to the default of 1 before any validation ran.

Fix

  • validate.checkPort is exported as validate.Port; internal/dns already imported the package, so ParseResolverList now applies the same 1-65535 rule and wraps the error with the offending line: invalid resolver "9.9.9.9:99999": invalid port: 99999 (must be 1-65535).
  • effectiveAttempts decides "given" from the source (attemptsSrc != sourceDefault), for -retries too. An explicit 0 now flows through to Options.Validate, which already rejects < 1; -attempts 0 -retries 3 now reports the "cannot use both" error instead of silently using 3.

Testing

  • internal/dns/pool_test.go: TestParseResolverListRejectsBadPorts — table over abc, 99999, 0, -1, asserting an error that names the line and states the range.
  • internal/cli/cli_test.go: TestEffectiveAttempts extended — explicit -attempts 0 (flag and config source) returns 0 so validation rejects it, and -attempts 0 -retries 3 returns the cannot-use-both error.
go vet ./...        # clean
go test ./...       # clean

Negative control: with the three source files reverted, TestParseResolverListRejectsBadPorts and TestEffectiveAttempts fail (4 assertions), so the tests cover the change.

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.
@TMHSDigital
TMHSDigital merged commit 98836ec into TMHSDigital:main Nov 15, 2026
16 checks passed
TMHSDigital added a commit that referenced this pull request Nov 15, 2026
PR #339 rejected malformed -r ports and an explicit -attempts 0 but
added no changelog entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resolver list accepts invalid ports; -attempts 0 is silently treated as 1

2 participants