Skip to content

Commit 9701e18

Browse files
authored
Add cors check to config (#516)
1 parent e545bc2 commit 9701e18

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

pkg/apperrors/apperrors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,5 @@ var (
166166

167167
ErrLetsEncryptMissingCacheDir = errors.New("letsencrypt cache dir has not been set")
168168
ErrHijackerMethodMissing = errors.New("writer does not implement http.Hijacker method")
169+
ErrInvalidOriginWithCreds = errors.New("origin cannot be set to * together with AllowedCredentials true")
169170
)

pkg/keycloak/config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ func (r *Config) IsValid() error {
328328
r.isOpenIDProviderProxyValid,
329329
r.isMaxIdlleConnValid,
330330
r.isSameSiteValid,
331+
r.isCorsValid,
331332
r.isTLSFilesValid,
332333
r.isAdminTLSFilesValid,
333334
r.isLetsEncryptValid,
@@ -910,3 +911,12 @@ func (r *Config) isEnableLoAValid() error {
910911
}
911912
return nil
912913
}
914+
915+
func (r *Config) isCorsValid() error {
916+
for _, origin := range r.CorsOrigins {
917+
if origin == "*" && r.CorsCredentials {
918+
return apperrors.ErrInvalidOriginWithCreds
919+
}
920+
}
921+
return nil
922+
}

pkg/keycloak/config/config_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,3 +2592,44 @@ func TestEnableLoa(t *testing.T) {
25922592
)
25932593
}
25942594
}
2595+
2596+
func TestIsCorsValid(t *testing.T) {
2597+
testCases := []struct {
2598+
Name string
2599+
Config *Config
2600+
Valid bool
2601+
}{
2602+
{
2603+
Name: "ValidOrigin",
2604+
Config: &Config{
2605+
CorsOrigins: []string{"example.com"},
2606+
CorsCredentials: false,
2607+
},
2608+
Valid: true,
2609+
},
2610+
{
2611+
Name: "InvalidOrigin",
2612+
Config: &Config{
2613+
CorsOrigins: []string{"*"},
2614+
CorsCredentials: true,
2615+
},
2616+
Valid: false,
2617+
},
2618+
}
2619+
2620+
for _, testCase := range testCases {
2621+
t.Run(
2622+
testCase.Name,
2623+
func(t *testing.T) {
2624+
err := testCase.Config.isCorsValid()
2625+
if err != nil && testCase.Valid {
2626+
t.Fatalf("Expected test not to fail")
2627+
}
2628+
2629+
if err == nil && !testCase.Valid {
2630+
t.Fatalf("Expected test to fail")
2631+
}
2632+
},
2633+
)
2634+
}
2635+
}

0 commit comments

Comments
 (0)