Skip to content

Commit 0c3286e

Browse files
authored
Merge pull request kubernetes#94803 from azush26/modify-bearertoken
Limit the max number of splitting.
2 parents cdc7a29 + 367214d commit 0c3286e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

staging/src/k8s.io/apiserver/pkg/authentication/request/bearertoken/bearertoken.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (a *Authenticator) AuthenticateRequest(req *http.Request) (*authenticator.R
3939
if auth == "" {
4040
return nil, false, nil
4141
}
42-
parts := strings.Split(auth, " ")
42+
parts := strings.SplitN(auth, " ", 3)
4343
if len(parts) < 2 || strings.ToLower(parts[0]) != "bearer" {
4444
return nil, false, nil
4545
}

staging/src/k8s.io/apiserver/pkg/authentication/request/bearertoken/bearertoken_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ func TestAuthenticateRequest(t *testing.T) {
4242
}
4343
}
4444

45+
func TestAuthenticateRequestIncludingValueAfterToken(t *testing.T) {
46+
testCases := []struct {
47+
Req *http.Request
48+
}{
49+
{Req: &http.Request{Header: http.Header{"Authorization": []string{"Bearer token a"}}}},
50+
{Req: &http.Request{Header: http.Header{"Authorization": []string{"Bearer token a b c"}}}},
51+
{Req: &http.Request{Header: http.Header{"Authorization": []string{"Bearer token a"}}}},
52+
}
53+
for i, testCase := range testCases {
54+
auth := New(authenticator.TokenFunc(func(ctx context.Context, token string) (*authenticator.Response, bool, error) {
55+
if token != "token" {
56+
t.Errorf("unexpected token: %s", token)
57+
}
58+
return &authenticator.Response{User: &user.DefaultInfo{Name: "user"}}, true, nil
59+
}))
60+
resp, ok, err := auth.AuthenticateRequest(testCase.Req)
61+
if !ok || resp == nil || err != nil {
62+
t.Errorf("%d: expected valid user", i)
63+
}
64+
}
65+
}
66+
4567
func TestAuthenticateRequestTokenInvalid(t *testing.T) {
4668
auth := New(authenticator.TokenFunc(func(ctx context.Context, token string) (*authenticator.Response, bool, error) {
4769
return nil, false, nil

0 commit comments

Comments
 (0)