Skip to content

Commit 5998db8

Browse files
authored
Merge branch 'main' into multiple-instances
2 parents f23dd26 + b6ce502 commit 5998db8

35 files changed

+279
-253
lines changed

.github/dependabot.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 2
2+
3+
updates:
4+
- package-ecosystem: "github-actions"
5+
directory: "/"
6+
schedule:
7+
interval: "weekly"

.github/workflows/build.yml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ jobs:
1515
ports:
1616
- 11211:11211
1717
strategy:
18+
fail-fast: false
1819
matrix:
1920
ruby:
21+
- '3.3'
2022
- '3.2'
2123
- '3.1'
2224
- '3.0'
@@ -26,7 +28,6 @@ jobs:
2628
gemfile:
2729
- rack_3
2830
- rack_2
29-
- rack_1
3031
- rails_7_1
3132
- rails_7_0
3233
- rails_6_1
@@ -47,8 +48,14 @@ jobs:
4748
- active_support_5_redis_cache_store_pooled
4849
- redis_store
4950
exclude:
50-
- gemfile: rack_1
51-
ruby: '3.2'
51+
- gemfile: rails_5_2
52+
ruby: '3.3'
53+
- gemfile: active_support_5_redis_cache_store
54+
ruby: '3.3'
55+
- gemfile: active_support_5_redis_cache_store_pooled
56+
ruby: '3.3'
57+
- gemfile: dalli2
58+
ruby: '3.3'
5259
- gemfile: rails_5_2
5360
ruby: '3.2'
5461
- gemfile: active_support_5_redis_cache_store
@@ -57,8 +64,6 @@ jobs:
5764
ruby: '3.2'
5865
- gemfile: dalli2
5966
ruby: '3.2'
60-
- gemfile: rack_1
61-
ruby: '3.1'
6267
- gemfile: rails_5_2
6368
ruby: '3.1'
6469
- gemfile: active_support_5_redis_cache_store
@@ -67,8 +72,6 @@ jobs:
6772
ruby: '3.1'
6873
- gemfile: dalli2
6974
ruby: '3.1'
70-
- gemfile: rack_1
71-
ruby: '3.0'
7275
- gemfile: rails_5_2
7376
ruby: '3.0'
7477
- gemfile: active_support_5_redis_cache_store
@@ -77,8 +80,6 @@ jobs:
7780
ruby: '3.0'
7881
- gemfile: dalli2
7982
ruby: '3.0'
80-
- gemfile: rack_1
81-
ruby: '2.7'
8283
- gemfile: rails_7_0
8384
ruby: '2.6'
8485
- gemfile: rails_7_0
@@ -106,7 +107,7 @@ jobs:
106107
env:
107108
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
108109
steps:
109-
- uses: actions/checkout@v2
110+
- uses: actions/checkout@v4
110111
- uses: ruby/setup-ruby@v1
111112
with:
112113
ruby-version: ${{ matrix.ruby }}

Appraisals

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@ appraise "rack_2" do
88
gem "rack", "~> 2.0"
99
end
1010

11-
appraise "rack_1" do
12-
# Override activesupport and actionpack version constraints by making
13-
# it more loose so it's compatible with rack 1.6.x
14-
gem "actionpack", ">= 4.2"
15-
gem "activesupport", ">= 4.2"
16-
17-
gem "rack", "~> 1.6"
18-
19-
# Override rack-test version constraint by making it more loose
20-
# so it's compatible with actionpack 4.2.x
21-
gem "rack-test", ">= 0.6"
22-
end
23-
2411
appraise 'rails_7-1' do
2512
gem 'railties', '~> 7.1.0'
2613
end

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ Rack::Attack.track("special_agent", limit: 6, period: 60) do |req|
291291
end
292292

293293
# Track it using ActiveSupport::Notification
294-
ActiveSupport::Notifications.subscribe("track.rack_attack") do |name, start, finish, request_id, payload|
294+
ActiveSupport::Notifications.subscribe("track.rack_attack") do |name, start, finish, instrumenter_id, payload|
295295
req = payload[:request]
296296
if req.env['rack.attack.matched'] == "special_agent"
297297
Rails.logger.info "special_agent: #{req.path}"
@@ -383,7 +383,7 @@ To get notified about specific type of events, subscribe to the event name follo
383383
E.g. for throttles use:
384384

385385
```ruby
386-
ActiveSupport::Notifications.subscribe("throttle.rack_attack") do |name, start, finish, request_id, payload|
386+
ActiveSupport::Notifications.subscribe("throttle.rack_attack") do |name, start, finish, instrumenter_id, payload|
387387
# request object available in payload[:request]
388388

389389
# Your code here
@@ -393,7 +393,7 @@ end
393393
If you want to subscribe to every `rack_attack` event, use:
394394

395395
```ruby
396-
ActiveSupport::Notifications.subscribe(/rack_attack/) do |name, start, finish, request_id, payload|
396+
ActiveSupport::Notifications.subscribe(/rack_attack/) do |name, start, finish, instrumenter_id, payload|
397397
# request object available in payload[:request]
398398

399399
# Your code here

gemfiles/rack_1.gemfile

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/rack/attack/store_proxy/redis_cache_store_proxy.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ def self.handle?(store)
1010
store.class.name == "ActiveSupport::Cache::RedisCacheStore"
1111
end
1212

