-
Notifications
You must be signed in to change notification settings - Fork 337
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
Make it possible to block requests based on response. #646
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
class Attack | ||
class Postrequest < Check | ||
def initialize(name = nil, &block) | ||
super | ||
@type = :postrequest | ||
end | ||
|
||
def matched_by?(request, response) | ||
block.call(request, response).tap do |match| | ||
if match | ||
request.env["rack.attack.matched"] = name | ||
request.env["rack.attack.match_type"] = type | ||
Rack::Attack.instrument(request) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../spec_helper" | ||
require "timecop" | ||
|
||
describe "postrequest" do | ||
before do | ||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new | ||
|
||
Rack::Attack.postrequest("fail2ban for 404") do |request, response| | ||
Rack::Attack::Fail2Ban.filter(request.ip, maxretry: 2, findtime: 30, bantime: 60) do | ||
if response.nil? | ||
false | ||
else | ||
response[0] == 404 | ||
end | ||
end | ||
end | ||
end | ||
|
||
it "returns OK for many requests with 200 status" do | ||
get "/" | ||
assert_equal 200, last_response.status | ||
|
||
get "/" | ||
assert_equal 200, last_response.status | ||
end | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spec/acceptance/postrequest_spec.rb:29:1: C: [Correctable] Layout/EmptyLines: Extra blank line detected. 68 files inspected, 2 offenses detected, 2 offenses auto-correctable |
||
it "returns OK for few requests with 404 status" do | ||
get "/not_found" | ||
assert_equal 404, last_response.status | ||
|
||
get "/not_found" | ||
assert_equal 404, last_response.status | ||
end | ||
|
||
it "forbids all access after reaching maxretry limit" do | ||
get "/not_found" | ||
assert_equal 404, last_response.status | ||
|
||
get "/not_found" | ||
assert_equal 404, last_response.status | ||
|
||
get "/not_found" | ||
assert_equal 403, last_response.status | ||
|
||
get "/" | ||
assert_equal 403, last_response.status | ||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that this method calling twice:
app.call
app.call
I guess that we can accidentally mark a request as matched even when it doesn't blocked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mb change
postrequest.matched_by?
topostrequest.block.call
?#lib/rack/attack/configuration.rb