forked from aces/cbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When account request form submitted too quickly, the IP address is internally banned. This commit also includes a callback to a bash script that an admin can customize to ban the IP at the firewall level (or do anything else too).
- Loading branch information
Showing
7 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
# | ||
# CBRAIN Project | ||
# | ||
# Copyright (C) 2008-2024 | ||
# The Royal Institution for the Advancement of Learning | ||
# McGill University | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
# Utility class to persistently manage a list | ||
# of banned IP addresses within a Rails app. | ||
# All methods are class methods. | ||
class BannedIps | ||
|
||
MAIN_LIST_KEY = "banned_ips_array" | ||
BAN_PREFIX = "ban_ip_" | ||
BAN_TIME = 2.weeks | ||
|
||
# Return an array of the currently banned IPs | ||
def self.banned_ips | ||
list = Rails.cache.fetch(MAIN_LIST_KEY, :expires_in => BAN_TIME) { [] } | ||
list2 = list.select { |ip| banned_ip?(ip) } | ||
return list if list.size == list2.size | ||
Rails.cache.write(MAIN_LIST_KEY, list2, :expires_in => BAN_TIME) | ||
return list2 | ||
end | ||
|
||
# Add an IP to the ban list | ||
def self.ban_ip(ip, time = BAN_TIME) | ||
current_list = banned_ips | ||
current_list |= [ ip ] | ||
current_list.sort! | ||
Rails.cache.write(MAIN_LIST_KEY, current_list, :expires_in => time) | ||
Rails.cache.write("#{BAN_PREFIX}#{ip}",true, :expires_in => time) | ||
time | ||
end | ||
|
||
# Remove an IP to the ban list | ||
def self.unban_ip(ip) | ||
Rails.cache.delete("#{BAN_PREFIX}#{ip}") # true/nil | ||
end | ||
|
||
# Query an IP and return true if it is banned | ||
def self.banned_ip?(ip) | ||
Rails.cache.fetch("#{BAN_PREFIX}#{ip}") # true/nil | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash | ||
|
||
# This script can be adjusted by an administrator | ||
# to provide a mechanism to ban an IP address. | ||
# | ||
# This script will receive a single argument, | ||
# an IP address. | ||
# | ||
# Currently, CBRAIN will invoke it if a signup | ||
# request form was posted too quickly (less than | ||
# 10 seconds after the form was generated). | ||
|
||
ip_to_ban="$1" | ||
|
||
# Special safety check to never ban the local IP | ||
if test "X$ip_to_ban" = "X127.0.0.1" ; then | ||
echo "$0 not banning IP '$ip_to_ban'" | ||
exit 0 | ||
fi | ||
|
||
echo "$0 invoked to ban IP '$ip_to_ban'" | ||
|