mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-03 17:38:51 +00:00
fix: prevent possible nil pointer panics (#11728)
# Which Problems Are Solved We found multiple cases where potentially a panic occurred or could occur: - when de/encrypting certain information - returning apps without any configuration type (?) - apple IdPs without a proper private key # How the Problems Are Solved - Added nil checks - Check private key format for apple IdPs - Added necessary helper function # Additional Changes Fixed i18n yaml where the IDP errors were indented under `org` instead of directly under `errors`. # Additional Context - requires backport to v4.x --------- Co-authored-by: Marco A. <[email protected]>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql/driver"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"slices"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
@@ -41,7 +42,7 @@ func (c *CryptoValue) Value() (driver.Value, error) {
|
||||
return json.Marshal(c)
|
||||
}
|
||||
|
||||
func (c *CryptoValue) Scan(src interface{}) error {
|
||||
func (c *CryptoValue) Scan(src any) error {
|
||||
if b, ok := src.([]byte); ok {
|
||||
return json.Unmarshal(b, c)
|
||||
}
|
||||
@@ -58,6 +59,9 @@ func Crypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
||||
}
|
||||
|
||||
func Encrypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
||||
if alg == nil {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "CRYPT-03ltQd", "input encryption algorithm cannot be nil")
|
||||
}
|
||||
encrypted, err := alg.Encrypt(value)
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "CRYPT-qCD0JB", "error encrypting value")
|
||||
@@ -106,13 +110,17 @@ func DecryptString(value *CryptoValue, alg EncryptionAlgorithm) (string, error)
|
||||
}
|
||||
|
||||
func checkEncryptionAlgorithm(value *CryptoValue, alg EncryptionAlgorithm) error {
|
||||
if value == nil {
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-mNsQwe", "input value cannot be nil")
|
||||
}
|
||||
if alg == nil {
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-paiRey", "input encryption algorithm cannot be nil")
|
||||
}
|
||||
if value.Algorithm != alg.Algorithm() {
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Nx7XlT", "value was encrypted with a different key")
|
||||
}
|
||||
for _, id := range alg.DecryptionKeyIDs() {
|
||||
if id == value.KeyID {
|
||||
return nil
|
||||
}
|
||||
if slices.Contains(alg.DecryptionKeyIDs(), value.KeyID) {
|
||||
return nil
|
||||
}
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Kq12vn", "value was encrypted with a different key")
|
||||
}
|
||||
@@ -121,6 +129,9 @@ func CheckToken(alg EncryptionAlgorithm, token string, content string) error {
|
||||
if token == "" {
|
||||
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-Sfefs", "Errors.Intent.InvalidToken")
|
||||
}
|
||||
if alg == nil {
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-edCJsp", "input encryption algorithm cannot be nil")
|
||||
}
|
||||
data, err := base64.RawURLEncoding.DecodeString(token)
|
||||
if err != nil {
|
||||
return zerrors.ThrowPermissionDenied(err, "CRYPTO-Swg31", "Errors.Intent.InvalidToken")
|
||||
|
||||
Reference in New Issue
Block a user