-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #431 from fatkodima/use-middleware
Auto plug middleware for simpler installation
- Loading branch information
Showing
12 changed files
with
111 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
class Attack | ||
class Railtie < ::Rails::Railtie | ||
initializer 'rack.attack.middleware', after: :load_config_initializers, before: :build_middleware_stack do |app| | ||
if Gem::Version.new(::Rails::VERSION::STRING) >= Gem::Version.new("5") | ||
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 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../spec_helper" | ||
|
||
if defined?(Rails) | ||
describe "Middleware for Rails" do | ||
before do | ||
@app = Class.new(Rails::Application) do | ||
config.eager_load = false | ||
config.logger = Logger.new(nil) # avoid creating the log/ directory automatically | ||
config.cache_store = :null_store # avoid creating tmp/ directory for cache | ||
end | ||
end | ||
|
||
if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("5") | ||
it "is used by default" do | ||
@app.initialize! | ||
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! | ||
refute @app.middleware.include?(Rack::Attack) | ||
end | ||
end | ||
|
||
if Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new("5") | ||
it "is not used by default" do | ||
@app.initialize! | ||
assert_equal 0, @app.middleware.count(Rack::Attack) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters