Compare commits

...
Author SHA1 Message Date
Stavros 47b5866153 fix: allow for undescores and leading hyphens in domain validator 2026-08-21 13:22:41 +03:00
2 changed files with 22 additions and 4 deletions
+9 -1
View File
@@ -119,7 +119,15 @@ func (v *DomainValidator) getHostname(hostname string) (string, error) {
if net.ParseIP(hostname) != nil {
return "", fmt.Errorf("ip addresses are not supported")
}
hostname, err := idna.Lookup.ToASCII(hostname)
i := idna.New(
idna.MapForLookup(),
idna.Transitional(false),
idna.BidiRule(),
idna.StrictDomainName(false),
idna.CheckHyphens(false),
idna.CheckJoiners(false),
)
hostname, err := i.ToASCII(hostname)
if err != nil {
return "", fmt.Errorf("failed to convert hostname to ascii: %w", err)
}
+13 -3
View File
@@ -50,6 +50,16 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
input: "https://example.com",
expected: "example.com",
},
{
description: "Domain with underscores should pass",
input: "https://my_domain.com",
expected: "my_domain.com",
},
{
description: "Domain with leading hyphen should pass",
input: "https://-my-domain.com",
expected: "-my-domain.com",
},
{
description: "Domain without scheme should parse if scheme is disabled",
input: "example.com",
@@ -108,7 +118,7 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
},
{
description: "Invalid IDNA domain should fail",
input: "ab--cd.example.com",
input: "xn--r-kva.example.com",
errorFunc: func(t *testing.T, e error) {
assert.ErrorContains(t, e, "invalid label")
},
@@ -196,7 +206,7 @@ func TestDomainValidator_Validate(t *testing.T) {
},
{
description: "Failure to format expected domain should fail",
expected: "ab--cd.example.com",
expected: "xn--r-kva.example.com",
actual: "example.com",
errorFunc: func(t *testing.T, e error) {
assert.ErrorContains(t, e, "idna: invalid label")
@@ -205,7 +215,7 @@ func TestDomainValidator_Validate(t *testing.T) {
{
description: "Failure to format check domain should fail",
expected: "example.com",
actual: "ab--cd.example.com",
actual: "xn--r-kva.example.com",
errorFunc: func(t *testing.T, e error) {
assert.ErrorContains(t, e, "idna: invalid label")
},