13-
def increment(name, amount = 1, **options)
14-
# RedisCacheStore#increment ignores options[:expires_in].
15-
#
16-
# So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize
17-
# the counter. After that we continue using the original RedisCacheStore#increment.
18-
if options[:expires_in] && !read(name)
19-
write(name, amount, options)
13+
if defined?(::ActiveSupport) && ::ActiveSupport::VERSION::MAJOR < 6
14+
def increment(name, amount = 1, **options)
15+
# RedisCacheStore#increment ignores options[:expires_in] in versions prior to 6.
16+
#
17+
# So in order to workaround this we use RedisCacheStore#write (which sets expiration) to initialize
18+
# the counter. After that we continue using the original RedisCacheStore#increment.
19+
if options[:expires_in] && !read(name)
20+
write(name, amount, options)
2021

21-
amount
22-
else
23-
super
22+
amount
23+
else
24+
super
25+
end
2426
end
2527
end
2628

spec/acceptance/blocking_ip_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "Blocking an IP" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist_ip("1.2.3.4")
810
end
@@ -26,21 +28,18 @@
2628
end
2729

2830
it "notifies when the request is blocked" do
29-
notified = false
30-
notification_type = nil
31-
3231
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
33-
notified = true
34-
notification_type = payload[:request].env["rack.attack.match_type"]
32+
notifications.push(payload)
3533
end
3634

3735
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3836

39-
refute notified
37+
assert notifications.empty?
4038

4139
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
4240

43-
assert notified
44-
assert_equal :blocklist, notification_type
41+
assert_equal 1, notifications.size
42+
notification = notifications.pop
43+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
4544
end
4645
end

spec/acceptance/blocking_spec.rb

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "#blocklist" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist do |request|
810
request.ip == "1.2.3.4"
@@ -22,27 +24,26 @@
2224
end
2325

2426
it "notifies when the request is blocked" do
25-
notification_matched = nil
26-
notification_type = nil
27-
2827
ActiveSupport::Notifications.subscribe("rack.attack") do |_name, _start, _finish, _id, payload|
29-
notification_matched = payload[:request].env["rack.attack.matched"]
30-
notification_type = payload[:request].env["rack.attack.match_type"]
28+
notifications.push(payload)
3129
end
3230

3331
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3432

35-
assert_nil notification_matched
36-
assert_nil notification_type
33+
assert notifications.empty?
3734

3835
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
3936

40-
assert_nil notification_matched
41-
assert_equal :blocklist, notification_type
37+
assert_equal 1, notifications.size
38+
notification = notifications.pop
39+
assert_nil notification[:request].env["rack.attack.matched"]
40+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
4241
end
4342
end
4443

4544
describe "#blocklist with name" do
45+
let(:notifications) { [] }
46+
4647
before do
4748
Rack::Attack.blocklist("block 1.2.3.4") do |request|
4849
request.ip == "1.2.3.4"
@@ -62,22 +63,19 @@
6263
end
6364

6465
it "notifies when the request is blocked" do
65-
notification_matched = nil
66-
notification_type = nil
67-
6866
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
69-
notification_matched = payload[:request].env["rack.attack.matched"]
70-
notification_type = payload[:request].env["rack.attack.match_type"]
67+
notifications.push(payload)
7168
end
7269

7370
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
7471

75-
assert_nil notification_matched
76-
assert_nil notification_type
72+
assert notifications.empty?
7773

7874
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
7975

80-
assert_equal "block 1.2.3.4", notification_matched
81-
assert_equal :blocklist, notification_type
76+
assert_equal 1, notifications.size
77+
notification = notifications.pop
78+
assert_equal "block 1.2.3.4", notification[:request].env["rack.attack.matched"]
79+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
8280
end
8381
end

spec/acceptance/blocking_subnet_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require_relative "../spec_helper"
44

55
describe "Blocking an IP subnet" do
6+
let(:notifications) { [] }
7+
68
before do
79
Rack::Attack.blocklist_ip("1.2.3.4/31")
810
end
@@ -26,21 +28,18 @@
2628
end
2729

2830
it "notifies when the request is blocked" do
29-
notified = false
30-
notification_type = nil
31-
3231
ActiveSupport::Notifications.subscribe("blocklist.rack_attack") do |_name, _start, _finish, _id, payload|
33-
notified = true
34-
notification_type = payload[:request].env["rack.attack.match_type"]
32+
notifications.push(payload)
3533
end
3634

3735
get "/", {}, "REMOTE_ADDR" => "5.6.7.8"
3836

39-
refute notified
37+
assert notifications.empty?
4038

4139
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
4240

43-
assert notified
44-
assert_equal :blocklist, notification_type
41+
assert_equal 1, notifications.size
42+
notification = notifications.pop
43+
assert_equal :blocklist, notification[:request].env["rack.attack.match_type"]
4544
end
4645
end

spec/acceptance/cache_store_config_for_allow2ban_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
end
1313
end
1414

15-
it "gives semantic error if no store was configured" do
16-
assert_raises(Rack::Attack::MissingStoreError) do
17-
get "/scarce-resource"
15+
unless defined?(Rails)
16+
it "gives semantic error if no store was configured" do
17+
assert_raises(Rack::Attack::MissingStoreError) do
18+
get "/scarce-resource"
19+
end
1820
end
1921
end
2022

0 commit comments

Comments
 (0)