Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Fixed the pointer overwrite issue in oauthServer metadata #183

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/auth/oauthserver/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ func getJSONWebKeys(publicKeys []rsa.PublicKey) (jwk.Set, error) {
if err != nil {
return nil, fmt.Errorf("failed to write public key. Error: %w", err)
}

err = key.Set(KeyMetadataPublicCert, &publicKey)
var localPublicKey rsa.PublicKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var localPublicKey rsa.PublicKey
localPublicKey := publicKey

just replace with oneline

localPublicKey = publicKey
err = key.Set(KeyMetadataPublicCert, &localPublicKey)
if err != nil {
return nil, fmt.Errorf("failed to write public key. Error: %w", err)
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/auth/oauthserver/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package oauthserver

import (
"crypto/rand"
"crypto/rsa"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetJSONWebKeys(t *testing.T) {
newpriv, err := rsa.GenerateMultiPrimeKey(rand.Reader, 4, 128)
if err != nil {
t.Errorf("failed to generate key")
}
oldpriv, err := rsa.GenerateMultiPrimeKey(rand.Reader, 4, 128)
if err != nil {
t.Errorf("failed to generate key")
}
newKey := newpriv.PublicKey
oldKey := oldpriv.PublicKey
publicKeys := []rsa.PublicKey{newKey, oldKey}
keyset, err := getJSONWebKeys(publicKeys)
assert.Nil(t, err)
assert.NotNil(t, keyset)
oldJwkKey, exists := keyset.Get(1)
assert.True(t, exists)
oldpublicKey, exists := oldJwkKey.Get(KeyMetadataPublicCert)
op, ok := oldpublicKey.(*rsa.PublicKey)
assert.True(t, ok)
assert.Equal(t, &oldKey, op)
newJwkKey, exists := keyset.Get(0)
assert.True(t, exists)
newpublicKey, exists := newJwkKey.Get(KeyMetadataPublicCert)
np, ok := newpublicKey.(*rsa.PublicKey)
assert.True(t, ok)
assert.NotEqual(t, np, op)
assert.Equal(t, &newKey, np)
}