Skip to content

fix(hostname): reject names exceeding the DNS length limit - #1686

Open
DangTAnh wants to merge 1 commit into
go-playground:masterfrom
DangTAnh:pr/hostname-total-length
Open

DangTAnh wants to merge 1 commit into
go-playground:masterfrom
DangTAnh:pr/hostname-total-length

Conversation

@DangTAnh

@DangTAnh DangTAnh commented Nov 2, 2026

Copy link
Copy Markdown

Fixes Or Enhances

hostname and hostname_rfc1123 validate each label's length but accept names whose total length exceeds the DNS limit. For example, four labels of 63, 63, 63 and 62 characters pass both tags.

Reject hostnames longer than 253 ASCII characters before applying the existing regexes. With the currently accepted syntax (no trailing dot), that matches the 255-octet DNS wire-name limit, including label-length octets and the terminating root, and is consistent with the existing fqdn validator.

Add 253/254/255-character cases to both existing test tables, keeping each label within 63 characters. Existing label syntax and other validators are unchanged.

Testing on Go 1.26.6:

  • The new boundary cases fail against the original source.
  • go test -run '^(TestHostname.*Validation|TestFQDNValidation)$' -count=1 . passes with the fix.
  • go test ./... passes.

The local race run could not build because the installed MinGW compiler is not usable for the amd64 target; CI race results remain to be verified.

AI-assisted contribution using OpenAI Codex (GPT-6).

Make sure that you've checked the boxes below before you submit PR:

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

@go-playground/validator-maintainers

Co-Authored-By: OpenAI Codex (GPT-6) <noreply@openai.com>
@DangTAnh
DangTAnh requested a review from a team as a code owner November 2, 2026 17:25
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 72.589% (+0.008%) from 72.581% — DangTAnh:pr/hostname-total-length into go-playground:master

Comment thread baked_in.go
func isHostnameRFC952(fl FieldLevel) bool {
return hostnameRegexRFC952().MatchString(fl.Field().String())
hostname := fl.Field().String()
if len(hostname) > 253 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

253 is a magic number. Consider defining a named constant such as const maxDNSHostnameLength = 253 and using it instead.

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.

3 participants