From 5dd8526833f4f673a409d9588a58f11e739f97da Mon Sep 17 00:00:00 2001 From: Stavros Date: Mon, 22 Sep 2025 19:29:55 +0300 Subject: [PATCH] fix: fix key normalization function handing more cases than it needs to --- internal/utils/decoders/decoders.go | 14 +++++++++++--- internal/utils/decoders/decoders_test.go | 5 +++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/utils/decoders/decoders.go b/internal/utils/decoders/decoders.go index 7493717..63604b1 100644 --- a/internal/utils/decoders/decoders.go +++ b/internal/utils/decoders/decoders.go @@ -18,10 +18,14 @@ func NormalizeKeys(keys map[string]string, rootName string, sep string) map[stri finalKey = append(finalKey, rootName) finalKey = append(finalKey, "providers") - kebabKey := strings.ToLower(k) + lowerKey := strings.ToLower(k) + + if !strings.HasPrefix(lowerKey, "providers"+sep) { + continue + } for _, known := range knownKeys { - if strings.HasSuffix(kebabKey, strings.ReplaceAll(known, "-", sep)) { + if strings.HasSuffix(lowerKey, strings.ReplaceAll(known, "-", sep)) { suffix = known break } @@ -31,7 +35,11 @@ func NormalizeKeys(keys map[string]string, rootName string, sep string) map[stri continue } - clientNameParts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(kebabKey, sep+strings.ReplaceAll(suffix, "-", sep)), "providers"+sep), sep) + if strings.TrimSpace(strings.TrimSuffix(strings.TrimPrefix(lowerKey, "providers"+sep), strings.ReplaceAll(suffix, "-", sep))) == "" { + continue + } + + clientNameParts := strings.Split(strings.TrimPrefix(strings.TrimSuffix(lowerKey, sep+strings.ReplaceAll(suffix, "-", sep)), "providers"+sep), sep) for i, p := range clientNameParts { if i == 0 { diff --git a/internal/utils/decoders/decoders_test.go b/internal/utils/decoders/decoders_test.go index 285760c..fdec286 100644 --- a/internal/utils/decoders/decoders_test.go +++ b/internal/utils/decoders/decoders_test.go @@ -14,6 +14,8 @@ func TestNormalizeKeys(t *testing.T) { "PROVIDERS_CLIENT1_CLIENT_SECRET": "my-client-secret", "PROVIDERS_MY_AWESOME_CLIENT_CLIENT_ID": "my-awesome-client-id", "PROVIDERS_MY_AWESOME_CLIENT_CLIENT_SECRET_FILE": "/path/to/secret", + "I_LOOK_LIKE_A_KEY_CLIENT_ID": "should-not-appear", + "PROVIDERS_CLIENT_ID": "should-not-appear", } expected := map[string]string{ "tinyauth.providers.client1.clientId": "my-client-id", @@ -31,6 +33,9 @@ func TestNormalizeKeys(t *testing.T) { "providers-client1-client-secret": "my-client-secret", "providers-my-awesome-client-client-id": "my-awesome-client-id", "providers-my-awesome-client-client-secret-file": "/path/to/secret", + "providers-should-not-appear-client": "should-not-appear", + "i-look-like-a-key-client-id": "should-not-appear", + "providers-client-id": "should-not-appear", } expected = map[string]string{ "tinyauth.providers.client1.clientId": "my-client-id",