fix(crypto): fail loudly when encrypted DB fields cannot be decrypted
GORM Scan / AfterFind hooks for every AES-encrypted column followed
the same lenient pattern:
if decrypted, err := utils.DecryptAESGCM(c.APIKey, key); err == nil {
c.APIKey = decrypted
}
When SYSTEM_AES_KEY was missing, rotated, or the wrong length the
decryption error was swallowed and the in-memory struct kept the raw
"enc:v1:..." ciphertext. The application then happily forwarded the
ciphertext upstream as the actual API key / password, surfacing as
401/403/SignatureDoesNotMatch from third-party vendors. Worse, a
ciphertext snippet of a customer credential was leaking into the
external provider's request logs.
Introduce utils.DecryptStoredSecret that:
- returns "" / legacy plaintext untouched (no false positives for
pre-encryption rows);
- returns ErrEncryptedDataMissingKey when the value carries the
enc:v1: prefix but no AES key is configured;
- propagates any GCM auth-tag failure from a rotated key.
Wire it into the five Scan / AfterFind sites that currently swallow
the error:
- Tenant.AfterFind (tenants.api_key)
- CredentialsConfig.Scan (tenants.we_knora_cloud.app_secret)
- ModelParameters.Scan (models.parameters.api_key, .app_secret)
- ConnectionConfig.Scan (vector_store_connections.password, .api_key)
- WebSearchProviderParameters.Scan (web_search_providers.api_key)
The error is wrapped with the originating column so the failure is
diagnosable in the logs. Operators must restore the previous
SYSTEM_AES_KEY (or rotate the affected secrets) instead of receiving
silent vendor 401s.
Add table-driven tests covering empty input, legacy plaintext,
round-trip, missing key, wrong-length key, and rotated key. The
strict path explicitly asserts that ciphertext does NOT leak in the
returned plaintext on any error path.
This commit is contained in:
@@ -74,6 +74,70 @@ func TestDecryptAESGCM(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecryptStoredSecret(t *testing.T) {
|
||||
t.Run("returns empty string as-is", func(t *testing.T) {
|
||||
t.Setenv("SYSTEM_AES_KEY", testAESKey)
|
||||
out, err := DecryptStoredSecret("")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "", out)
|
||||
})
|
||||
|
||||
t.Run("legacy plaintext (no enc:v1: prefix) returned as-is", func(t *testing.T) {
|
||||
t.Setenv("SYSTEM_AES_KEY", testAESKey)
|
||||
out, err := DecryptStoredSecret("sk-legacy-plaintext")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "sk-legacy-plaintext", out)
|
||||
})
|
||||
|
||||
t.Run("legacy plaintext is returned even when key is unset", func(t *testing.T) {
|
||||
t.Setenv("SYSTEM_AES_KEY", "")
|
||||
out, err := DecryptStoredSecret("sk-legacy-plaintext")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "sk-legacy-plaintext", out)
|
||||
})
|
||||
|
||||
t.Run("round-trip with valid key", func(t *testing.T) {
|
||||
t.Setenv("SYSTEM_AES_KEY", testAESKey)
|
||||
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
|
||||
require.NoError(t, err)
|
||||
|
||||
out, err := DecryptStoredSecret(encrypted)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "sk-secret", out)
|
||||
})
|
||||
|
||||
t.Run("encrypted value with missing key returns ErrEncryptedDataMissingKey", func(t *testing.T) {
|
||||
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Setenv("SYSTEM_AES_KEY", "")
|
||||
out, err := DecryptStoredSecret(encrypted)
|
||||
require.ErrorIs(t, err, ErrEncryptedDataMissingKey)
|
||||
assert.Equal(t, "", out, "ciphertext must NOT leak when decryption is impossible")
|
||||
})
|
||||
|
||||
t.Run("encrypted value with wrong-length key returns ErrEncryptedDataMissingKey", func(t *testing.T) {
|
||||
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Setenv("SYSTEM_AES_KEY", "too-short")
|
||||
out, err := DecryptStoredSecret(encrypted)
|
||||
require.ErrorIs(t, err, ErrEncryptedDataMissingKey)
|
||||
assert.Equal(t, "", out)
|
||||
})
|
||||
|
||||
t.Run("encrypted value with rotated key returns auth-tag error", func(t *testing.T) {
|
||||
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Setenv("SYSTEM_AES_KEY", "abcdefghijklmnopqrstuvwxyz123456")
|
||||
out, err := DecryptStoredSecret(encrypted)
|
||||
require.Error(t, err)
|
||||
assert.NotErrorIs(t, err, ErrEncryptedDataMissingKey, "should be a real auth failure, not the missing-key sentinel")
|
||||
assert.Equal(t, "", out, "ciphertext must NOT leak when decryption fails")
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetAESKey(t *testing.T) {
|
||||
t.Run("returns key when SYSTEM_AES_KEY is 32 bytes", func(t *testing.T) {
|
||||
t.Setenv("SYSTEM_AES_KEY", testAESKey)
|
||||
|
||||
Reference in New Issue
Block a user