fix: 加密密钥长度校验 + AI 标签同步丢失 (#184)#187
Conversation
- Add normalizeEncryptionKey() to handle non-standard ENCRYPTION_KEY formats (short hex, base64, plain text) via SHA-256 derivation, passing through valid 64-char hex keys unchanged for backward compatibility (#184) - Change PUT /api/repositories from INSERT OR REPLACE to ON CONFLICT DO UPDATE with conditional updates for AI metadata fields (ai_tags, ai_summary, ai_platforms, custom_tags, etc.) — empty values no longer overwrite existing backend data, preventing AI tags from disappearing after sync - Add unit tests for normalizeEncryptionKey (8 cases) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis PR normalizes encryption keys into deterministic 64-hex AES-256-ready strings (applied to env and file keys, with file rewrite when changed) and changes the repository bulk upsert to use ON CONFLICT(id) DO UPDATE that preserves existing nullable/empty metadata fields. ChangesEncryption Key Normalization
Repository Bulk Upsert Preservation
Sequence Diagram(s)sequenceDiagram
participant Env as process.env.ENCRYPTION_KEY
participant File as data/.encryption-key
participant Normalizer as normalizeEncryptionKey
participant FS as Filesystem
Env->>Normalizer: pass raw env value
File->>Normalizer: pass raw file contents
Normalizer->>Normalizer: trim, hex-check, or SHA-256 derive
Normalizer-->>Env: return normalized key
alt file normalized != original
Normalizer->>FS: write normalized key (mode 0o600)
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
- Change too-long hex handling from truncation to SHA-256 derivation for consistent behavior across all non-standard key formats - Write normalized key back to .encryption-key file so future startups use the correct format even without normalization code - Add edge case tests: empty string, 64-char non-hex, SHA-256 derivation for too-long hex (10 total tests, all passing) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add JSDoc comments to resolveDataDir, resolveEncryptionKey, loadConfig, parseJsonColumn, and transformRepo functions. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
问题
Closes #184
Related: #166
问题 1:AI 配置同步返回 422
用户设置
ENCRYPTION_KEY为 32 字符 hex(如openssl rand -hex 16),Buffer.from(key, hex)只解出 16 字节,AES-256 要求 32 字节 → 抛Invalid key length→ AI config 加密失败 → 422。问题 2:AI 标签同步后消失
PUT /api/repositories用INSERT OR REPLACE整行替换,前端推送空ai_tags([])会覆盖后端已有的 AI 标签,导致用户手动恢复的标签在下一次同步后凭空消失。#166 中报告的「同步后自动分类消失」也是同一根因。修复方案
Fix 1:加密密钥自动规范化 —
server/src/config.ts新增
normalizeEncryptionKey()函数,对所有来源的 key 做规范化:规范化后的 key 会回写到
.encryption-key文件,确保后续启动即使无规范化代码也能正常使用。Fix 2:AI Tags 同步丢失 —
server/src/routes/repositories.ts改为
INSERT ... ON CONFLICT(id) DO UPDATE SET ...,对 AI 相关字段做条件更新:仅当推送的值非空时才更新,空值保留后端已有数据。受影响字段:
ai_summary、ai_tags、ai_platforms、custom_description、custom_tags、custom_category、analyzed_at、last_edited。测试
server/tests/services/config.test.ts(10 个用例),覆盖所有 key 格式场景 + 边界条件Summary by CodeRabbit
Bug Fixes
Improvements
Tests