Skip to content

fix(fqdn): reject domain labels with a trailing hyphen - #1592

Open
mahirhir wants to merge 2 commits into
go-playground:masterfrom
mahirhir:fix-fqdn-trailing-hyphen
Open

fix(fqdn): reject domain labels with a trailing hyphen#1592
mahirhir wants to merge 2 commits into
go-playground:masterfrom
mahirhir:fix-fqdn-trailing-hyphen

Conversation

@mahirhir

Copy link
Copy Markdown

Fixes Or Enhances

The fqdn validator accepts domain labels that end in a hyphen, even though those are not valid hostnames. All of these pass today but shouldn't:

  • foo-.example.com (first label ends in a hyphen)
  • foo.bar-.com (middle label ends in a hyphen)
  • example.com- (TLD ends in a hyphen)

fqdnRegexStringRFC1123 matches each label with [a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62}, i.e. one alphanumeric followed by up to 62 of [a-zA-Z0-9-], so nothing forces the last character of a label to be alphanumeric.

The hostname validators had the same issue and were fixed in #1565 (RFC 1123) and #1569 (RFC 952), which changed each label to [a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?. The fqdn pattern was not updated at the same time, even though its comment says it is "same as hostnameRegexStringRFC1123 but must contain a non numerical TLD". This PR applies that same label shape to the fqdn pattern, including the TLD (which still has to start with a letter).

Behaviour that stays the same: the non-numeric TLD requirement, the optional trailing dot, and hyphens inside the last label added in #1548 (test-site-http.test-site still validates). I diffed the new pattern against the old one over a large set of random label strings; the only inputs whose result changes are the ones with a label ending in a hyphen.

Added regression cases to TestFQDNValidation and ran the package tests locally.

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

fqdnRegexStringRFC1123 matched each label with
[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62}, which allows a label to end in a
hyphen (e.g. "foo-.example.com" or "example.com-"). The hostname
validators were fixed for the same issue in go-playground#1565 (RFC 1123) and go-playground#1569
(RFC 952), but the fqdn pattern, whose comment says it should match
hostnameRegexStringRFC1123, was left behind.

Use the same label shape [a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])? so
a label must start and end with an alphanumeric. The non-numeric TLD,
the optional trailing dot, and hyphens inside the last label (go-playground#1548) are
preserved.
@mahirhir
mahirhir requested a review from a team as a code owner June 28, 2026 20:26
Comment thread validator_test.go
{"24.example24.com", true},
{"test.24.example.com", true},
{"test-site-http.test-site", true},
{"foo-.example.com", false},

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perhaps some test cases confirming label length (63 characters) would be helpful as an addition to the test suite? I believe the regex seems correct, but it may be beneficial for future contributors, what do you think?

@mahirhir

Copy link
Copy Markdown
Author

Added cases for a 63-char label (passes, the RFC max a single label can be) and 64 (fails). TestFQDNValidation passes.

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.

2 participants