Repository navigation
Fix a couple of problems with crypto utilities - #18897
Conversation
- changed warning to a debug since the api might fail without leaving an error - fixed use of unsigned arithmetic when signed values are required
gtritchie
left a comment
There was a problem hiding this comment.
No blocking issues. 1 follow-up, 1 nit
🔴 must be resolved before merge. 🟠 is real but not merge-blocking — either address it here or open an issue to do so later. 🟡 is optional.
- 🟠
src/cpp/shared_core/system/encryption/EncryptionVersion.cpp:418— v2 ciphertext of an empty plaintext no longer decrypts - 🟡
src/cpp/shared_core/system/encryption/EncryptionVersion.cpp:54— comment describes a path that no longer reaches this branch
| auto macIndex = ENCRYPTION_VERSION_SIZE_BYTES + dataLength; | ||
| // avoid using unsigned comparisons here | ||
| const size_t v2_OVERHEAD_BYTES = ENCRYPTION_VERSION_SIZE_BYTES + MAC_SIZE_BYTES; | ||
| if (in_v2_data.size() <= v2_OVERHEAD_BYTES) |
There was a problem hiding this comment.
🟠 Encrypting an empty plaintext with v2 gives exactly 17 bytes ([0x02][tag]), which this guard now rejects, so that round-trip fails.
Use < here, and drop overhead from the rejected lengths in the new test (EncryptionVersionTests.cpp:347).
Why
v2::aesEncrypt on empty data: GCM emits 0 ciphertext bytes plus a 16-byte tag, so the output is 1 + MAC_SIZE_BYTES = 17 bytes. With <=, v2::aesDecrypt throws EncryptionVersionMismatchException. crypto::aesDecrypt then falls back to v0 (AES-128-CBC) on 17 bytes. That isn't a block multiple, so it errors. Before this PR dataLength came out to 0, GCM checked the tag, and decryption returned an empty vector.
A zero-length payload is still well-formed. Only a buffer shorter than the overhead needs rejecting. I found no caller in this repo that encrypts an empty value, which is why this isn't marked blocking, but encryptAndBase64Encode is public API and downstream callers may pass empty strings (a blank password, for example).
There was a problem hiding this comment.
Fixed in 4b51ea4 — the guard is now <, and the new round-trip test covers the empty-plaintext case.
| if (ec == 0) | ||
| { | ||
| log::logWarningMessage("getLastCryptoError called with no pending error"); | ||
| // Some OpenSSL operations report failure only by return value - not adding an error |
There was a problem hiding this comment.
🟡 This comment says version detection leads here, but after this PR neither fallback path hits the empty-queue branch.
Consider rewording the comment to describe what still reaches this branch, or keep it at warning level now that the noisy caller is gone.
Why
The v2 tag mismatch now returns its own bad_message error without calling getLastCryptoError. A v1/v0 bad-padding failure in EVP_CipherFinal_ex queues an OpenSSL error (bad decrypt), so it takes the ec != 0 path. Anything left that hits ec == 0 is an unexpected OpenSSL failure, and at debug level it won't show up in logs.
There was a problem hiding this comment.
Fixed in 4b51ea4 — the reworded comment matches the code now. Thanks!
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
gtritchie
left a comment
There was a problem hiding this comment.
Both findings addressed — LGTM.
|
snyk-code-scan failure: existing findings, not from this PR's changes The Snyk Code stage scans every changed file in full ( These are the The cipher can't be changed here. Recommended action: mark the two findings as ignored in the Snyk UI ("legacy backward-compatible encryption; v2 uses AES-256-GCM"). Without that, every future PR that touches this file will fail the same way. The check isn't required, so this PR can merge in the meantime. |
Intent
Fixes #18896
Approach
Turn warning into debug - fixed unsigned problem.