From d0c23546ec34722b98fb1ade26b56f34d55d8c99 Mon Sep 17 00:00:00 2001 From: Roman Kolokhanin Date: Wed, 4 Dec 2024 23:56:36 +0300 Subject: [PATCH] fix(oidc): prompts slice conversion function returns slice which contains unexpected empty strings (#8997) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Which Problems Are Solved Slice initialized with a fixed length instead of capacity, this leads to unexpected results when calling the append function. # How the Problems Are Solved fixed slice initialization, slice is initialized with zero length and with capacity of function's argument # Additional Changes test case added # Additional Context none Co-authored-by: Kolokhanin Roman Co-authored-by: Tim Möhlmann --- internal/api/oidc/auth_request_converter.go | 2 +- .../api/oidc/auth_request_converter_test.go | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/internal/api/oidc/auth_request_converter.go b/internal/api/oidc/auth_request_converter.go index cd52cdbe5..2144ca8ba 100644 --- a/internal/api/oidc/auth_request_converter.go +++ b/internal/api/oidc/auth_request_converter.go @@ -158,7 +158,7 @@ func IpFromContext(ctx context.Context) net.IP { } func PromptToBusiness(oidcPrompt []string) []domain.Prompt { - prompts := make([]domain.Prompt, len(oidcPrompt)) + prompts := make([]domain.Prompt, 0, len(oidcPrompt)) for _, oidcPrompt := range oidcPrompt { switch oidcPrompt { case oidc.PromptNone: diff --git a/internal/api/oidc/auth_request_converter_test.go b/internal/api/oidc/auth_request_converter_test.go index b35d51966..06750aad3 100644 --- a/internal/api/oidc/auth_request_converter_test.go +++ b/internal/api/oidc/auth_request_converter_test.go @@ -94,3 +94,36 @@ func TestResponseModeToOIDC(t *testing.T) { }) } } + +func TestPromptToBusiness(t *testing.T) { + type args struct { + oidcPrompt []string + } + tests := []struct { + name string + args args + want []domain.Prompt + }{ + { + name: "unspecified", + args: args{nil}, + want: []domain.Prompt{}, + }, + { + name: "invalid", + args: args{[]string{"non_existing_prompt"}}, + want: []domain.Prompt{}, + }, + { + name: "prompt_none", + args: args{[]string{oidc.PromptNone}}, + want: []domain.Prompt{domain.PromptNone}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := PromptToBusiness(tt.args.oidcPrompt) + assert.Equal(t, tt.want, got) + }) + } +}