Skip to content

Commit d7f199a

Browse files
trasnake87claude
andcommitted
Redact query parameter values in HTTP error messages
Add a static redact_url_query() helper that replaces query parameter values with [redacted] while preserving parameter names and URL structure. Update both synchronous and asynchronous perform() error messages to use it, preventing sensitive tokens or credentials from leaking into error output. Closes minecraft-linux#3 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cf7cd80 commit d7f199a

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

lib/playapi/util/http.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@
99

1010
using namespace playapi;
1111

12+
static std::string redact_url_query(const std::string& url) {
13+
auto pos = url.find('?');
14+
if (pos == std::string::npos) return url;
15+
std::string base = url.substr(0, pos + 1); // include '?'
16+
std::string query = url.substr(pos + 1);
17+
std::string out;
18+
size_t i = 0;
19+
while (i < query.size()) {
20+
size_t amp = query.find('&', i);
21+
std::string part = (amp == std::string::npos) ? query.substr(i) : query.substr(i, amp - i);
22+
size_t eq = part.find('=');
23+
if (eq == std::string::npos) {
24+
// key without value
25+
out += part;
26+
} else {
27+
// keep key and '=' then redact value
28+
out += part.substr(0, eq + 1);
29+
out += "[redacted]";
30+
}
31+
if (amp == std::string::npos) break;
32+
out += '&';
33+
i = amp + 1;
34+
}
35+
return base + out;
36+
}
37+
1238
void url_encoded_entity::add_pair(const std::string& key, const std::string& val) {
1339
pairs.push_back({key, val});
1440
}
@@ -179,7 +205,7 @@ http_response http_request::perform() {
179205
return http_response(curlerr, status, output.str());
180206
} else {
181207
std::stringstream errormsg;
182-
errormsg << "Failed to perform http request to " << url << " : CURLcode " << curlerr << " Details: " << errbuf;
208+
errormsg << "Failed to perform http request to " << redact_url_query(url) << " : CURLcode " << curlerr << " Details: " << errbuf;
183209
curl_easy_cleanup(curl);
184210
throw std::runtime_error(errormsg.str().data());
185211
}
@@ -211,7 +237,7 @@ void http_request::perform(std::function<void(http_response)> success, std::func
211237
success(http_response(curlerr, status, output.str()));
212238
} else {
213239
std::stringstream errormsg;
214-
errormsg << "Failed to perform http request to " << req->url << " : CURLcode " << curlerr << " Details: " << errbuf;
240+
errormsg << "Failed to perform http request to " << redact_url_query(req->url) << " : CURLcode " << curlerr << " Details: " << errbuf;
215241
try {
216242
throw std::runtime_error(errormsg.str().data());
217243
} catch (...) {

0 commit comments

Comments
 (0)