feat(security): simplify decrypt error handling #51301
Draft
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.
Summary
Before this patch, when decrypting a value without using a password, it would call
decryptWithoutSecret
with the systemsecret
aspassword
. When this fails, it would retry with an empty string aspassword
.This has the practical disadvantage that it can lead to confusing error messages. For example, when using the TOTP app, when the system
secret
is misconfigured, the first invocation will throw a sensibleHMAC does not match.
error, but then it is retried and the retry throws aHash_hkdf(): Argument #2 ($key) cannot be empty
error causing confusion (e.g.https://help.nextcloud.com/t/hash-hkdf-argument-2-key-cannot-be-empty/192556).
Of course this fallback to using an empty string is likely part of some sort of graceful migration from the days when the secret could be empty (e.g. #34012, #31499, /cc @juliusknorr ).
However, taking a wider perspective, such 'fallback logic' in security-critical areas makes things more complex, which is a risk. It's not quite the same scenario, but Heartbleed does come to mind.
For this reason, rather than a 'surgical' improvement for the particular case encountered above (increasing complexity further), I think it'd be worth it to start considering removing this fallback entirely (perhaps in the next major version?) - hence this conversation-starter PR.
I'll keep this in draft for now because it needs a unit test, but otherwise I'd love to start the review/conversation.
TODO
Checklist