Skip to content

fix: reject empty ports in tcp4 address validation - #1659

Open
sergioperezcheco wants to merge 1 commit into
go-playground:masterfrom
sergioperezcheco:fix/tcp4-empty-port-20261002
Open

sergioperezcheco wants to merge 1 commit into
go-playground:masterfrom
sergioperezcheco:fix/tcp4-empty-port-20261002

Conversation

@sergioperezcheco

Copy link
Copy Markdown
Contributor

Reject an empty TCP4 port

tcp4_addr accepts 127.0.0.1: because net.ResolveTCPAddr treats an empty port as zero. Check the host/port split before resolution and reject an empty port, as discussed in #1003. Explicit :0 remains valid, and the existing IPv4 and port-range checks are preserved.

The regression exercises both Validate.Var and Validate.Struct for empty, missing, zero, normal, maximum, overflowing and negative ports, plus an invalid IPv4 address. The original implementation fails the two empty-port cases; the change passes all nine cases. Native Go 1.26.4 validation passed go test -race -cover ./..., go vet ./..., and golangci-lint v2.13.1 with zero issues. Other OS/Go-version matrix jobs have not been run locally.

Fixes #1003.

  • Tests exist or have been written that cover this particular change.

AI-assisted contribution: investigation, implementation, tests and this description were produced with OpenAI GPT-6.1-Sol through Hermes; the commit includes the required model attribution.

Preserve explicit port zero and the existing address resolution checks.

Co-Authored-By: OpenAI GPT-6.1-Sol <noreply@openai.com>
Signed-off-by: sergioperezcheco <checo520@outlook.com>
@sergioperezcheco
sergioperezcheco requested a review from a team as a code owner October 2, 2026 08:30
@nodivbyzero

Copy link
Copy Markdown
Contributor

According to the documentation, tcp_addr validates a TCP address.

validator/doc.go

Lines 1302 to 1306 in f3194ca

# Transmission Control Protocol Address TCP
This validates that a string value contains a valid resolvable TCP Address.
Usage: tcp_addr

What is the TCP (or TCP4) address?

IPv4 address: a 32-bit identifier for a network interface, written as four octets (e.g., 192.168.1.10). 

This is what the "address" refers to.

Address does not include port. Am I wrong?

@sergioperezcheco

Copy link
Copy Markdown
Contributor Author

Good question — and the docs are indeed easy to read that way. Empirically, though, tcp4_addr has required the host:port form all along: the tag delegates to net.ResolveTCPAddr("tcp4", ...), so a bare 127.0.0.1 already fails today with address 127.0.0.1: missing port in address (checked against current main, v10.30.5). I.e. a port-less address was never accepted, so the fix doesn't redefine "address" as including a port.

The gap it closes is the empty-port form: ResolveTCPAddr silently maps 127.0.0.1: to port 0, so tcp4_addr accepted a string with no explicit port while rejecting the equivalent bare address — an inconsistency rather than intended behavior. After the change both are consistently rejected, and an explicit :0 stays valid. The regression covers empty, missing, zero, normal, max, overflowing and negative ports on both paths.

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.

'127.0.0.1:' on tag 'tcp4_addr'

2 participants