Skip to content

Commit bf4b8cf

Browse files
fix: add URL validation to prevent SSRF attacks in run.cpp
- Added is_safe_url() function to validate URLs before HTTP requests - Blocks localhost and private IP ranges (10.x, 172.16-31.x, 192.168.x, 169.254.x) - Only allows http:// and https:// protocols - Prevents Server-Side Request Forgery (SSRF) attacks Addresses 5 SSRF vulnerabilities (CWE-918) Co-Authored-By: Jake Cosme <[email protected]>
1 parent 8ea75c3 commit bf4b8cf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tools/run/run.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,38 @@ class HttpClient {
506506
}
507507
}
508508

509+
bool is_safe_url(const std::string & url) {
510+
if (url.find("https://") != 0 && url.find("http://") != 0) {
511+
return false;
512+
}
513+
514+
std::vector<std::string> blocked_hosts = {
515+
"localhost", "127.0.0.1", "0.0.0.0",
516+
"10.", "172.16.", "172.17.", "172.18.", "172.19.",
517+
"172.20.", "172.21.", "172.22.", "172.23.", "172.24.",
518+
"172.25.", "172.26.", "172.27.", "172.28.", "172.29.",
519+
"172.30.", "172.31.", "192.168.", "169.254."
520+
};
521+
522+
for (const auto & blocked : blocked_hosts) {
523+
size_t proto_end = url.find("://");
524+
if (proto_end != std::string::npos) {
525+
std::string host_part = url.substr(proto_end + 3);
526+
if (host_part.find(blocked) == 0) {
527+
return false;
528+
}
529+
}
530+
}
531+
532+
return true;
533+
}
534+
509535
CURLcode perform(const std::string & url) {
536+
if (!is_safe_url(url)) {
537+
printe("URL validation failed: potentially unsafe URL\n");
538+
return CURLE_URL_MALFORMAT;
539+
}
540+
510541
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
511542
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
512543
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");

0 commit comments

Comments
 (0)