Skip to content

Commit

Permalink
Protect webserver::bans and webserver::allowances
Browse files Browse the repository at this point in the history
  • Loading branch information
fchevassu-antidot committed Jul 25, 2023
1 parent d4a89d8 commit 2f7b4ff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/httpserver/webserver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class webserver {
std::map<details::http_endpoint, http_resource*> registered_resources;
std::map<std::string, http_resource*> registered_resources_str;

std::shared_mutex bans_and_allowances_mutex;
std::set<http::ip_representation> bans;
std::set<http::ip_representation> allowances;

Expand Down
5 changes: 5 additions & 0 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ void webserver::unregister_resource(const string& resource) {
}

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

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

void webserver::unban_ip(const string& ip) {
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
bans.erase(ip_representation(ip));
}

void webserver::disallow_ip(const string& ip) {
std::unique_lock bans_and_allowances_lock(bans_and_allowances_mutex);
allowances.erase(ip_representation(ip));
}

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

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

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

0 comments on commit 2f7b4ff

Please sign in to comment.