mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
The issue
---
When using rack-attack with a rails app, developers expect the request
path to be normalized. In particular, trailing slashes are stripped so
a request path "/login/" becomes "/login" by the time you're in
ActionController.
Since Rack::Attack runs before ActionDispatch, the request path is not
yet normalized. This can cause throttles and blacklists to not work as
expected.
E.g., a throttle:
throttle('logins', ...) {|req| req.path == "/login" }
would not match a request to '/login/', though Rails would route
'/login/' to the same '/login' action.
The solution
---
This patch looks if ActionDispatch's request normalization is loaded,
and if so, uses it to normalize the path before processing throttles,
blacklists, etc.
If it's not loaded, the request path is not modified.
Credit
---
Thanks to Andres Riancho at Include Security for reporting this issue.
45 lines
852 B
Ruby
45 lines
852 B
Ruby
require "rubygems"
|
|
require "bundler/setup"
|
|
|
|
require "minitest/autorun"
|
|
require "minitest/pride"
|
|
require "rack/test"
|
|
require 'active_support'
|
|
require 'action_dispatch'
|
|
|
|
# Load Journey for Rails 3.2
|
|
require 'journey' if ActionPack::VERSION::MAJOR == 3
|
|
|
|
require "rack/attack"
|
|
|
|
begin
|
|
require 'pry'
|
|
rescue LoadError
|
|
#nothing to do here
|
|
end
|
|
|
|
class MiniTest::Spec
|
|
|
|
include Rack::Test::Methods
|
|
|
|
after { Rack::Attack.clear! }
|
|
|
|
def app
|
|
Rack::Builder.new {
|
|
use Rack::Attack
|
|
run lambda {|env| [200, {}, ['Hello World']]}
|
|
}.to_app
|
|
end
|
|
|
|
def self.allow_ok_requests
|
|
it "must allow ok requests" do
|
|
get '/', {}, 'REMOTE_ADDR' => '127.0.0.1'
|
|
last_response.status.must_equal 200
|
|
last_response.body.must_equal 'Hello World'
|
|
end
|
|
end
|
|
end
|
|
|
|
class Minitest::SharedExamples < Module
|
|
include Minitest::Spec::DSL
|
|
end
|