-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.72] Fix Base64 duplicate padding (#12700)
* Ensure origin in WebSocket headers (#12643) * update cgmanifest * Ensure origin in WebSocket headers * Use case-insensitive comparison * Change files * Assign SetRequestHeader mocks where needed * Fix Base64 duplicate padding (#12689) * Remove unused Boost headers from FR module source * Declare encode/decode base64 utility methods * Implement Encode methods * Update packages.lock.json * Update packages.lock.json * Use string as return value to ensure lifetime See https://learn.microsoft.com/en-us/cpp/code-quality/c26816?view=msvc-170 * Apply padding for Boost variant * Remove decode from base64 wstring variant * Cover 4*3 (plus empty) cases in unit tests * Make output padding size match to input size % 4 * clang format * Implement DecodeBase64 * Replace scattered usage of Boost base64_from_binary * Remove Boost includes from BaseFrRc * Change files * Remove EncodeBase64(wstring_view) * Add test for non-text values * Pass string_view by value * Ensure binary data lifetime in test * Remove change files * Change files * Add missing sstream includes (#12713) * Add missing sstream includes - C++17 won't infer the missing entries automatically. * Change files * Remove change files
- Loading branch information
1 parent
4db77b3
commit f1cb8a4
Showing
11 changed files
with
306 additions
and
74 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/react-native-windows-586bf782-33d5-425f-95e6-4ac2f82ef1d2.json
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Fix Base64 duplicate padding (#12689)", | ||
"packageName": "react-native-windows", | ||
"email": "julio.rocha@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
#include "Utilities.h" | ||
|
||
// Boost Library | ||
#include <boost/archive/iterators/base64_from_binary.hpp> | ||
#include <boost/archive/iterators/binary_from_base64.hpp> | ||
#include <boost/archive/iterators/ostream_iterator.hpp> | ||
#include <boost/archive/iterators/transform_width.hpp> | ||
|
||
// Windows API | ||
#include <winrt/Windows.Security.Cryptography.h> | ||
|
||
// Standard Library | ||
#include <sstream> | ||
|
||
using std::string; | ||
using std::string_view; | ||
using std::wstring_view; | ||
using winrt::array_view; | ||
|
||
using winrt::Windows::Security::Cryptography::BinaryStringEncoding; | ||
using winrt::Windows::Security::Cryptography::CryptographicBuffer; | ||
|
||
namespace Microsoft::React::Utilities { | ||
|
||
string DecodeBase64(string_view base64) noexcept { | ||
typedef array_view<char const> av_t; | ||
auto bytes = av_t(base64.data(), static_cast<av_t::size_type>(base64.size())); | ||
|
||
using namespace boost::archive::iterators; | ||
typedef transform_width<binary_from_base64<const char *>, 8, 6> decode_base64; | ||
std::ostringstream oss; | ||
std::copy(decode_base64(bytes.cbegin()), decode_base64(bytes.cend()), ostream_iterator<char>(oss)); | ||
|
||
return oss.str(); | ||
} | ||
|
||
// https://www.boost.org/doc/libs/1_76_0/libs/serialization/doc/dataflow.html | ||
string EncodeBase64(string_view text) noexcept { | ||
typedef array_view<char const> av_t; | ||
auto bytes = av_t(text.data(), static_cast<av_t::size_type>(text.size())); | ||
|
||
using namespace boost::archive::iterators; | ||
typedef base64_from_binary<transform_width<const char *, 6, 8>> encode_base64; | ||
std::ostringstream oss; | ||
std::copy(encode_base64(bytes.cbegin()), encode_base64(bytes.cend()), ostream_iterator<char>(oss)); | ||
|
||
// https://unix.stackexchange.com/questions/631501 | ||
auto padLength = (4 - (oss.tellp() % 4)) % 4; | ||
for (auto i = 0; i < padLength; ++i) { | ||
oss << '='; | ||
} | ||
|
||
return oss.str(); | ||
} | ||
|
||
} // namespace Microsoft::React::Utilities |
This file contains 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
Oops, something went wrong.