Skip to content

fix(security): don't propagate ValueError from Crypto::decrypt() fallback#60735

Open
miaulalala wants to merge 1 commit into
masterfrom
fix/noid/crypto-decrypt-fallback-valueerror
Open

fix(security): don't propagate ValueError from Crypto::decrypt() fallback#60735
miaulalala wants to merge 1 commit into
masterfrom
fix/noid/crypto-decrypt-fallback-valueerror

Conversation

@miaulalala
Copy link
Copy Markdown
Contributor

Summary

  • When decrypting a v3 ciphertext with a mismatched instance secret, Crypto::decrypt() first tries with the current secret (fails with HMAC mismatch), then falls back to an empty string
  • For v3 ciphertexts, the empty-string fallback calls hash_hkdf('sha512', ''), which throws a ValueError
  • ValueError extends \Error, not \Exception, so it bypassed the catch (Exception $e) block and propagated as an unhandled error, crashing the entire request
  • Fix: wrap the fallback decryptWithoutSecret() call in its own catch (\Throwable) and rethrow the original HMAC exception

Test plan

  • NOCOVERAGE=1 ./autotest.sh sqlite tests/lib/Security/CryptoTest.php
  • New test testDecryptWithWrongSecretThrowsHmacExceptionNotValueError covers the regression: encrypts with secret-A, decrypts with secret-B, asserts a proper Exception('HMAC does not match.') is thrown rather than a ValueError

🤖 Generated with Claude Code

@miaulalala miaulalala requested a review from a team as a code owner May 26, 2026 14:09
@miaulalala miaulalala requested review from Altahrim, icewind1991, provokateurin and salmart-dev and removed request for a team May 26, 2026 14:09
@miaulalala miaulalala force-pushed the fix/noid/crypto-decrypt-fallback-valueerror branch from f281f96 to e930c63 Compare May 26, 2026 14:12
…back

When decrypting a v3 ciphertext with a mismatched secret, the first
attempt throws an Exception (HMAC mismatch). The fallback then calls
decryptWithoutSecret() with an empty string, which causes hash_hkdf()
to throw a ValueError. Since ValueError extends \Error rather than
\Exception, it bypassed the catch block and propagated as an unhandled
error, crashing the whole request.

Wrap the fallback in its own try/catch(\Throwable) and rethrow the
original Exception so callers get a meaningful HMAC mismatch error.

Signed-off-by: Anna Larch <anna@nextcloud.com>
AI-Assisted-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@miaulalala miaulalala force-pushed the fix/noid/crypto-decrypt-fallback-valueerror branch from e930c63 to 29f43d8 Compare May 26, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant