From 3b23713c5423deff6a233755958e6660dffb56c8 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 30 Apr 2026 12:22:34 +0800 Subject: [PATCH] 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. --- internal/types/model.go | 21 +++++---- internal/types/tenant.go | 23 +++++----- internal/types/vectorstore.go | 20 ++++----- internal/types/web_search_provider.go | 9 ++-- internal/utils/crypto.go | 40 +++++++++++++++++ internal/utils/crypto_test.go | 64 +++++++++++++++++++++++++++ 6 files changed, 141 insertions(+), 36 deletions(-) diff --git a/internal/types/model.go b/internal/types/model.go index c5f26d4a..30748551 100644 --- a/internal/types/model.go +++ b/internal/types/model.go @@ -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 } diff --git a/internal/types/tenant.go b/internal/types/tenant.go index 489042bd..51172dd0 100644 --- a/internal/types/tenant.go +++ b/internal/types/tenant.go @@ -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 } diff --git a/internal/types/vectorstore.go b/internal/types/vectorstore.go index f8f76bc6..83662463 100644 --- a/internal/types/vectorstore.go +++ b/internal/types/vectorstore.go @@ -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 } diff --git a/internal/types/web_search_provider.go b/internal/types/web_search_provider.go index 58c14928..4a60a3d1 100644 --- a/internal/types/web_search_provider.go +++ b/internal/types/web_search_provider.go @@ -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 } diff --git a/internal/utils/crypto.go b/internal/utils/crypto.go index 75991099..b1d911f7 100644 --- a/internal/utils/crypto.go +++ b/internal/utils/crypto.go @@ -53,6 +53,13 @@ func EncryptAESGCM(plaintext string, key []byte) (string, error) { return EncPrefix + base64.RawURLEncoding.EncodeToString(combined), nil } +// ErrEncryptedDataMissingKey is returned by DecryptStoredSecret when the value +// carries the enc:v1: prefix but no AES key is available to decrypt it. This +// signals an operator misconfiguration (typically a rotated or unset +// SYSTEM_AES_KEY) and must propagate so the system fails loudly instead of +// silently using ciphertext as a credential. +var ErrEncryptedDataMissingKey = errors.New("encrypted data found but SYSTEM_AES_KEY is not set or has wrong length") + // DecryptAESGCM decrypts an AES-256-GCM encrypted string. // If the string lacks the enc:v1: prefix, it's treated as legacy plaintext and returned as-is. func DecryptAESGCM(encrypted string, key []byte) (string, error) { @@ -87,3 +94,36 @@ func DecryptAESGCM(encrypted string, key []byte) (string, error) { } return string(plaintext), nil } + +// DecryptStoredSecret decrypts a value loaded from the database with strict +// error propagation. Use this from GORM Scan/AfterFind hooks for any field that +// stores AES-encrypted secrets (API keys, passwords, app secrets). +// +// Behaviour: +// - empty input -> empty output, no error. +// - no enc:v1: prefix -> returned as-is (legacy plaintext column), no error. +// - has enc:v1: prefix and SYSTEM_AES_KEY is missing or wrong length -> +// returns ErrEncryptedDataMissingKey. Callers MUST propagate this so the +// load fails loudly instead of silently surfacing ciphertext as a credential. +// - has enc:v1: prefix and key is set -> decrypts, returns any decryption +// error verbatim (e.g. base64 decode failure, GCM auth tag mismatch from a +// rotated key). +// +// The previous lenient pattern (`if decrypted, err := ...; err == nil { ... }`) +// hid the rotated-key case and caused the encrypted ciphertext to be sent +// upstream as the actual API key, surfacing as 401/403 from third-party +// vendors. Always prefer this helper over calling DecryptAESGCM directly when +// loading from the database. +func DecryptStoredSecret(encrypted string) (string, error) { + if encrypted == "" { + return "", nil + } + if !strings.HasPrefix(encrypted, EncPrefix) { + return encrypted, nil + } + key := GetAESKey() + if key == nil { + return "", ErrEncryptedDataMissingKey + } + return DecryptAESGCM(encrypted, key) +} diff --git a/internal/utils/crypto_test.go b/internal/utils/crypto_test.go index 3ca170bb..4cc9c1f9 100644 --- a/internal/utils/crypto_test.go +++ b/internal/utils/crypto_test.go @@ -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)