WeKnora/internal/utils
wizardchen 3b23713c54 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.
2026-04-30 15:17:56 +08:00
..
crypto.go fix(crypto): fail loudly when encrypted DB fields cannot be decrypted 2026-04-30 15:17:56 +08:00
crypto_test.go fix(crypto): fail loudly when encrypted DB fields cannot be decrypted 2026-04-30 15:17:56 +08:00
debug.go feat: 使用增强的任务ID生成器替换UUID 2026-01-14 17:09:19 +08:00
extraheaders.go feat(models): support custom HTTP headers across all remote model calls 2026-04-23 23:36:40 +08:00
extraheaders_test.go feat(models): support custom HTTP headers across all remote model calls 2026-04-23 23:36:40 +08:00
filesize.go feat: 添加可配置的文件上传大小限制 2025-12-30 14:35:10 +08:00
fileutil.go fix(storage): auto-detect content-type to resolve image rendering issues 2026-04-20 21:59:54 +08:00
httputil.go feat: enhance document loading and state management in doc-content component 2026-03-02 21:21:49 +08:00
inject.go fix: improve tenant access token logging and update test cases 2026-04-22 21:17:30 +08:00
inject_test.go fix: improve tenant access token logging and update test cases 2026-04-22 21:17:30 +08:00
json.go feat: Add DataSchema tool for retrieving schema information from CSV and Excel files 2025-12-29 20:03:51 +08:00
log_sanitize.go feat: implement image upload and multimodal support 2026-03-12 10:32:26 +08:00
presign.go fix(im): revert tenant-from-context for presigned URL 2026-04-30 11:40:30 +08:00
presign_test.go fix(im): rewrite private storage URLs to HTTP in IM channel replies 2026-04-30 11:40:30 +08:00
security.go feat: enhance URL validation and timeout handling in knowledge base components 2026-04-16 18:13:19 +08:00
security_test.go fix(security): support IPv6 in SSRF validation via whitelist mechanism 2026-03-31 20:45:15 +08:00
taskid.go feat: 使用增强的任务ID生成器替换UUID 2026-01-14 17:09:19 +08:00