Guard to_base64 against empty input (UB on back())#39
Open
achamayou wants to merge 2 commits into
Open
Conversation
For an empty byte vector, r is an empty string and 'while (r.back() == "=")' calls back() on it, which is undefined behaviour. Return early for empty input and also make the padding-trim loop check for emptiness defensively. Currently unreachable (to_base64 is only called on fixed-size hashes and non-empty key coordinates), so this is a hardening fix.
Contributor
There was a problem hiding this comment.
Pull request overview
Hardens the internal to_base64 helper against undefined behavior when called with an empty input vector, preventing std::string::back() from being invoked on an empty string during padding trimming.
Changes:
- Return early from
to_base64when the input byte vector is empty. - Defensively guard the padding-trim loop with
!r.empty().
Comments suppressed due to low confidence (1)
didx509cpp.h:110
EVP_EncodeBlocktakes anintlength, but this code passesbytes.size()(size_t) and also stores the computed output size in anintwithout bounds checks. For sufficiently large inputs this can overflow/narrow and lead to incorrect sizing and undefined behavior. Consider explicitly checking the input/output sizes fit inintand casting intentionally before calling OpenSSL.
const int r_sz = 4 * ((bytes.size() + 2) / 3);
std::string r(r_sz, 0);
auto out_sz =
EVP_EncodeBlock((unsigned char*)r.data(), bytes.data(), bytes.size());
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
to_base64trims trailing=padding with:For an empty input vector,
r_szis0,ris an empty string, andr.back()on an emptystd::stringis undefined behaviour.Fix
EVP_EncodeBlockproduces nothing anyway).!r.empty().Currently unreachable —
to_base64is only ever called on fixed-size hashes and non-empty EC/RSA key components — so this is a hardening fix.Testing
cmake --build build— clean.ctest— all tests pass, including a new regression test coveringto_base64({})andto_base64url({}).clang-tidy ../didx509cpp.h— exit 0, no findings (mirrors CI).Found during a security review of the codebase.