Skip to content

Commit

Permalink
Merge pull request #2306 from MushroomObserver/jdc-strip-blocked-ips
Browse files Browse the repository at this point in the history
Strip blocked ip addrs
  • Loading branch information
JoeCohen authored Aug 13, 2024
2 parents a8a9e38 + 570997d commit 2cb2c1d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/controllers/admin/blocked_ips_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Admin
class BlockedIpsController < AdminController
# This page allows editing of blocked ips via params
# params[:add_okay] and params[:add_bad]
# GETting this page with params[:report] will show info about a chosen IP
# Using params[:report] will show info about a chosen IP
def edit
@ip = params[:report] if validate_ip!(params[:report])
@blocked_ips = sort_by_ip(IpStats.read_blocked_ips)
Expand All @@ -14,6 +14,7 @@ def edit

# Render the page after an update
def update
strip_params!
process_blocked_ips_commands
@blocked_ips = sort_by_ip(IpStats.read_blocked_ips)
@okay_ips = sort_by_ip(IpStats.read_okay_ips)
Expand All @@ -23,6 +24,12 @@ def update

private

def strip_params!
[:add_bad, :remove_bad, :add_okay, :remove_okay].each do |param|
params[param] = params[param].strip if params[param]
end
end

def sort_by_ip(ips)
ips.sort_by do |ip|
ip.to_s.split(".").map { |n| n.to_i + 1000 }.map(&:to_s).join(" ")
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/admin/blocked_ips_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def test_blocked_ips
assert(time < File.mtime(MO.blocked_ips_file))
IpStats.reset!
assert_false(IpStats.blocked?(new_ip))

time = 1.minute.ago
File.utime(time.to_time, time.to_time, MO.blocked_ips_file)
patch(:update, params: { add_bad: " #{new_ip} " })
assert_no_flash
assert(time < File.mtime(MO.blocked_ips_file))
IpStats.reset!
assert_true(IpStats.blocked?(new_ip),
"It should ignore leading & trailing spaces in ip addr")
end
end
end

0 comments on commit 2cb2c1d

Please sign in to comment.