Skip to content

Commit 8614f87

Browse files
committed
fix: no need for idna conversion in domain validator
1 parent 61d372b commit 8614f87

4 files changed

Lines changed: 20 additions & 66 deletions

File tree

‎internal/utils/app_utils.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@ func SafeParseAppURL(str string) (string, error) {
3838
return "", fmt.Errorf("ip addresses not allowed")
3939
}
4040

41-
hostname, err = idna.Lookup.ToASCII(hostname)
41+
i := idna.New(
42+
idna.MapForLookup(),
43+
idna.Transitional(false),
44+
idna.BidiRule(),
45+
idna.StrictDomainName(false),
46+
idna.CheckHyphens(true),
47+
idna.CheckJoiners(false),
48+
)
49+
50+
hostname, err = i.ToASCII(hostname)
4251

4352
if err != nil {
4453
return "", fmt.Errorf("failed to convert hostname to ascii: %w", err)

‎internal/utils/app_utils_test.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func TestSafeParseAPPURL(t *testing.T) {
4343
assert.NoError(t, err)
4444
assert.Equal(t, expected, result)
4545

46+
// Underscores
47+
appURL = "http://sub_tinyauth.app"
48+
expected = "http://sub_tinyauth.app"
49+
result, err = utils.SafeParseAppURL(appURL)
50+
assert.NoError(t, err)
51+
assert.Equal(t, expected, result)
52+
4653
// Lowercase
4754
appURL = "HTTP://SUb.tinyAUth.aPP"
4855
expected = "http://sub.tinyauth.app"
@@ -66,7 +73,7 @@ func TestSafeParseAPPURL(t *testing.T) {
6673
assert.ErrorContains(t, err, "invalid url")
6774

6875
// Invalid punycode
69-
appURL = "http://ab--cd.example.com"
76+
appURL = "http://xn--h-kva.example.com"
7077
_, err = utils.SafeParseAppURL(appURL)
7178
assert.ErrorContains(t, err, "failed to convert hostname to ascii")
7279

‎pkg/validators/domain_validator.go‎

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"net"
1212
"net/url"
1313
"strings"
14-
15-
"golang.org/x/net/idna"
1614
)
1715

1816
// Errors
@@ -114,23 +112,11 @@ func (v *DomainValidator) getURL(i string) (*url.URL, error) {
114112
}
115113

116114
func (v *DomainValidator) getHostname(hostname string) (string, error) {
117-
hostname = strings.ToLower(hostname)
118-
hostname = strings.TrimSuffix(hostname, ".")
119115
if net.ParseIP(hostname) != nil {
120116
return "", fmt.Errorf("ip addresses are not supported")
121117
}
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)
131-
if err != nil {
132-
return "", fmt.Errorf("failed to convert hostname to ascii: %w", err)
133-
}
118+
hostname = strings.ToLower(hostname)
119+
hostname = strings.TrimSuffix(hostname, ".")
134120
return hostname, nil
135121
}
136122

‎pkg/validators/domain_validator_test.go‎

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ 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-
},
6353
{
6454
description: "Domain without scheme should parse if scheme is disabled",
6555
input: "example.com",
@@ -111,18 +101,6 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
111101
assert.ErrorContains(t, e, "ip addresses are not supported")
112102
},
113103
},
114-
{
115-
description: "Domains with unicode characters should be allowed",
116-
input: "bücher.example.com",
117-
expected: "xn--bcher-kva.example.com",
118-
},
119-
{
120-
description: "Invalid IDNA domain should fail",
121-
input: "xn--r-kva.example.com",
122-
errorFunc: func(t *testing.T, e error) {
123-
assert.ErrorContains(t, e, "invalid label")
124-
},
125-
},
126104
{
127105
description: "With port enabled without any port should work",
128106
options: DomainValidatorOptions{WithPort: true},
@@ -204,22 +182,6 @@ func TestDomainValidator_Validate(t *testing.T) {
204182
expected: "https://example.com:443",
205183
actual: "https://example.com:443",
206184
},
207-
{
208-
description: "Failure to format expected domain should fail",
209-
expected: "xn--r-kva.example.com",
210-
actual: "example.com",
211-
errorFunc: func(t *testing.T, e error) {
212-
assert.ErrorContains(t, e, "idna: invalid label")
213-
},
214-
},
215-
{
216-
description: "Failure to format check domain should fail",
217-
expected: "example.com",
218-
actual: "xn--r-kva.example.com",
219-
errorFunc: func(t *testing.T, e error) {
220-
assert.ErrorContains(t, e, "idna: invalid label")
221-
},
222-
},
223185
{
224186
description: "Valid domains with matching schemes and ports should pass",
225187
options: DomainValidatorOptions{WithScheme: true, AllowedSchemes: []string{"https", "http"}, WithPort: true},
@@ -246,16 +208,6 @@ func TestDomainValidator_Validate(t *testing.T) {
246208
actual: "example.com",
247209
expected: "example.com",
248210
},
249-
{
250-
description: "Unicode valid domains should pass",
251-
expected: "xn--bcher-kva.example.com",
252-
actual: "bücher.example.com",
253-
},
254-
{
255-
description: "Unicode valid domains should pass (reverse)",
256-
expected: "bücher.example.com",
257-
actual: "xn--bcher-kva.example.com",
258-
},
259211
{
260212
description: "Non matching hostnames should fail",
261213
expected: "example.com",

0 commit comments

Comments
 (0)