From 469ad97d75a2d67845e1f5ab5180c7959582b241 Mon Sep 17 00:00:00 2001 From: matheusgb Date: Tue, 21 Jul 2026 15:52:54 -0300 Subject: [PATCH] fix(fqdn): enforce maximum DNS name length --- baked_in.go | 3 +-- validator_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/baked_in.go b/baked_in.go index 86a35a21..cea97829 100644 --- a/baked_in.go +++ b/baked_in.go @@ -2917,7 +2917,7 @@ func isHostnameRFC1123(fl FieldLevel) bool { func isFQDN(fl FieldLevel) bool { val := fl.Field().String() - if val == "" { + if val == "" || len(strings.TrimSuffix(val, ".")) > 253 { return false } @@ -3531,4 +3531,3 @@ var ( errMethodReturnNoValues = errors.New(`method return o values (void)`) errMethodReturnInvalidType = errors.New(`method should return invalid type`) ) - diff --git a/validator_test.go b/validator_test.go index ac0cf313..2f2c1c7a 100644 --- a/validator_test.go +++ b/validator_test.go @@ -11168,6 +11168,8 @@ func TestHostnameRFC1123AliasValidation(t *testing.T) { } func TestFQDNValidation(t *testing.T) { + maxFQDN := strings.Repeat(strings.Repeat("a", 63)+".", 3) + strings.Repeat("a", 61) + tests := []struct { param string expected bool @@ -11185,6 +11187,10 @@ func TestFQDNValidation(t *testing.T) { {"24.example24.com", true}, {"test.24.example.com", true}, {"test-site-http.test-site", true}, + {maxFQDN, true}, + {maxFQDN + ".", true}, + {maxFQDN + "a", false}, + {maxFQDN + "a.", false}, {"test24.example24.com..", false}, {"example", false}, {"192.168.0.1", false},