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:
+10
-11
@@ -3,6 +3,7 @@ package types
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
@@ -139,18 +140,16 @@ func (c *ModelParameters) Scan(value interface{}) error {
|
||||
if err := json.Unmarshal(b, c); err != nil {
|
||||
return err
|
||||
}
|
||||
if key := utils.GetAESKey(); key != nil {
|
||||
if c.APIKey != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(c.APIKey, key); err == nil {
|
||||
c.APIKey = decrypted
|
||||
}
|
||||
}
|
||||
if c.AppSecret != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(c.AppSecret, key); err == nil {
|
||||
c.AppSecret = decrypted
|
||||
}
|
||||
}
|
||||
apiKey, err := utils.DecryptStoredSecret(c.APIKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt model parameters api_key: %w", err)
|
||||
}
|
||||
c.APIKey = apiKey
|
||||
appSecret, err := utils.DecryptStoredSecret(c.AppSecret)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt model parameters app_secret: %w", err)
|
||||
}
|
||||
c.AppSecret = appSecret
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+13
-10
@@ -150,13 +150,16 @@ func (t *Tenant) BeforeSave(tx *gorm.DB) error {
|
||||
}
|
||||
|
||||
// AfterFind decrypts APIKey after loading from database.
|
||||
// Legacy plaintext (without enc:v1: prefix) is returned as-is.
|
||||
// Legacy plaintext (without enc:v1: prefix) is returned as-is. When the value
|
||||
// is encrypted but SYSTEM_AES_KEY is missing/rotated and the data cannot be
|
||||
// decrypted, the error is propagated so the read fails loudly instead of
|
||||
// returning ciphertext to callers.
|
||||
func (t *Tenant) AfterFind(tx *gorm.DB) error {
|
||||
if key := utils.GetAESKey(); key != nil && t.APIKey != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(t.APIKey, key); err == nil {
|
||||
t.APIKey = decrypted
|
||||
}
|
||||
decrypted, err := utils.DecryptStoredSecret(t.APIKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt tenants.api_key (id=%d): %w", t.ID, err)
|
||||
}
|
||||
t.APIKey = decrypted
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -299,12 +302,12 @@ func (c *CredentialsConfig) Scan(value interface{}) error {
|
||||
if err := json.Unmarshal(b, c); err != nil {
|
||||
return err
|
||||
}
|
||||
if c.WeKnoraCloud != nil && c.WeKnoraCloud.AppSecret != "" {
|
||||
if key := utils.GetAESKey(); key != nil {
|
||||
if decrypted, err := utils.DecryptAESGCM(c.WeKnoraCloud.AppSecret, key); err == nil {
|
||||
c.WeKnoraCloud.AppSecret = decrypted
|
||||
}
|
||||
if c.WeKnoraCloud != nil {
|
||||
decrypted, err := utils.DecryptStoredSecret(c.WeKnoraCloud.AppSecret)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt tenant credentials we_knora_cloud.app_secret: %w", err)
|
||||
}
|
||||
c.WeKnoraCloud.AppSecret = decrypted
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -151,18 +151,16 @@ func (c *ConnectionConfig) Scan(value interface{}) error {
|
||||
if err := json.Unmarshal(b, c); err != nil {
|
||||
return err
|
||||
}
|
||||
if key := utils.GetAESKey(); key != nil {
|
||||
if c.Password != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(c.Password, key); err == nil {
|
||||
c.Password = decrypted
|
||||
}
|
||||
}
|
||||
if c.APIKey != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(c.APIKey, key); err == nil {
|
||||
c.APIKey = decrypted
|
||||
}
|
||||
}
|
||||
password, err := utils.DecryptStoredSecret(c.Password)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt vector store connection password: %w", err)
|
||||
}
|
||||
c.Password = password
|
||||
apiKey, err := utils.DecryptStoredSecret(c.APIKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt vector store connection api_key: %w", err)
|
||||
}
|
||||
c.APIKey = apiKey
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package types
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/utils"
|
||||
@@ -101,11 +102,11 @@ func (p *WebSearchProviderParameters) Scan(value interface{}) error {
|
||||
if err := json.Unmarshal(b, p); err != nil {
|
||||
return err
|
||||
}
|
||||
if key := utils.GetAESKey(); key != nil && p.APIKey != "" {
|
||||
if decrypted, err := utils.DecryptAESGCM(p.APIKey, key); err == nil {
|
||||
p.APIKey = decrypted
|
||||
}
|
||||
apiKey, err := utils.DecryptStoredSecret(p.APIKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt web search provider api_key: %w", err)
|
||||
}
|
||||
p.APIKey = apiKey
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user