Zhou
Zhou MingxuanHead of R&D
Quality gate blocked
  • 1 critical finding exceeds the limit of 0

Key library master key derivation and tenant access control

hanvault-corefeat/key-derivationLu Heng · 2026-06-05 09:41
Changed files(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 reviewerAIMajor
The fixed b"vault" salt is shared across tenants and enables precomputed attacks. Generate a random salt for each record and store it with the ciphertext.
2+ # Insecure example: fixed salt and too few iterations
A
AI reviewerAIMinor
A warning comment does not make an unsafe implementation acceptable. Replace the weak implementation before merging.
23 return hashlib.pbkdf2_hmac(
3- "sha256", password.encode(), salt, 200_000
4+ "sha256", password.encode(), salt, 1_000
A
AI reviewerAIMajor
PBKDF2 iterations have been reduced from 200,000 to 1,000, significantly weakening its resistance to brute-force attacks. OWASP 2026 recommends PBKDF2-SHA256 ≥ 600,000 iterations.
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 reviewerAICritical
AES-ECB exposes patterns because identical plaintext blocks produce identical ciphertext. Encrypt secrets with AES-GCM and a unique nonce to provide confidentiality and integrity.
Suggested change
+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 review process

Review the plan

  1. Develop a review planThis change affects key encryption and access control, so route it to Opus. Verify the encryption mode, KDF strength, and tenant isolation.

Output

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

Output

2 passed — cross-tenant access and GCM integrity failures are not covered
Review conclusions

The quality gate is blocked by ECB encryption, a fixed salt, too few KDF iterations, and broken tenant isolation. Restore tenant validation, adopt AES-GCM, generate a random salt, raise the iteration count, and resubmit the review.

51
F
Quality score

Summary of issues

    Critical1
    Major3
    Minor1
    Info0
Lead reviewer modelClaude Opus 4.7
Code coverage68%
Review cost¥0.051