Skip to content

Commit 858ef61

Browse files
Protect webserver::bans and webserver::allowances
1 parent 8b97ee9 commit 858ef61

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/httpserver/webserver.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class webserver {
179179
std::map<details::http_endpoint, http_resource*> registered_resources;
180180
std::map<std::string, http_resource*> registered_resources_str;
181181

182+
std::shared_mutex bans_and_allowances_mutex;
182183
std::set<http::ip_representation> bans;
183184
std::set<http::ip_representation> allowances;
184185

src/webserver.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ void webserver::unregister_resource(const string& resource) {
370370
}
371371

372372
void webserver::ban_ip(const string& ip) {
373+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
373374
ip_representation t_ip(ip);
374375
set<ip_representation>::iterator it = bans.find(t_ip);
375376
if (it != bans.end() && (t_ip.weight() < (*it).weight())) {
@@ -381,6 +382,7 @@ void webserver::ban_ip(const string& ip) {
381382
}
382383

383384
void webserver::allow_ip(const string& ip) {
385+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
384386
ip_representation t_ip(ip);
385387
set<ip_representation>::iterator it = allowances.find(t_ip);
386388
if (it != allowances.end() && (t_ip.weight() < (*it).weight())) {
@@ -392,10 +394,12 @@ void webserver::allow_ip(const string& ip) {
392394
}
393395

394396
void webserver::unban_ip(const string& ip) {
397+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
395398
bans.erase(ip_representation(ip));
396399
}
397400

398401
void webserver::disallow_ip(const string& ip) {
402+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
399403
allowances.erase(ip_representation(ip));
400404
}
401405

@@ -405,6 +409,7 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add
405409

406410
if (!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES;
407411

412+
std::shared_lock bans_and_allowances_lock((static_cast<webserver*>(cls))->bans_and_allowances_mutex);
408413
if ((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) &&
409414
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr))) &&
410415
(!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr)))) ||

0 commit comments

Comments
 (0)