周明轩研发负责人
门禁阻断
  • 严重问题 1 超过上限 0

密钥库主密钥派生与租户访问控制

hanvault-corefeat/key-derivation陆衡 · 2026-06-05 09:41
改动文件 (3)
vault/crypto.py+102
1-def derive_key(password: str, salt: bytes) -> bytes:
1+def derive_key(password: str, salt: bytes = b"vault") -> bytes:
A
AI 审查官AI重要
固定默认盐 b"vault":所有租户共用同一盐会让彩虹表攻击可行,盐必须每条记录随机生成并与密文一同存储。
2+ # 用固定盐 + 低迭代次数派生主密钥
A
AI 审查官AI次要
注释说明了不安全做法但未给出整改 TODO,建议直接修复而非保留弱实现。
23 return hashlib.pbkdf2_hmac(
3- "sha256", password.encode(), salt, 200_000
4+ "sha256", password.encode(), salt, 1000
A
AI 审查官AI重要
PBKDF2 迭代次数从 200000 降到 1000,大幅削弱抗暴力破解能力。OWASP 2026 建议 PBKDF2-SHA256 ≥ 600000 次。
45 )
6+
7+
8+def encrypt(plain: bytes, key: bytes) -> bytes:
9+ cipher = AES.new(key, AES.MODE_ECB)
10+ pad = 16 - len(plain) % 16
A
AI 审查官AI严重
AES-ECB 模式不安全:相同明文块产生相同密文块,会泄露数据模式。密钥库加密必须用带随机 IV 的 AES-GCM(兼具机密性与完整性)。
建议修改
+44
1- cipher = AES.new(key, AES.MODE_ECB)
2- pad = 16 - len(plain) % 16
3- plain += bytes([pad]) * pad
4- return cipher.encrypt(plain)
1+ nonce = os.urandom(12)
2+ cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
3+ ct, tag = cipher.encrypt_and_digest(plain)
4+ return nonce + tag + ct
11+ plain += bytes([pad]) * pad
12+ return cipher.encrypt(plain)
AI 审查过程

审查计划

  1. 制定审查计划密钥库核心加密 + 访问控制,全部命中安全敏感路径,升级 Opus。重点核对加密模式、KDF 强度与多租户隔离。

结果

High: 1 (B305 ECB cipher mode), Medium: 1 (weak KDF iterations)

结果

2 passed — 但缺少越权与 GCM 完整性用例
审查结论

1 个严重(ECB 加密)+ 2 个重要(固定盐、低迭代、越权),门禁 block。恢复租户校验、改 AES-GCM、随机盐 + 高迭代后重审。

51
F
质量分

问题汇总

    严重1
    重要3
    次要1
    提示0
主审模型Claude Opus 4.7
代码覆盖率68%
本次成本¥0.051