test: handle non-deterministic AES-256-CBC decryption in crypto test #26383
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes flaky unit test failures that started appearing after the Vitest 4.0 upgrade (PR #26351). The
symmetricDecrypttest was expecting decryption with a wrong key to always throw an error, but AES-256-CBC doesn't guarantee this behavior.Root cause: AES-256-CBC is not authenticated encryption. When decrypting with a wrong key, whether it throws depends on whether the decrypted bytes happen to have valid PKCS#7 padding. Sometimes the random garbage bytes coincidentally look like valid padding, so
decipher.final()succeeds without throwing.The fix: Updated the test to accept either behavior - the decryption either throws OR returns a value different from the original plaintext. Both outcomes correctly indicate that wrong-key decryption doesn't produce the original data.
Note: The "Closing rpc while fetch was pending" error seen in CI was a secondary symptom - when this test failed, it caused Vitest workers to tear down while other modules were still loading.
Requested by: [email protected] (@keithwillcode)
Link to Devin run: https://app.devin.ai/sessions/5c48f4259a5f439a8aec25fe84d2baba
Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
TZ=UTC yarn test packages/lib/crypto.test.ts- all tests should passTZ=UTC yarn testChecklist
Human Review Checklist
threwError || decryptedWithWrongKey !== testTextcorrectly handles both cases