Skip to content

Commit 81067ef

Browse files
fchevassu-antidotetr
authored andcommitted
Protect webserver::bans and webserver::allowances
1 parent 69bbead commit 81067ef

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/httpserver/webserver.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ class webserver {
184184
std::map<details::http_endpoint, http_resource*> registered_resources;
185185
std::map<std::string, http_resource*> registered_resources_str;
186186

187+
std::shared_mutex bans_and_allowances_mutex;
187188
std::set<http::ip_representation> bans;
188189
std::set<http::ip_representation> allowances;
189190

src/webserver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ void webserver::unregister_resource(const string& resource) {
379379
}
380380

381381
void webserver::ban_ip(const string& ip) {
382+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
382383
ip_representation t_ip(ip);
383384
set<ip_representation>::iterator it = bans.find(t_ip);
384385
if (it != bans.end() && (t_ip.weight() < (*it).weight())) {
@@ -390,6 +391,7 @@ void webserver::ban_ip(const string& ip) {
390391
}
391392

392393
void webserver::allow_ip(const string& ip) {
394+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
393395
ip_representation t_ip(ip);
394396
set<ip_representation>::iterator it = allowances.find(t_ip);
395397
if (it != allowances.end() && (t_ip.weight() < (*it).weight())) {
@@ -401,10 +403,12 @@ void webserver::allow_ip(const string& ip) {
401403
}
402404

403405
void webserver::unban_ip(const string& ip) {
406+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
404407
bans.erase(ip_representation(ip));
405408
}
406409

407410
void webserver::disallow_ip(const string& ip) {
411+
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
408412
allowances.erase(ip_representation(ip));
409413
}
410414

@@ -451,6 +455,7 @@ MHD_Result policy_callback(void *cls, const struct sockaddr* addr, socklen_t add
451455

452456
if (!(static_cast<webserver*>(cls))->ban_system_enabled) return MHD_YES;
453457

458+
std::shared_lock bans_and_allowances_lock((static_cast<webserver*>(cls))->bans_and_allowances_mutex);
454459
if ((((static_cast<webserver*>(cls))->default_policy == http_utils::ACCEPT) &&
455460
((static_cast<webserver*>(cls))->bans.count(ip_representation(addr))) &&
456461
(!(static_cast<webserver*>(cls))->allowances.count(ip_representation(addr)))) ||

0 commit comments

Comments
 (0)