mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
style: prefer ruby 1.9+ hash syntax
This commit is contained in:
parent
ee7177fbff
commit
c67e71defe
9 changed files with 23 additions and 20 deletions
|
|
@ -44,6 +44,9 @@ Style/BracesAroundHashParameters:
|
||||||
Style/FrozenStringLiteralComment:
|
Style/FrozenStringLiteralComment:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
|
Style/HashSyntax:
|
||||||
|
Enabled: true
|
||||||
|
|
||||||
Style/RedundantFreeze:
|
Style/RedundantFreeze:
|
||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
|
|
|
||||||
2
Rakefile
2
Rakefile
|
|
@ -26,4 +26,4 @@ Rake::TestTask.new(:test) do |t|
|
||||||
t.pattern = "spec/**/*_spec.rb"
|
t.pattern = "spec/**/*_spec.rb"
|
||||||
end
|
end
|
||||||
|
|
||||||
task :default => [:rubocop, :test]
|
task default: [:rubocop, :test]
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ module Rack
|
||||||
end
|
end
|
||||||
|
|
||||||
def write(unprefixed_key, value, expires_in)
|
def write(unprefixed_key, value, expires_in)
|
||||||
store.write("#{prefix}:#{unprefixed_key}", value, :expires_in => expires_in)
|
store.write("#{prefix}:#{unprefixed_key}", value, expires_in: expires_in)
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_count(unprefixed_key, period)
|
def reset_count(unprefixed_key, period)
|
||||||
|
|
@ -54,13 +54,13 @@ module Rack
|
||||||
enforce_store_presence!
|
enforce_store_presence!
|
||||||
enforce_store_method_presence!(:increment)
|
enforce_store_method_presence!(:increment)
|
||||||
|
|
||||||
result = store.increment(key, 1, :expires_in => expires_in)
|
result = store.increment(key, 1, expires_in: expires_in)
|
||||||
|
|
||||||
# NB: Some stores return nil when incrementing uninitialized values
|
# NB: Some stores return nil when incrementing uninitialized values
|
||||||
if result.nil?
|
if result.nil?
|
||||||
enforce_store_method_presence!(:write)
|
enforce_store_method_presence!(:write)
|
||||||
|
|
||||||
store.write(key, 1, :expires_in => expires_in)
|
store.write(key, 1, expires_in: expires_in)
|
||||||
end
|
end
|
||||||
result || 1
|
result || 1
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -31,10 +31,10 @@ module Rack
|
||||||
epoch_time = cache.last_epoch_time
|
epoch_time = cache.last_epoch_time
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
:count => count,
|
count: count,
|
||||||
:period => current_period,
|
period: current_period,
|
||||||
:limit => current_limit,
|
limit: current_limit,
|
||||||
:epoch_time => epoch_time
|
epoch_time: epoch_time
|
||||||
}
|
}
|
||||||
|
|
||||||
(request.env['rack.attack.throttle_data'] ||= {})[name] = data
|
(request.env['rack.attack.throttle_data'] ||= {})[name] = data
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ describe 'Rack::Attack.Allow2Ban' do
|
||||||
@findtime = 60
|
@findtime = 60
|
||||||
@bantime = 60
|
@bantime = 60
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||||
@f2b_options = { :bantime => @bantime, :findtime => @findtime, :maxretry => 2 }
|
@f2b_options = { bantime: @bantime, findtime: @findtime, maxretry: 2 }
|
||||||
|
|
||||||
Rack::Attack.blocklist('pentest') do |req|
|
Rack::Attack.blocklist('pentest') do |req|
|
||||||
Rack::Attack::Allow2Ban.filter(req.ip, @f2b_options) { req.query_string =~ /OMGHAX/ }
|
Rack::Attack::Allow2Ban.filter(req.ip, @f2b_options) { req.query_string =~ /OMGHAX/ }
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ describe 'Rack::Attack.Fail2Ban' do
|
||||||
@findtime = 60
|
@findtime = 60
|
||||||
@bantime = 60
|
@bantime = 60
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||||
@f2b_options = { :bantime => @bantime, :findtime => @findtime, :maxretry => 2 }
|
@f2b_options = { bantime: @bantime, findtime: @findtime, maxretry: 2 }
|
||||||
|
|
||||||
Rack::Attack.blocklist('pentest') do |req|
|
Rack::Attack.blocklist('pentest') do |req|
|
||||||
Rack::Attack::Fail2Ban.filter(req.ip, @f2b_options) { req.query_string =~ /OMGHAX/ }
|
Rack::Attack::Fail2Ban.filter(req.ip, @f2b_options) { req.query_string =~ /OMGHAX/ }
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ if defined?(::ActiveSupport::Cache::RedisStore)
|
||||||
before do
|
before do
|
||||||
@cache = Rack::Attack::Cache.new
|
@cache = Rack::Attack::Cache.new
|
||||||
# Use presumably unused port for Redis client
|
# Use presumably unused port for Redis client
|
||||||
@cache.store = ActiveSupport::Cache::RedisStore.new(:host => '127.0.0.1', :port => 3333)
|
@cache.store = ActiveSupport::Cache::RedisStore.new(host: '127.0.0.1', port: 3333)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ describe 'Rack::Attack.throttle' do
|
||||||
before do
|
before do
|
||||||
@period = 60 # Use a long period; failures due to cache key rotation less likely
|
@period = 60 # Use a long period; failures due to cache key rotation less likely
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||||
Rack::Attack.throttle('ip/sec', :limit => 1, :period => @period) { |req| req.ip }
|
Rack::Attack.throttle('ip/sec', limit: 1, period: @period) { |req| req.ip }
|
||||||
end
|
end
|
||||||
|
|
||||||
it('should have a throttle') { Rack::Attack.throttles.key?('ip/sec') }
|
it('should have a throttle') { Rack::Attack.throttles.key?('ip/sec') }
|
||||||
|
|
@ -22,7 +22,7 @@ describe 'Rack::Attack.throttle' do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should populate throttle data' do
|
it 'should populate throttle data' do
|
||||||
data = { :count => 1, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
|
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
|
||||||
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -39,7 +39,7 @@ describe 'Rack::Attack.throttle' do
|
||||||
it 'should tag the env' do
|
it 'should tag the env' do
|
||||||
last_request.env['rack.attack.matched'].must_equal 'ip/sec'
|
last_request.env['rack.attack.matched'].must_equal 'ip/sec'
|
||||||
last_request.env['rack.attack.match_type'].must_equal :throttle
|
last_request.env['rack.attack.match_type'].must_equal :throttle
|
||||||
last_request.env['rack.attack.match_data'].must_equal(:count => 2, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i)
|
last_request.env['rack.attack.match_data'].must_equal(count: 2, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i)
|
||||||
last_request.env['rack.attack.match_discriminator'].must_equal('1.2.3.4')
|
last_request.env['rack.attack.match_discriminator'].must_equal('1.2.3.4')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ describe 'Rack::Attack.throttle with limit as proc' do
|
||||||
before do
|
before do
|
||||||
@period = 60 # Use a long period; failures due to cache key rotation less likely
|
@period = 60 # Use a long period; failures due to cache key rotation less likely
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||||
Rack::Attack.throttle('ip/sec', :limit => lambda { |_req| 1 }, :period => @period) { |req| req.ip }
|
Rack::Attack.throttle('ip/sec', limit: lambda { |_req| 1 }, period: @period) { |req| req.ip }
|
||||||
end
|
end
|
||||||
|
|
||||||
it_allows_ok_requests
|
it_allows_ok_requests
|
||||||
|
|
@ -67,7 +67,7 @@ describe 'Rack::Attack.throttle with limit as proc' do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should populate throttle data' do
|
it 'should populate throttle data' do
|
||||||
data = { :count => 1, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
|
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
|
||||||
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -77,7 +77,7 @@ describe 'Rack::Attack.throttle with period as proc' do
|
||||||
before do
|
before do
|
||||||
@period = 60 # Use a long period; failures due to cache key rotation less likely
|
@period = 60 # Use a long period; failures due to cache key rotation less likely
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||||
Rack::Attack.throttle('ip/sec', :limit => lambda { |_req| 1 }, :period => lambda { |_req| @period }) { |req| req.ip }
|
Rack::Attack.throttle('ip/sec', limit: lambda { |_req| 1 }, period: lambda { |_req| @period }) { |req| req.ip }
|
||||||
end
|
end
|
||||||
|
|
||||||
it_allows_ok_requests
|
it_allows_ok_requests
|
||||||
|
|
@ -91,7 +91,7 @@ describe 'Rack::Attack.throttle with period as proc' do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should populate throttle data' do
|
it 'should populate throttle data' do
|
||||||
data = { :count => 1, :limit => 1, :period => @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
|
data = { count: 1, limit: 1, period: @period, epoch_time: Rack::Attack.cache.last_epoch_time.to_i }
|
||||||
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
last_request.env['rack.attack.throttle_data']['ip/sec'].must_equal data
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -101,7 +101,7 @@ describe 'Rack::Attack.throttle with block retuning nil' do
|
||||||
before do
|
before do
|
||||||
@period = 60
|
@period = 60
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||||
Rack::Attack.throttle('ip/sec', :limit => 1, :period => @period) { |_| nil }
|
Rack::Attack.throttle('ip/sec', limit: 1, period: @period) { |_| nil }
|
||||||
end
|
end
|
||||||
|
|
||||||
it_allows_ok_requests
|
it_allows_ok_requests
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ describe 'Rack::Attack.track' do
|
||||||
|
|
||||||
describe "with limit and period options" do
|
describe "with limit and period options" do
|
||||||
it "should assign the track filter to a Throttle instance" do
|
it "should assign the track filter to a Throttle instance" do
|
||||||
track = Rack::Attack.track("homepage", :limit => 10, :period => 10) { |req| req.path == "/" }
|
track = Rack::Attack.track("homepage", limit: 10, period: 10) { |req| req.path == "/" }
|
||||||
track.filter.class.must_equal Rack::Attack::Throttle
|
track.filter.class.must_equal Rack::Attack::Throttle
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue