Skip to content

Commit 4c41606

Browse files
authored
Merge pull request #1170 from mikael-s-persson/fix/secure_string
Replaced the secureStringClear mechanism with a SecureString class
2 parents 96cd609 + ef3ce37 commit 4c41606

19 files changed

+136
-158
lines changed

cpr/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ add_library(cpr
44
accept_encoding.cpp
55
async.cpp
66
auth.cpp
7-
bearer.cpp
87
callback.cpp
98
cert_info.cpp
109
cookies.cpp

cpr/auth.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "cpr/auth.h"
2-
#include "cpr/util.h"
32

43
#include <string_view>
54

@@ -12,10 +11,6 @@ Authentication::Authentication(std::string_view username, std::string_view passw
1211
auth_string_ += password;
1312
}
1413

15-
Authentication::~Authentication() noexcept {
16-
util::secureStringClear(auth_string_);
17-
}
18-
1914
const char* Authentication::GetAuthString() const noexcept {
2015
return auth_string_.c_str();
2116
}

cpr/bearer.cpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

cpr/cookies.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <iomanip>
66
#include <sstream>
77
#include <string>
8+
#include <string_view>
89

910
namespace cpr {
1011
const std::string Cookie::GetDomain() const {
@@ -53,15 +54,15 @@ const std::string Cookies::GetEncoded(const CurlHolder& holder) const {
5354
std::stringstream stream;
5455
for (const cpr::Cookie& item : cookies_) {
5556
// Depending on if encoding is set to "true", we will URL-encode cookies
56-
stream << (encode ? holder.urlEncode(item.GetName()) : item.GetName()) << "=";
57+
stream << (encode ? std::string_view{holder.urlEncode(item.GetName())} : std::string_view{item.GetName()}) << "=";
5758

5859
// special case version 1 cookies, which can be distinguished by
5960
// beginning and trailing quotes
6061
if (!item.GetValue().empty() && item.GetValue().front() == '"' && item.GetValue().back() == '"') {
6162
stream << item.GetValue();
6263
} else {
6364
// Depending on if encoding is set to "true", we will URL-encode cookies
64-
stream << (encode ? holder.urlEncode(item.GetValue()) : item.GetValue());
65+
stream << (encode ? std::string_view{holder.urlEncode(item.GetValue())} : std::string_view{item.GetValue()});
6566
}
6667
stream << "; ";
6768
}

cpr/curl_container.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ const std::string CurlContainer<Parameter>::GetContent(const CurlHolder& holder)
2727
content += "&";
2828
}
2929

30-
const std::string escapedKey = encode ? holder.urlEncode(parameter.key) : parameter.key;
30+
const std::string escapedKey = encode ? std::string{holder.urlEncode(parameter.key)} : parameter.key;
3131
if (parameter.value.empty()) {
3232
content += escapedKey;
3333
} else {
34-
const std::string escapedValue = encode ? holder.urlEncode(parameter.value) : parameter.value;
34+
const std::string escapedValue = encode ? std::string{holder.urlEncode(parameter.value)} : parameter.value;
3535
content += escapedKey + "=";
3636
content += escapedValue;
3737
}
@@ -47,7 +47,7 @@ const std::string CurlContainer<Pair>::GetContent(const CurlHolder& holder) cons
4747
if (!content.empty()) {
4848
content += "&";
4949
}
50-
const std::string escaped = encode ? holder.urlEncode(element.value) : element.value;
50+
const std::string escaped = encode ? std::string{holder.urlEncode(element.value)} : element.value;
5151
content += element.key + "=" + escaped;
5252
}
5353

cpr/curlholder.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include <cassert>
33
#include <curl/curl.h>
44
#include <curl/easy.h>
5-
#include <string>
5+
#include <string_view>
6+
#include "cpr/secure_string.h"
67

78
namespace cpr {
89
CurlHolder::CurlHolder() {
@@ -28,22 +29,22 @@ CurlHolder::~CurlHolder() {
2829
curl_easy_cleanup(handle);
2930
}
3031

31-
std::string CurlHolder::urlEncode(const std::string& s) const {
32+
util::SecureString CurlHolder::urlEncode(std::string_view s) const {
3233
assert(handle);
33-
char* output = curl_easy_escape(handle, s.c_str(), static_cast<int>(s.length()));
34+
char* output = curl_easy_escape(handle, s.data(), static_cast<int>(s.length()));
3435
if (output) {
35-
std::string result = output;
36+
util::SecureString result = output;
3637
curl_free(output);
3738
return result;
3839
}
3940
return "";
4041
}
4142

42-
std::string CurlHolder::urlDecode(const std::string& s) const {
43+
util::SecureString CurlHolder::urlDecode(std::string_view s) const {
4344
assert(handle);
44-
char* output = curl_easy_unescape(handle, s.c_str(), static_cast<int>(s.length()), nullptr);
45+
char* output = curl_easy_unescape(handle, s.data(), static_cast<int>(s.length()), nullptr);
4546
if (output) {
46-
std::string result = output;
47+
util::SecureString result = output;
4748
curl_free(output);
4849
return result;
4950
}

cpr/file.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
namespace cpr {
77

8-
Files::Files(const std::initializer_list<std::string>& p_filepaths) {
9-
for (const std::string& filepath : p_filepaths) {
10-
files.emplace_back(filepath);
11-
}
12-
}
8+
Files::Files(const std::initializer_list<std::string>& p_filepaths) : files(p_filepaths.begin(), p_filepaths.end()) {}
139

1410
Files::iterator Files::begin() {
1511
return files.begin();

cpr/proxyauth.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,25 @@
33
#include <string>
44

55
namespace cpr {
6-
EncodedAuthentication::~EncodedAuthentication() noexcept {
7-
util::secureStringClear(username);
8-
util::secureStringClear(password);
9-
}
106

11-
const std::string& EncodedAuthentication::GetUsername() const {
7+
std::string_view EncodedAuthentication::GetUsername() const {
128
return username;
139
}
1410

15-
const std::string& EncodedAuthentication::GetPassword() const {
11+
std::string_view EncodedAuthentication::GetPassword() const {
1612
return password;
1713
}
1814

1915
bool ProxyAuthentication::has(const std::string& protocol) const {
2016
return proxyAuth_.count(protocol) > 0;
2117
}
2218

23-
const char* ProxyAuthentication::GetUsername(const std::string& protocol) {
24-
return proxyAuth_[protocol].username.c_str();
19+
std::string_view ProxyAuthentication::GetUsername(const std::string& protocol) {
20+
return proxyAuth_[protocol].GetUsername();
2521
}
2622

27-
const char* ProxyAuthentication::GetPassword(const std::string& protocol) {
28-
return proxyAuth_[protocol].password.c_str();
23+
std::string_view ProxyAuthentication::GetPassword(const std::string& protocol) {
24+
return proxyAuth_[protocol].GetPassword();
2925
}
3026

3127
} // namespace cpr

cpr/session.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ const std::optional<Response> Session::intercept() {
903903
if (current_interceptor_ == interceptors_.end()) {
904904
current_interceptor_ = first_interceptor_;
905905
} else {
906-
current_interceptor_++;
906+
++current_interceptor_;
907907
}
908908

909909
if (current_interceptor_ != interceptors_.end()) {

cpr/util.cpp

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <curl/curl.h>
1212
#include <fstream>
1313
#include <ios>
14+
#include <iterator>
1415
#include <sstream>
1516
#include <string>
1617
#include <type_traits>
@@ -166,7 +167,7 @@ int debugUserFunction(CURL* /*handle*/, curl_infotype type, char* data, size_t s
166167
* std::string input = "Hello World!";
167168
* std::string result = holder.urlEncode(input);
168169
**/
169-
std::string urlEncode(const std::string& s) {
170+
util::SecureString urlEncode(std::string_view s) {
170171
const CurlHolder holder; // Create a temporary new holder for URL encoding
171172
return holder.urlEncode(s);
172173
}
@@ -181,55 +182,16 @@ std::string urlEncode(const std::string& s) {
181182
* std::string input = "Hello%20World%21";
182183
* std::string result = holder.urlDecode(input);
183184
**/
184-
std::string urlDecode(const std::string& s) {
185+
util::SecureString urlDecode(std::string_view s) {
185186
const CurlHolder holder; // Create a temporary new holder for URL decoding
186187
return holder.urlDecode(s);
187188
}
188189

189-
#if defined(__STDC_LIB_EXT1__)
190-
void secureStringClear(std::string& s) {
191-
if (s.empty()) {
192-
return;
193-
}
194-
memset_s(&s.front(), s.length(), 0, s.length());
195-
s.clear();
196-
}
197-
#elif defined(_WIN32)
198-
void secureStringClear(std::string& s) {
199-
if (s.empty()) {
200-
return;
201-
}
202-
SecureZeroMemory(&s.front(), s.length());
203-
s.clear();
204-
}
205-
#else
206-
#if defined(__clang__)
207-
#pragma clang optimize off // clang
208-
#elif defined(__GNUC__) || defined(__MINGW32__) || defined(__MINGW32__) || defined(__MINGW64__)
209-
#pragma GCC push_options // g++
210-
#pragma GCC optimize("O0") // g++
211-
#endif
212-
void secureStringClear(std::string& s) {
213-
if (s.empty()) {
214-
return;
215-
}
216-
// NOLINTNEXTLINE (readability-container-data-pointer)
217-
char* ptr = &(s[0]);
218-
memset(ptr, '\0', s.length());
219-
s.clear();
220-
}
221-
222-
#if defined(__clang__)
223-
#pragma clang optimize on // clang
224-
#elif defined(__GNUC__) || defined(__MINGW32__) || defined(__MINGW32__) || defined(__MINGW64__)
225-
#pragma GCC pop_options // g++
226-
#endif
227-
#endif
228-
229190
bool isTrue(const std::string& s) {
230-
std::string temp_string{s};
231-
std::transform(temp_string.begin(), temp_string.end(), temp_string.begin(), [](unsigned char c) { return static_cast<unsigned char>(std::tolower(c)); });
232-
return temp_string == "true";
191+
constexpr std::string_view tmp = "true";
192+
auto [s_it, tmp_it] = std::mismatch(s.begin(), s.end(), tmp.begin(), tmp.end(),
193+
[](auto s_c, auto t_c) { return std::tolower(s_c) == t_c; });
194+
return s_it == s.end() && tmp_it == tmp.end();
233195
}
234196

235197
time_t sTimestampToT(const std::string& st) {

0 commit comments

Comments
 (0)