Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #588 don't fail if request.ip is missing #630

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/rack/attack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ def blocklist(name = nil, &block)
end

def blocklist_ip(ip_address)
@anonymous_blocklists << Blocklist.new { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
@anonymous_blocklists << Blocklist.new do |request|
request.ip && !request.ip.empty? && IPAddr.new(ip_address).include?(IPAddr.new(request.ip))
end
end

def safelist_ip(ip_address)
@anonymous_safelists << Safelist.new { |request| IPAddr.new(ip_address).include?(IPAddr.new(request.ip)) }
@anonymous_safelists << Safelist.new do |request|
request.ip && !request.ip.empty? && IPAddr.new(ip_address).include?(IPAddr.new(request.ip))
end
end

def throttle(name, options, &block)
Expand Down
6 changes: 6 additions & 0 deletions spec/acceptance/blocking_ip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
assert_equal 200, last_response.status
end

it "succeeds if IP is missing" do
get "/", {}, "REMOTE_ADDR" => ""
santib marked this conversation as resolved.
Show resolved Hide resolved

assert_equal 200, last_response.status
end

it "notifies when the request is blocked" do
notified = false
notification_type = nil
Expand Down
6 changes: 6 additions & 0 deletions spec/acceptance/safelisting_ip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
assert_equal 403, last_response.status
end

it "forbids request if blocklist condition is true and safelist is false (missing IP)" do
get "/admin", {}, "REMOTE_ADDR" => ""
santib marked this conversation as resolved.
Show resolved Hide resolved

assert_equal 403, last_response.status
end

it "succeeds if blocklist condition is false and safelist is false" do
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"

Expand Down
Loading