mirror of
https://github.com/tinyauthapp/tinyauth.git
synced 2026-08-31 18:43:31 +08:00
fix: allow for undescores and leading hyphens in domain validator (#1088)
This commit is contained in:
@@ -119,7 +119,15 @@ func (v *DomainValidator) getHostname(hostname string) (string, error) {
|
|||||||
if net.ParseIP(hostname) != nil {
|
if net.ParseIP(hostname) != nil {
|
||||||
return "", fmt.Errorf("ip addresses are not supported")
|
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 {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to convert hostname to ascii: %w", err)
|
return "", fmt.Errorf("failed to convert hostname to ascii: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,16 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
|
|||||||
input: "https://example.com",
|
input: "https://example.com",
|
||||||
expected: "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",
|
description: "Domain without scheme should parse if scheme is disabled",
|
||||||
input: "example.com",
|
input: "example.com",
|
||||||
@@ -108,7 +118,7 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Invalid IDNA domain should fail",
|
description: "Invalid IDNA domain should fail",
|
||||||
input: "ab--cd.example.com",
|
input: "xn--r-kva.example.com",
|
||||||
errorFunc: func(t *testing.T, e error) {
|
errorFunc: func(t *testing.T, e error) {
|
||||||
assert.ErrorContains(t, e, "invalid label")
|
assert.ErrorContains(t, e, "invalid label")
|
||||||
},
|
},
|
||||||
@@ -196,7 +206,7 @@ func TestDomainValidator_Validate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Failure to format expected domain should fail",
|
description: "Failure to format expected domain should fail",
|
||||||
expected: "ab--cd.example.com",
|
expected: "xn--r-kva.example.com",
|
||||||
actual: "example.com",
|
actual: "example.com",
|
||||||
errorFunc: func(t *testing.T, e error) {
|
errorFunc: func(t *testing.T, e error) {
|
||||||
assert.ErrorContains(t, e, "idna: invalid label")
|
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",
|
description: "Failure to format check domain should fail",
|
||||||
expected: "example.com",
|
expected: "example.com",
|
||||||
actual: "ab--cd.example.com",
|
actual: "xn--r-kva.example.com",
|
||||||
errorFunc: func(t *testing.T, e error) {
|
errorFunc: func(t *testing.T, e error) {
|
||||||
assert.ErrorContains(t, e, "idna: invalid label")
|
assert.ErrorContains(t, e, "idna: invalid label")
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user