mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: additional check when decrypting values (#34637)
* Chore: additional check when decrypting values * Apply suggestions from code review
This commit is contained in:
committed by
GitHub
parent
ab26c4dfa4
commit
a5082ab112
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"golang.org/x/crypto/pbkdf2"
|
||||
@@ -15,6 +16,9 @@ const saltLength = 8
|
||||
|
||||
// Decrypt decrypts a payload with a given secret.
|
||||
func Decrypt(payload []byte, secret string) ([]byte, error) {
|
||||
if len(payload) < saltLength {
|
||||
return nil, fmt.Errorf("unable to compute salt")
|
||||
}
|
||||
salt := payload[:saltLength]
|
||||
key, err := encryptionKeyToBytes(secret, string(salt))
|
||||
if err != nil {
|
||||
|
||||
@@ -27,4 +27,11 @@ func TestEncryption(t *testing.T) {
|
||||
|
||||
assert.Equal(t, []byte("grafana"), decrypted)
|
||||
})
|
||||
|
||||
t.Run("decrypting empty payload should not fail", func(t *testing.T) {
|
||||
_, err := Decrypt([]byte(""), "1234")
|
||||
require.Error(t, err)
|
||||
|
||||
assert.Equal(t, "unable to compute salt", err.Error())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user