Skip to content

Commit 4dc12c6

Browse files
authored
fix: allow for undescores and leading hyphens in domain validator (#1088)
1 parent cf5d5ca commit 4dc12c6

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎pkg/validators/domain_validator.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@ func (v *DomainValidator) getHostname(hostname string) (string, error) {
119119
if net.ParseIP(hostname) != nil {
120120
return "", fmt.Errorf("ip addresses are not supported")
121121
}
122-
hostname, err := idna.Lookup.ToASCII(hostname)
122+
i := idna.New(
123+
idna.MapForLookup(),
124+
idna.Transitional(false),
125+
idna.BidiRule(),
126+
idna.StrictDomainName(false),
127+
idna.CheckHyphens(false),
128+
idna.CheckJoiners(false),
129+
)
130+
hostname, err := i.ToASCII(hostname)
123131
if err != nil {
124132
return "", fmt.Errorf("failed to convert hostname to ascii: %w", err)
125133
}

‎pkg/validators/domain_validator_test.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
5050
input: "https://example.com",
5151
expected: "example.com",
5252
},
53+
{
54+
description: "Domain with underscores should pass",
55+
input: "https://my_domain.com",
56+
expected: "my_domain.com",
57+
},
58+
{
59+
description: "Domain with leading hyphen should pass",
60+
input: "https://-my-domain.com",
61+
expected: "-my-domain.com",
62+
},
5363
{
5464
description: "Domain without scheme should parse if scheme is disabled",
5565
input: "example.com",
@@ -108,7 +118,7 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
108118
},
109119
{
110120
description: "Invalid IDNA domain should fail",
111-
input: "ab--cd.example.com",
121+
input: "xn--r-kva.example.com",
112122
errorFunc: func(t *testing.T, e error) {
113123
assert.ErrorContains(t, e, "invalid label")
114124
},
@@ -196,7 +206,7 @@ func TestDomainValidator_Validate(t *testing.T) {
196206
},
197207
{
198208
description: "Failure to format expected domain should fail",
199-
expected: "ab--cd.example.com",
209+
expected: "xn--r-kva.example.com",
200210
actual: "example.com",
201211
errorFunc: func(t *testing.T, e error) {
202212
assert.ErrorContains(t, e, "idna: invalid label")
@@ -205,7 +215,7 @@ func TestDomainValidator_Validate(t *testing.T) {
205215
{
206216
description: "Failure to format check domain should fail",
207217
expected: "example.com",
208-
actual: "ab--cd.example.com",
218+
actual: "xn--r-kva.example.com",
209219
errorFunc: func(t *testing.T, e error) {
210220
assert.ErrorContains(t, e, "idna: invalid label")
211221
},

0 commit comments

Comments
 (0)