mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Refactor the "doesn't leak keys" spec
This commit is contained in:
parent
fab29695f9
commit
03b8ce6f9e
11 changed files with 37 additions and 232 deletions
|
|
@ -16,28 +16,6 @@ if defined?(::Dalli)
|
|||
Rack::Attack.cache.store.clear
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.fetch(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,28 +15,6 @@ if defined?(::Dalli)
|
|||
Rack::Attack.cache.store.flush_all
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.get(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.get(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.get(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -14,27 +14,5 @@ describe "ActiveSupport::Cache::MemoryStore as a cache backend" do
|
|||
Rack::Attack.cache.store.clear
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.fetch(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,28 +15,6 @@ if defined?(::ConnectionPool) && defined?(::Redis) && defined?(::ActiveSupport::
|
|||
Rack::Attack.cache.store.clear
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.fetch(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,29 +15,6 @@ if defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore)
|
|||
Rack::Attack.cache.store.clear
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
# puts key
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.fetch(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,28 +15,6 @@ if defined?(::ActiveSupport::Cache::RedisStore)
|
|||
Rack::Attack.cache.store.flushdb
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.read(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.read(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.read(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -17,28 +17,8 @@ if defined?(::Dalli) && defined?(::ConnectionPool)
|
|||
Rack::Attack.cache.store.with { |client| client.flush_all }
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert(Rack::Attack.cache.store.with { |client| client.fetch(key) })
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil(Rack::Attack.cache.store.with { |client| client.fetch(key) })
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) {
|
||||
Rack::Attack.cache.store.with { |client| client.fetch(key) }
|
||||
})
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -16,28 +16,6 @@ if defined?(::Dalli)
|
|||
Rack::Attack.cache.store.flush_all
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.fetch(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,28 +15,6 @@ if defined?(::Redis)
|
|||
Rack::Attack.cache.store.flushdb
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.get(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.get(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.get(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,28 +15,6 @@ if defined?(::Redis::Store)
|
|||
Rack::Attack.cache.store.flushdb
|
||||
end
|
||||
|
||||
it_works_for_cache_backed_features
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.read(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.read(key)
|
||||
end
|
||||
it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.read(key) })
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Minitest::Spec
|
||||
def self.it_works_for_cache_backed_features
|
||||
def self.it_works_for_cache_backed_features(options)
|
||||
fetch_from_store = options.fetch(:fetch_from_store)
|
||||
|
||||
it "works for throttle" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request|
|
||||
request.ip
|
||||
|
|
@ -56,5 +58,27 @@ class Minitest::Spec
|
|||
get "/"
|
||||
assert_equal 403, last_response.status
|
||||
end
|
||||
|
||||
it "doesn't leak keys" do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
key = nil
|
||||
|
||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||
# we pre-calculate in local variable `key`
|
||||
Timecop.freeze do
|
||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
|
||||
assert fetch_from_store.call(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil fetch_from_store.call(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue