Skip to content

Commit c17e1f5

Browse files
authored
chore: prevent panic by enforcing nonce length (#2459)
## What kind of change does this PR introduce? Bug fix / chore ## What is the current behavior? Current `IsValid` in `crypto.go` only validates that the nonce length is > 0. `cipher.NewGCM()` from the Go standard library panics if the nonce is not exactly 12 bytes. ## What is the new behavior? Matches the `IsValid` check with the behaviour of `cipher.NewGCM()` and prevents a panic.
1 parent c7b58be commit c17e1f5

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

internal/crypto/crypto.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ type EncryptedString struct {
6060
}
6161

6262
func (es *EncryptedString) IsValid() bool {
63-
return es.KeyID != "" && len(es.Data) > 0 && len(es.Nonce) > 0 && es.Algorithm == "aes-gcm-hkdf"
63+
// cipher.NewGCM() is always used, which panics on a Nonce that is not 12 bytes
64+
// enforce that the nonce length and other values are correct
65+
return es.KeyID != "" && len(es.Data) > 0 && len(es.Nonce) == 12 && es.Algorithm == "aes-gcm-hkdf"
6466
}
6567

6668
// ShouldReEncrypt tells you if the value encrypted needs to be encrypted again with a newer key.

0 commit comments

Comments
 (0)