rack-attack 5.0.0.beta1 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f610566e30822bb7e044db96673364461ab144bc
4
- data.tar.gz: 0c455cc58bdc60917bcf26f8d9a50323c96f9237
3
+ metadata.gz: ef73037a3db9840b433aeb5a0d185f95edeef9be
4
+ data.tar.gz: 98bbf2ea7bbad24f423ff21bf2da4ce4a4f13fc9
5
5
  SHA512:
6
- metadata.gz: 23256e856a36a8a0b37d2617f33ef2ba21032de6053a062d77f2ac035c5e5bde34c463c44d2d3b1c0546b079d7a01a0e005be0cf142d54870eaef64af4f56c6e
7
- data.tar.gz: 9645bcc5b224881e1348e4c97e8ada425ea768c0d7e851c3d022863ba6a01bb31b9e0b942e35c914152ee17775c4d305f893820f3df32392d1d46dd372670371
6
+ metadata.gz: 0c796ea400d3425a0111f16d78efc883113f1fd2a3f55ba624c089978b39235f4851ffe2b546da7b1fe4fce181eea3cff9644185dbb36c373ce22bff3011ddd3
7
+ data.tar.gz: a89e8d96ba25124d062d6da2665827d85773876705310a364a6cc74c90e1349c3d5efed63065846770ffd1c0c537fd0a9756539c6d77f8473653c4d7e20f5940
data/README.md CHANGED
@@ -230,19 +230,40 @@ Rack::Attack.blocklisted_response = lambda do |env|
230
230
  end
231
231
 
232
232
  Rack::Attack.throttled_response = lambda do |env|
233
- # name and other data about the matched throttle
234
- body = [
235
- env['rack.attack.matched'],
236
- env['rack.attack.match_type'],
237
- env['rack.attack.match_data']
238
- ].inspect
233
+ # NB: you have access to the name and other data about the matched throttle
234
+ # env['rack.attack.matched'],
235
+ # env['rack.attack.match_type'],
236
+ # env['rack.attack.match_data']
239
237
 
240
238
  # Using 503 because it may make attacker think that they have successfully
241
239
  # DOSed the site. Rack::Attack returns 429 for throttling by default
242
- [ 503, {}, [body]]
240
+ [ 503, {}, ["Server Error\n"]]
243
241
  end
244
242
  ```
245
243
 
244
+ ### X-RateLimit headers for well-behaved clients
245
+
246
+ While Rack::Attack's primary focus is minimizing harm from abusive clients, it
247
+ can also be used to return rate limit data that's helpful for well-behaved clients.
248
+
249
+ Here's an example response that includes conventional `X-RateLimit-*` headers:
250
+
251
+ ```ruby
252
+ Rack::Attack.throttled_response = lambda do |env|
253
+ now = Time.now
254
+ match_data = env['rack.attack.match_data']
255
+
256
+ headers = {
257
+ 'X-RateLimit-Limit' => match_data[:limit].to_s,
258
+ 'X-RateLimit-Remaining' => '0',
259
+ 'X-RateLimit-Reset' => (now + (match_data[:period] - now.to_i % match_data[:period])).to_s
260
+ }
261
+
262
+ [ 429, headers, ["Throttled\n"]]
263
+ end
264
+ ```
265
+
266
+
246
267
  For responses that did not exceed a throttle limit, Rack::Attack annotates the env with match data:
247
268
 
248
269
  ```ruby
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Attack
3
- VERSION = '5.0.0.beta1'
3
+ VERSION = '5.0.0'
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  require 'active_support/cache'
2
- require 'active_support/cache/redis_store'
2
+ require 'redis-activesupport'
3
3
  require 'dalli'
4
4
  require_relative '../spec_helper'
5
5
 
@@ -85,3 +85,25 @@ describe 'Rack::Attack.throttle with period as proc' do
85
85
  end
86
86
  end
87
87
  end
88
+
89
+ describe 'Rack::Attack.throttle with block retuning nil' do
90
+ before do
91
+ @period = 60
92
+ Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
93
+ Rack::Attack.throttle('ip/sec', :limit => 1, :period => @period) { |_| nil }
94
+ end
95
+
96
+ allow_ok_requests
97
+
98
+ describe 'a single request' do
99
+ before { get '/', {}, 'REMOTE_ADDR' => '1.2.3.4' }
100
+ it 'should not set the counter' do
101
+ key = "rack::attack:#{Time.now.to_i/@period}:ip/sec:1.2.3.4"
102
+ Rack::Attack.cache.store.read(key).must_equal nil
103
+ end
104
+
105
+ it 'should not populate throttle data' do
106
+ last_request.env['rack.attack.throttle_data'].must_equal nil
107
+ end
108
+ end
109
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-attack
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.beta1
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Suggs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-05 00:00:00.000000000 Z
11
+ date: 2016-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -215,9 +215,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
215
215
  version: 2.0.0
216
216
  required_rubygems_version: !ruby/object:Gem::Requirement
217
217
  requirements:
218
- - - ">"
218
+ - - ">="
219
219
  - !ruby/object:Gem::Version
220
- version: 1.3.1
220
+ version: '0'
221
221
  requirements: []
222
222
  rubyforge_project:
223
223
  rubygems_version: 2.5.1