Skip to content

Commit

Permalink
Move allowed_errors to Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Nov 20, 2023
1 parent 7f72d4f commit b8f51d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
12 changes: 4 additions & 8 deletions lib/rack/attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ class IncompatibleStoreError < Error; end
autoload :Fail2Ban, 'rack/attack/fail2ban'
autoload :Allow2Ban, 'rack/attack/allow2ban'

DEFAULT_ALLOWED_ERRORS = %w[Dalli::DalliError Redis::BaseError].freeze

class << self
attr_accessor :enabled,
:notifier,
:throttle_discriminator_normalizer,
:allowed_errors
:throttle_discriminator_normalizer

attr_reader :configuration

Expand Down Expand Up @@ -97,13 +94,12 @@ def allow_error?(error)
:safelists,
:blocklists,
:throttles,
:tracks
:tracks,
:allowed_errors,
:allowed_errors=
)
end

# Set class defaults
self.allowed_errors = DEFAULT_ALLOWED_ERRORS.dup

# Set instance defaults
@enabled = true
@notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
Expand Down
19 changes: 16 additions & 3 deletions lib/rack/attack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module Rack
class Attack
class Configuration
DEFAULT_ALLOWED_ERRORS = %w[Dalli::DalliError Redis::BaseError].freeze

DEFAULT_BLOCKLISTED_RESPONDER = lambda { |_req| [403, { 'content-type' => 'text/plain' }, ["Forbidden\n"]] }

DEFAULT_THROTTLED_RESPONDER = lambda do |req|
Expand All @@ -19,10 +21,20 @@ class Configuration
end
end

attr_reader :safelists, :blocklists, :throttles, :anonymous_blocklists, :anonymous_safelists
attr_accessor :blocklisted_responder, :throttled_responder, :throttled_response_retry_after_header
attr_reader :safelists,
:blocklists,
:throttles,
:anonymous_blocklists,
:anonymous_safelists

attr_accessor :allowed_errors,
:blocklisted_responder,
:throttled_responder,
:throttled_response_retry_after_header

attr_reader :blocklisted_response, :throttled_response # Keeping these for backwards compatibility
# Keeping these for backwards compatibility
attr_reader :blocklisted_response,
:throttled_response

def blocklisted_response=(responder)
warn "[DEPRECATION] Rack::Attack.blocklisted_response is deprecated. "\
Expand Down Expand Up @@ -116,6 +128,7 @@ def set_defaults
@anonymous_blocklists = []
@anonymous_safelists = []
@throttled_response_retry_after_header = false
@allowed_errors = DEFAULT_ALLOWED_ERRORS.dup

@blocklisted_responder = DEFAULT_BLOCKLISTED_RESPONDER
@throttled_responder = DEFAULT_THROTTLED_RESPONDER
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Minitest::Spec
after do
Rack::Attack.clear_configuration
Rack::Attack.instance_variable_set(:@cache, nil)
Rack::Attack.allowed_errors = Rack::Attack::DEFAULT_ALLOWED_ERRORS.dup
Rack::Attack.allowed_errors = Rack::Attack::Configuration::DEFAULT_ALLOWED_ERRORS.dup
end

def app
Expand Down

0 comments on commit b8f51d0

Please sign in to comment.