Skip to content

Commit fdb8dc7

Browse files
authored
fix: vcs.decodeToken can eat an error (#4336)
When the cookie contains valid base64 in json, with an empty token field. We return a nil token and eat the innerError, which leads to nil pointer exception down the line.
1 parent f9c4f2c commit fdb8dc7

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pkg/frontend/vcs/token.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,11 @@ func decodeToken(value string, key []byte) (*oauth2.Token, error) {
153153
// This may be a deprecated cookie. Deprecated cookies are base64 encoded deprecatedGitSessionTokenCookie objects.
154154
token, innerErr := decodeDeprecatedToken(decoded, key)
155155
if innerErr != nil {
156-
// Deprecated fallback failed, return the original error.
157-
return nil, err
156+
// Deprecated fallback failed, return the original error if exists.
157+
if err != nil {
158+
return nil, err
159+
}
160+
return nil, innerErr
158161
}
159162
return token, nil
160163
}

pkg/frontend/vcs/token_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"net/http"
88
"net/url"
9+
"strings"
910
"testing"
1011
"time"
1112

@@ -193,6 +194,26 @@ func Test_tokenFromRequest(t *testing.T) {
193194
require.Error(t, err)
194195
require.EqualError(t, err, wantErr)
195196
})
197+
198+
t.Run("token with garbage should fail", func(t *testing.T) {
199+
githubSessionSecret = []byte("16_byte_key_XXXX")
200+
201+
// a cookie with false metadata
202+
cookieData, err := json.Marshal(map[string]interface{}{
203+
"metadata": base64.StdEncoding.EncodeToString([]byte(strings.Repeat("x", 128))),
204+
"expiry": 1234,
205+
})
206+
require.NoError(t, err)
207+
208+
req := connect.NewRequest(&vcsv1.GetFileRequest{})
209+
req.Header().Add("Cookie", "pyroscope_git_session="+base64.RawStdEncoding.EncodeToString(cookieData))
210+
211+
token, err := tokenFromRequest(ctx, req)
212+
require.Error(t, err)
213+
require.EqualError(t, err, "cipher: message authentication failed")
214+
require.Nil(t, token)
215+
216+
})
196217
}
197218

198219
func Test_encodeTokenInCookie(t *testing.T) {

0 commit comments

Comments
 (0)