Skip to content

Commit 019e943

Browse files
committed
fix bug in whitelist
1 parent 23608bf commit 019e943

3 files changed

Lines changed: 54 additions & 7 deletions

File tree

internal/common/config/loader.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,16 +434,17 @@ func (l *ConfigLoader) loadServerConfig(cfg *Config, def Definition) {
434434
cfg.Server.Auth.OIDC.ClientSecret = oidc.ClientSecret
435435
cfg.Server.Auth.OIDC.ClientUrl = oidc.ClientUrl
436436
cfg.Server.Auth.OIDC.Issuer = oidc.Issuer
437-
cfg.Server.Auth.OIDC.Scopes = oidc.Scopes
438-
cfg.Server.Auth.OIDC.Whitelist = oidc.Whitelist
437+
// Use parseStringList to support both comma-separated strings and YAML lists
438+
cfg.Server.Auth.OIDC.Scopes = parseStringList(l.v.Get("auth.oidc.scopes"))
439+
cfg.Server.Auth.OIDC.Whitelist = parseStringList(l.v.Get("auth.oidc.whitelist"))
439440
// Builtin-specific fields (only used when auth.mode=builtin)
440441
if oidc.AutoSignup != nil {
441442
cfg.Server.Auth.OIDC.AutoSignup = *oidc.AutoSignup
442443
} else {
443444
// Default to true - if OIDC is configured, auto-signup is expected
444445
cfg.Server.Auth.OIDC.AutoSignup = true
445446
}
446-
cfg.Server.Auth.OIDC.AllowedDomains = oidc.AllowedDomains
447+
cfg.Server.Auth.OIDC.AllowedDomains = parseStringList(l.v.Get("auth.oidc.allowedDomains"))
447448
cfg.Server.Auth.OIDC.ButtonLabel = oidc.ButtonLabel
448449
// Load role mapping configuration
449450
if oidc.RoleMapping != nil {
@@ -1246,6 +1247,35 @@ func parseWorkerLabels(labelsStr string) map[string]string {
12461247
return labels
12471248
}
12481249

1250+
// parseStringList parses input that can be either a comma-separated string or a list of strings.
1251+
// This allows config values to be specified as either:
1252+
// - YAML list: ["a", "b", "c"]
1253+
// - Comma-separated string: "a,b,c"
1254+
//
1255+
// Empty strings and whitespace-only entries are filtered out.
1256+
func parseStringList(input interface{}) []string {
1257+
var result []string
1258+
switch v := input.(type) {
1259+
case string:
1260+
if v != "" {
1261+
for _, s := range strings.Split(v, ",") {
1262+
if trimmed := strings.TrimSpace(s); trimmed != "" {
1263+
result = append(result, trimmed)
1264+
}
1265+
}
1266+
}
1267+
case []interface{}:
1268+
for _, item := range v {
1269+
if s, ok := item.(string); ok && s != "" {
1270+
result = append(result, s)
1271+
}
1272+
}
1273+
case []string:
1274+
result = v
1275+
}
1276+
return result
1277+
}
1278+
12491279
func cleanServerBasePath(s string) string {
12501280
if s == "" {
12511281
return ""

internal/service/oidcprovision/service.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,15 @@ func (s *Service) syncUserRole(ctx context.Context, user *auth.User, claims OIDC
240240
// Logic:
241241
// - If whitelist is not empty and email is in whitelist: ALLOW
242242
// - If allowedDomains is not empty and email domain is in allowedDomains: ALLOW
243-
// - If allowedDomains is not empty and email domain is NOT in allowedDomains: DENY
243+
// - If either whitelist or allowedDomains is configured but email doesn't match: DENY
244244
// - If both whitelist and allowedDomains are empty: ALLOW (no restrictions)
245245
func (s *Service) isEmailAllowed(email string) bool {
246246
email = strings.ToLower(email)
247+
hasWhitelist := len(s.config.Whitelist) > 0
248+
hasAllowedDomains := len(s.config.AllowedDomains) > 0
247249

248250
// Check whitelist first (takes precedence)
249-
if len(s.config.Whitelist) > 0 {
251+
if hasWhitelist {
250252
for _, allowed := range s.config.Whitelist {
251253
if strings.EqualFold(email, allowed) {
252254
return true
@@ -255,14 +257,17 @@ func (s *Service) isEmailAllowed(email string) bool {
255257
}
256258

257259
// Check allowed domains
258-
if len(s.config.AllowedDomains) > 0 {
260+
if hasAllowedDomains {
259261
domain := stringutil.ExtractEmailDomain(email)
260262
for _, allowed := range s.config.AllowedDomains {
261263
if strings.EqualFold(domain, allowed) {
262264
return true
263265
}
264266
}
265-
// Domain not in allowed list
267+
}
268+
269+
// If any restriction is configured but email didn't match, deny
270+
if hasWhitelist || hasAllowedDomains {
266271
return false
267272
}
268273

internal/service/oidcprovision/service_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ func TestIsEmailAllowed(t *testing.T) {
480480
481481
expected: true,
482482
},
483+
{
484+
name: "whitelist only - email not in whitelist - denied",
485+
whitelist: []string{"[email protected]"},
486+
487+
expected: false,
488+
},
489+
{
490+
name: "whitelist only - email in whitelist - allowed",
491+
whitelist: []string{"[email protected]"},
492+
493+
expected: true,
494+
},
483495
}
484496

485497
for _, tt := range tests {

0 commit comments

Comments
 (0)