Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/utils/app_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ func IsRedirectSafe(redirectURL string, domain string) bool {
return false
}

cookieDomain, err := GetCookieDomain(redirectURL)

if err != nil {
return false
host := parsedURL.Hostname()
if host == domain {
return true
}

if cookieDomain != domain {
cookieDomain, err := GetCookieDomain(redirectURL)
if err != nil {
return false
}

return true
return cookieDomain == domain
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

func GetLogLevel(level string) zerolog.Level {
Expand Down
37 changes: 36 additions & 1 deletion internal/utils/app_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func TestIsRedirectSafe(t *testing.T) {
// Case with no subdomain
redirectURL := "http://example.com/welcome"
result := utils.IsRedirectSafe(redirectURL, domain)
assert.Equal(t, false, result)
assert.Equal(t, true, result)

// Case with different domain
redirectURL = "http://malicious.com/phishing"
Expand Down Expand Up @@ -202,6 +202,41 @@ func TestIsRedirectSafe(t *testing.T) {
assert.Equal(t, false, result)
}

func TestIsRedirectSafeMultiLevel(t *testing.T) {
// Setup
cookieDomain := "tinyauth.example.com"

// Case with 3rd level domain
redirectURL := "http://tinyauth.example.com/welcome"
result := utils.IsRedirectSafe(redirectURL, cookieDomain)
assert.Equal(t, true, result)

// Case with root domain
redirectURL = "http://example.com/unsafe"
result = utils.IsRedirectSafe(redirectURL, cookieDomain)
assert.Equal(t, false, result)

// Case with 4th level domain
redirectURL = "http://auth.tinyauth.example.com/post-login"
result = utils.IsRedirectSafe(redirectURL, cookieDomain)
assert.Equal(t, true, result)

// Case with 5th level domain (should be unsafe)
redirectURL = "http://x.auth.tinyauth.example.com/deep"
result = utils.IsRedirectSafe(redirectURL, cookieDomain)
assert.Equal(t, false, result)

// Case with different subdomain
redirectURL = "http://auth.tinyauth.example.net/attack"
result = utils.IsRedirectSafe(redirectURL, cookieDomain)
assert.Equal(t, false, result)

// Case with malformed URL
redirectURL = "http://[::1]:namedport"
result = utils.IsRedirectSafe(redirectURL, cookieDomain)
assert.Equal(t, false, result)
}

func TestGetOAuthProvidersConfig(t *testing.T) {
env := []string{"PROVIDERS_CLIENT1_CLIENT_ID=client1-id", "PROVIDERS_CLIENT1_CLIENT_SECRET=client1-secret"}
args := []string{"/tinyauth/tinyauth", "--providers-client2-client-id=client2-id", "--providers-client2-client-secret=client2-secret"}
Expand Down