mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Merge branch '6-stable'
This commit is contained in:
commit
886ba3a18d
5 changed files with 31 additions and 11 deletions
20
CHANGELOG.md
20
CHANGELOG.md
|
|
@ -2,6 +2,25 @@
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [6.5.0] - 2021-02-07
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added ability to normalize throttle discriminator by setting `Rack::Attack.throttle_discriminator_normalizer` (@fatkodima)
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
Rack::Attack.throttle_discriminator_normalizer = ->(discriminator) { ... }
|
||||||
|
|
||||||
|
or disable default normalization with:
|
||||||
|
|
||||||
|
Rack::Attack.throttle_discriminator_normalizer = nil
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
- Dropped support for ruby v2.4
|
||||||
|
- Dropped support for rails v5.1
|
||||||
|
|
||||||
## [6.4.0] - 2021-01-23
|
## [6.4.0] - 2021-01-23
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
@ -232,6 +251,7 @@ so your custom code is less prone to race conditions ([#282](https://github.com/
|
||||||
- Remove unused variable
|
- Remove unused variable
|
||||||
- Extract mandatory options to constants
|
- Extract mandatory options to constants
|
||||||
|
|
||||||
|
[6.5.0]: https://github.com/rack/rack-attack/compare/v6.4.0...v6.5.0/
|
||||||
[6.4.0]: https://github.com/rack/rack-attack/compare/v6.3.1...v6.4.0/
|
[6.4.0]: https://github.com/rack/rack-attack/compare/v6.3.1...v6.4.0/
|
||||||
[6.3.1]: https://github.com/rack/rack-attack/compare/v6.3.0...v6.3.1/
|
[6.3.1]: https://github.com/rack/rack-attack/compare/v6.3.0...v6.3.1/
|
||||||
[6.3.0]: https://github.com/rack/rack-attack/compare/v6.2.2...v6.3.0/
|
[6.3.0]: https://github.com/rack/rack-attack/compare/v6.2.2...v6.3.0/
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ module Rack
|
||||||
autoload :Allow2Ban, 'rack/attack/allow2ban'
|
autoload :Allow2Ban, 'rack/attack/allow2ban'
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :enabled, :notifier, :discriminator_normalizer
|
attr_accessor :enabled, :notifier, :throttle_discriminator_normalizer
|
||||||
attr_reader :configuration
|
attr_reader :configuration
|
||||||
|
|
||||||
def instrument(request)
|
def instrument(request)
|
||||||
|
|
@ -87,7 +87,7 @@ module Rack
|
||||||
# Set defaults
|
# Set defaults
|
||||||
@enabled = true
|
@enabled = true
|
||||||
@notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
|
@notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
|
||||||
@discriminator_normalizer = lambda do |discriminator|
|
@throttle_discriminator_normalizer = lambda do |discriminator|
|
||||||
discriminator.to_s.strip.downcase
|
discriminator.to_s.strip.downcase
|
||||||
end
|
end
|
||||||
@configuration = Configuration.new
|
@configuration = Configuration.new
|
||||||
|
|
|
||||||
|
|
@ -51,8 +51,8 @@ module Rack
|
||||||
|
|
||||||
def discriminator_for(request)
|
def discriminator_for(request)
|
||||||
discriminator = block.call(request)
|
discriminator = block.call(request)
|
||||||
if discriminator && Rack::Attack.discriminator_normalizer
|
if discriminator && Rack::Attack.throttle_discriminator_normalizer
|
||||||
discriminator = Rack::Attack.discriminator_normalizer.call(discriminator)
|
discriminator = Rack::Attack.throttle_discriminator_normalizer.call(discriminator)
|
||||||
end
|
end
|
||||||
discriminator
|
discriminator
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,6 @@
|
||||||
|
|
||||||
module Rack
|
module Rack
|
||||||
class Attack
|
class Attack
|
||||||
VERSION = '6.4.0'
|
VERSION = '6.5.0'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -145,7 +145,7 @@ describe 'Rack::Attack.throttle with block retuning nil' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'Rack::Attack.throttle with discriminator_normalizer' do
|
describe 'Rack::Attack.throttle with throttle_discriminator_normalizer' do
|
||||||
before do
|
before do
|
||||||
@period = 60
|
@period = 60
|
||||||
@emails = [
|
@emails = [
|
||||||
|
|
@ -161,16 +161,16 @@ describe 'Rack::Attack.throttle with discriminator_normalizer' do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not differentiate requests when discriminator_normalizer is enabled' do
|
it 'should not differentiate requests when throttle_discriminator_normalizer is enabled' do
|
||||||
post_logins
|
post_logins
|
||||||
key = "rack::attack:#{Time.now.to_i / @period}:logins/email:person@example.com"
|
key = "rack::attack:#{Time.now.to_i / @period}:logins/email:person@example.com"
|
||||||
_(Rack::Attack.cache.store.read(key)).must_equal 3
|
_(Rack::Attack.cache.store.read(key)).must_equal 3
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should differentiate requests when discriminator_normalizer is disabled' do
|
it 'should differentiate requests when throttle_discriminator_normalizer is disabled' do
|
||||||
begin
|
begin
|
||||||
prev = Rack::Attack.discriminator_normalizer
|
prev = Rack::Attack.throttle_discriminator_normalizer
|
||||||
Rack::Attack.discriminator_normalizer = nil
|
Rack::Attack.throttle_discriminator_normalizer = nil
|
||||||
|
|
||||||
post_logins
|
post_logins
|
||||||
@emails.each do |email|
|
@emails.each do |email|
|
||||||
|
|
@ -178,7 +178,7 @@ describe 'Rack::Attack.throttle with discriminator_normalizer' do
|
||||||
_(Rack::Attack.cache.store.read(key)).must_equal 1
|
_(Rack::Attack.cache.store.read(key)).must_equal 1
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
Rack::Attack.discriminator_normalizer = prev
|
Rack::Attack.throttle_discriminator_normalizer = prev
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue