Skip to content

Commit

Permalink
fix: Panics when passing a malformed GitSession (#3058)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanhuhta committed Mar 1, 2024
1 parent 38218a8 commit bbb5f55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/querier/vcs/client/encryption.go
Expand Up @@ -3,6 +3,7 @@ package client
import (
"encoding/base64"
"encoding/json"
"errors"

encryption "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
"golang.org/x/oauth2"
Expand All @@ -24,11 +25,18 @@ func encryptToken(token *oauth2.Token, key []byte) (string, error) {
return base64.StdEncoding.EncodeToString(enc), nil
}

const gcmNonceSize = 12

func decryptToken(encodedText string, key []byte) (*oauth2.Token, error) {
encryptedData, err := base64.StdEncoding.DecodeString(encodedText)
if err != nil {
return nil, err
}

if len(encryptedData) < gcmNonceSize {
return nil, errors.New("malformed token")
}

cipher, err := encryption.NewGCMCipher(key)
if err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions pkg/querier/vcs/client/encryption_test.go
Expand Up @@ -23,3 +23,13 @@ func TestEncodeOAuth(t *testing.T) {
require.NoError(t, err)
require.Equal(t, token, actual)
}

func Test_decryptToken(t *testing.T) {
t.Run("malformed token shorter than nonce size", func(t *testing.T) {
encoded := "xxxx"
key := []byte("0123456789abcdef")

_, err := decryptToken(encoded, key)
require.EqualError(t, err, "malformed token")
})
}

0 comments on commit bbb5f55

Please sign in to comment.