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: avoid unintended effects on load_config_initializers and other gems load order #457

Merged
merged 1 commit into from
Oct 30, 2019
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
3 changes: 2 additions & 1 deletion lib/rack/attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ def initialize(app)
end

def call(env)
return @app.call(env) unless self.class.enabled
return @app.call(env) if !self.class.enabled || env["rack.attack.called"]

env["rack.attack.called"] = true
env['PATH_INFO'] = PathNormalizer.normalize_path(env['PATH_INFO'])
request = Rack::Attack::Request.new(env)

Expand Down
12 changes: 2 additions & 10 deletions lib/rack/attack/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@
module Rack
class Attack
class Railtie < ::Rails::Railtie
initializer 'rack.attack.middleware', after: :load_config_initializers, before: :build_middleware_stack do |app|
initializer "rack-attack.middleware" do |app|
if Gem::Version.new(::Rails::VERSION::STRING) >= Gem::Version.new("5.1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With new implementation, I think this restriction can be removed.

Copy link
Collaborator Author

@grzuy grzuy Oct 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably.

I want this change to be the minimal possible update that fixes the issue, which can be released as a patch-level update (v6.2.1).

If we open this up for more rails version, that can be part of v6.3.0.

Thanks for reviewing.

middlewares = app.config.middleware
operations = middlewares.send(:operations) + middlewares.send(:delete_operations)

use_middleware = operations.none? do |operation|
middleware = operation[1]
middleware.include?(Rack::Attack)
end

middlewares.use(Rack::Attack) if use_middleware
app.middleware.use(Rack::Attack)
end
end
end
Expand Down
6 changes: 0 additions & 6 deletions spec/acceptance/rails_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
assert_equal 1, @app.middleware.count(Rack::Attack)
end

it "is not added when it was added explicitly" do
@app.config.middleware.use(Rack::Attack)
@app.initialize!
assert_equal 1, @app.middleware.count(Rack::Attack)
end

it "is not added when it was explicitly deleted" do
@app.config.middleware.delete(Rack::Attack)
@app.initialize!
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def app
Rack::Builder.new do
# Use Rack::Lint to test that rack-attack is complying with the rack spec
use Rack::Lint
# Intentionally added twice to test idempotence property
use Rack::Attack

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

use Rack::Attack
use Rack::Lint

Expand Down