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:
Livio Spring
2026-03-16 14:30:17 +00:00
committed by GitHub
co-authored by Marco A.
parent 7c0e1750f3
commit 500821ce0c
33 changed files with 440 additions and 264 deletions
+16 -5
View File
@@ -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")
+52 -24
View File
@@ -69,10 +69,15 @@ func TestCrypt(t *testing.T) {
wantErr bool
}{
{
"encrypt",
args{[]byte("test"), &mockEncCrypto{}},
&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")},
false,
name: "encrypt ok",
args: args{[]byte("test"), &mockEncCrypto{}},
want: &CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")},
wantErr: false,
},
{
name: "encryption algorithm nil should return error",
args: args{[]byte("test"), nil},
wantErr: true,
},
}
for _, tt := range tests {
@@ -101,10 +106,15 @@ func TestEncrypt(t *testing.T) {
wantErr bool
}{
{
"ok",
args{[]byte("test"), &mockEncCrypto{}},
&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")},
false,
name: "ok",
args: args{[]byte("test"), &mockEncCrypto{}},
want: &CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")},
wantErr: false,
},
{
name: "encryption algorithm nil should return error",
args: args{[]byte("test"), nil},
wantErr: true,
},
}
for _, tt := range tests {
@@ -133,16 +143,25 @@ func TestDecrypt(t *testing.T) {
wantErr bool
}{
{
"ok",
args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")}, &mockEncCrypto{}},
[]byte("test"),
false,
name: "ok",
args: args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")}, &mockEncCrypto{}},
want: []byte("test"),
wantErr: false,
},
{
"wrong id",
args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID2", Crypted: []byte("test")}, &mockEncCrypto{}},
nil,
true,
name: "wrong id",
args: args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID2", Crypted: []byte("test")}, &mockEncCrypto{}},
wantErr: true,
},
{
name: "when encryption algorithm is nil should return error",
args: args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID2", Crypted: []byte("test")}, nil},
wantErr: true,
},
{
name: "when crypto value is nil should return error",
args: args{nil, &mockEncCrypto{}},
wantErr: true,
},
}
for _, tt := range tests {
@@ -171,16 +190,25 @@ func TestDecryptString(t *testing.T) {
wantErr bool
}{
{
"ok",
args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")}, &mockEncCrypto{}},
"test",
false,
name: "ok",
args: args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID", Crypted: []byte("test")}, &mockEncCrypto{}},
want: "test",
wantErr: false,
},
{
"wrong id",
args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID2", Crypted: []byte("test")}, &mockEncCrypto{}},
"",
true,
name: "wrong id",
args: args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID2", Crypted: []byte("test")}, &mockEncCrypto{}},
wantErr: true,
},
{
name: "when encryption algorithm is nil should return error",
args: args{&CryptoValue{CryptoType: TypeEncryption, Algorithm: "enc", KeyID: "keyID2", Crypted: []byte("test")}, nil},
wantErr: true,
},
{
name: "when crypto value is nil should return error",
args: args{nil, &mockEncCrypto{}},
wantErr: true,
},
}
for _, tt := range tests {
+46 -2
View File
@@ -2,6 +2,7 @@ package crypto
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
@@ -11,6 +12,8 @@ import (
"fmt"
"math/big"
"time"
"github.com/zitadel/zitadel/internal/zerrors"
)
func GenerateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error) {
@@ -128,18 +131,40 @@ func PrivateKeyToBytes(priv *rsa.PrivateKey) []byte {
)
}
func PrivateKeyToBytesPKCS8(priv crypto.PrivateKey) ([]byte, error) {
der, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
return nil, err
}
buffer := bytes.NewBuffer(nil)
err = pem.Encode(buffer,
&pem.Block{
Type: "PRIVATE KEY",
Bytes: der,
},
)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func PublicKeyToBytes(pub *rsa.PublicKey) ([]byte, error) {
pubASN1, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return nil, err
}
pubBytes := pem.EncodeToMemory(&pem.Block{
buffer := bytes.NewBuffer(nil)
err = pem.Encode(buffer, &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubASN1,
})
if err != nil {
return nil, err
}
return pubBytes, nil
return buffer.Bytes(), nil
}
func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error) {
@@ -160,6 +185,25 @@ func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error) {
return key, nil
}
func BytesToPrivateKeyPKCS8[T crypto.PrivateKey](priv []byte) (T, error) {
var zero T
if len(priv) == 0 {
return zero, ErrEmpty
}
block, _ := pem.Decode(priv)
if block == nil {
return zero, ErrEmpty
}
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return zero, err
}
if k, ok := key.(T); ok {
return k, nil
}
return zero, zerrors.ThrowInvalidArgumentf(nil, "CRYP-9n2s3", "wrong type: expected %T, got %T", zero, key)
}
var ErrEmpty = errors.New("cannot decode, empty data")
func BytesToPublicKey(pub []byte) (*rsa.PublicKey, error) {