Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport][Base64Utils] Removed regex to validate base64 #1685

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/utils/Base64Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ constexpr char PADDING{'='};
constexpr std::string_view CHARACTERS{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/"};
constexpr std::string_view REGEX("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$");
// clang-format off
constexpr unsigned char BASE64_TABLE[] = {
255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
Expand Down Expand Up @@ -214,8 +213,36 @@ std::string UTILS::BASE64::DecodeToStr(std::string_view input)

bool UTILS::BASE64::IsValidBase64(const std::string& input)
{
std::regex base64Regex(REGEX.data());
return std::regex_match(input, base64Regex);
// Check for empty input or incorrect length
if (input.empty() || input.size() % 4 != 0)
return false;

// Use a lookup table for faster character checking
bool lookup[256]{};
for (char c : CHARACTERS)
{
lookup[static_cast<unsigned char>(c)] = true;
}

// Iterate over the input string and check each character
size_t paddingSize = 0;
for (size_t i = 0; i < input.size(); ++i)
{
if (input[i] == '=')
{
paddingSize++;
} // Check of characters after padding, and validity of characters
else if (paddingSize > 0 || !lookup[static_cast<unsigned char>(input[i])])
{
return false; // Invalid character
}
}

// Max allowed padding chars
if (paddingSize > 2)
return false;

return true;
}

bool UTILS::BASE64::AddPadding(std::string& base64str)
Expand Down