Skip to content

Commit 50c25e4

Browse files
committed
fix: fix domain validator with port but no scheme and https or http
1 parent d6087dc commit 50c25e4

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

‎internal/controller/oauth_controller_test.go‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,6 @@ func TestOAuthController_isRedirectSafe(t *testing.T) {
113113
redirectURI: "https:/malicious",
114114
expected: false,
115115
},
116-
{
117-
description: "Redirect URI without scheme returns false",
118-
appURL: "https://tinyauth.example.com",
119-
cookieDomain: "example.com",
120-
subdomainsEnabled: true,
121-
redirectURI: "tinyauth.example.com",
122-
expected: false,
123-
},
124116
{
125117
description: "Relative redirect URI returns false",
126118
appURL: "https://tinyauth.example.com",

‎pkg/validators/domain_validator.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (v *DomainValidator) getURL(i string) (*url.URL, error) {
6363
return nil, ErrInvalidURL
6464
}
6565

66-
if v.opts.WithPort && !v.opts.WithScheme && u.Port() == "" {
66+
if v.opts.WithPort && u.Port() == "" && (u.Scheme != "http" && u.Scheme != "https") {
6767
return nil, fmt.Errorf("port validation is enabled but port is missing in input url and schemes are not enabled")
6868
}
6969

‎pkg/validators/domain_validator_test.go‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ func TestDomainValidator_SafeHostname(t *testing.T) {
104104
assert.ErrorContains(t, e, "input url is missing scheme")
105105
},
106106
},
107+
{
108+
description: "With port enabled but without scheme and url with https should work",
109+
options: DomainValidatorOptions{WithPort: true},
110+
input: "https://example.com",
111+
expected: "example.com",
112+
},
113+
{
114+
description: "With port enabled but without scheme and url with http should work",
115+
options: DomainValidatorOptions{WithPort: true},
116+
input: "http://example.com",
117+
expected: "example.com",
118+
},
119+
{
120+
description: "With port enabled but without scheme and url with port should work",
121+
options: DomainValidatorOptions{WithPort: true},
122+
input: "example.com:8080",
123+
expected: "example.com",
124+
},
107125
}
108126

109127
for _, test := range tests {
@@ -194,7 +212,7 @@ func TestDomainValidator_Validate(t *testing.T) {
194212
expected: "ssh://example.com:22",
195213
actual: "ssh://example.com",
196214
errorFunc: func(t *testing.T, e error) {
197-
assert.ErrorContains(t, e, "failed to get effective port for url")
215+
assert.ErrorContains(t, e, "port validation is enabled but port is missing in input url and schemes are not enabled")
198216
},
199217
},
200218
{
@@ -203,9 +221,21 @@ func TestDomainValidator_Validate(t *testing.T) {
203221
expected: "ssh://example.com",
204222
actual: "ssh://example.com:22",
205223
errorFunc: func(t *testing.T, e error) {
206-
assert.ErrorContains(t, e, "failed to get effective port for url")
224+
assert.ErrorContains(t, e, "port validation is enabled but port is missing in input url and schemes are not enabled")
207225
},
208226
},
227+
{
228+
description: "Port enabled but no scheme and https scheme should pass",
229+
options: DomainValidatorOptions{WithPort: true},
230+
expected: "https://example.com",
231+
actual: "https://example.com",
232+
},
233+
{
234+
description: "Port enabled but no scheme and http scheme should pass",
235+
options: DomainValidatorOptions{WithPort: true},
236+
expected: "http://example.com",
237+
actual: "http://example.com",
238+
},
209239
{
210240
description: "Port validation with port and no scheme should fail with different port",
211241
options: DomainValidatorOptions{WithPort: true},

0 commit comments

Comments
 (0)