rack-attack-rate-limit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - "2.0.0"
5
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-attack-ratelimit.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jason Byck
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # Rack::Attack::RateLimit
2
+
3
+ [![Build Status](https://travis-ci.org/jbyck/rack-attack-rate-limit.png?branch=master)](https://travis-ci.org/jbyck/rack-attack-rate-limit)
4
+
5
+ Add rate limit headers for [Rack::Attack](https://github.com/kickstarter/rack-attack) throttles.
6
+
7
+ ## Installation
8
+
9
+ Install the gem:
10
+
11
+ ```shell
12
+ gem install 'rack-attack-rate-limit'
13
+ ```
14
+
15
+ In your gemfile:
16
+ ```ruby
17
+ gem 'rack-attack-rate-limit', require: 'rack/attack/rate-limit'
18
+ ```
19
+
20
+
21
+
22
+ And then execute:
23
+ ```shell
24
+ bundle
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Rack::Attack::RateLimit expects a Rack::Attack throttle to be defined:
30
+
31
+ ```ruby
32
+ Rack::Attack.throttle('my_throttle') do |req|
33
+ req.ip
34
+ end
35
+ ```
36
+
37
+ To include rate limit headers for this throttle, include the Rack::Attack::RateLimit middleware
38
+
39
+ For Rails 3+:
40
+
41
+ ```ruby
42
+ config.middleware.use Rack::Attack::RateLimit, throttle: 'my_throttle'
43
+ ```
44
+
45
+ Currently, Rack::Attack::RateLimit can only be configured to return rate limit headers for a single throttle, whose name can be specified as an option.
46
+
47
+ Rate limit headers are:
48
+
49
+ * 'X-RateLimit-Limit' - The total number of requests allowed.
50
+ * 'X-RateLimit-Remaining' - The number of remaining requests.
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,84 @@
1
+ module Rack
2
+ module Attack
3
+ class RateLimit
4
+
5
+ RACK_ATTACK_KEY = 'rack.attack.throttle_data'
6
+
7
+ attr_reader :app, :options
8
+
9
+ def initialize(app, options = {})
10
+ @app = app
11
+ @options = default_options.merge(options)
12
+ end
13
+
14
+ def call(env)
15
+ # If env does not have necessary data to extract rate limit data for the provider, then app.call
16
+ return app.call(env) unless rate_limit_available?(env)
17
+ # Otherwise, add rate limit headers
18
+ status, headers, body = app.call(env)
19
+ add_rate_limit_headers!(headers, env)
20
+ [status, headers, body]
21
+ end
22
+
23
+ # Returns env key used by Rack::Attack to namespace data
24
+ #
25
+ # Returns string
26
+ def rack_attack_key
27
+ RACK_ATTACK_KEY
28
+ end
29
+
30
+ # Default options to configure Rack::RateLimit
31
+ #
32
+ # Returns hash
33
+ def default_options
34
+ { throttle: 'throttle' }
35
+ end
36
+
37
+ def throttle
38
+ options[:throttle] || ''
39
+ end
40
+
41
+ # Return hash of headers with Rate Limiting data
42
+ #
43
+ # headers - Hash of headers
44
+ #
45
+ # Returns hash
46
+ def add_rate_limit_headers!(headers, env)
47
+ headers['X-RateLimit-Limit'] = rate_limit_limit(env).to_s
48
+ headers['X-RateLimit-Remaining'] = rate_limit_remaining(env).to_s
49
+ headers
50
+ end
51
+
52
+ protected
53
+
54
+ # RateLimit upper limit from Rack::Attack
55
+ #
56
+ # env - Hash
57
+ #
58
+ # Returns Fixnum
59
+ def rate_limit_limit(env)
60
+ env[rack_attack_key][throttle][:limit]
61
+ end
62
+
63
+ # RateLimit remaining request from Rack::Attack
64
+ #
65
+ # env - Hash
66
+ #
67
+ # Returns Fixnum
68
+ def rate_limit_remaining(env)
69
+ rate_limit_limit(env) - env[rack_attack_key][throttle][:count]
70
+ end
71
+
72
+ # Rate Limit available method for Rack::Attack provider
73
+ # Checks the key identifed by options[:namespace] under the rack.attak.throttle_data env hash key
74
+ #
75
+ # env - Hash
76
+ #
77
+ # Returns boolean
78
+ def rate_limit_available?(env)
79
+ env.key?(rack_attack_key) and env[rack_attack_key].key?(throttle)
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,7 @@
1
+ module Rack
2
+ module Attack
3
+ class RateLimit
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'rack/attack/rate-limit/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "rack-attack-rate-limit"
9
+ spec.version = Rack::Attack::RateLimit::VERSION
10
+ spec.authors = ["Jason Byck"]
11
+ spec.email = ["jasonbyck@gmail.com"]
12
+ spec.description = %q{ Add RateLimit headers for Rack::Attack throttling }
13
+ spec.summary = %q{ Add RateLimit headers for Rack::Attack throttling }
14
+ spec.homepage = "https://github.com/jbyck/rack-attack-rate-limit"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'rack'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rack-test"
28
+
29
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::Attack::RateLimit do
4
+
5
+ include Rack::Test::Methods
6
+
7
+ let(:throttle) { 'rack_attack_throttle' }
8
+
9
+ let(:app) do
10
+ use_throttle = throttle
11
+ Rack::Builder.new {
12
+ use Rack::Attack::RateLimit, throttle: use_throttle
13
+ run lambda { |env| [200, {}, 'Hello, World!'] }
14
+ }.to_app
15
+
16
+ end
17
+
18
+
19
+ context 'Throttle data not present from Rack::Attack' do
20
+
21
+ before(:each) do
22
+ get '/'
23
+ end
24
+
25
+ it 'should not create RateLimit headers' do
26
+ last_response.header.key?('X-RateLimit-Limit').should be false
27
+ last_response.header.key?('X-RateLimit-Remaining').should be false
28
+ end
29
+
30
+ end
31
+
32
+ context 'Throttle data present from Rack::Attack' do
33
+
34
+ let(:request_limit) { (1..10000).to_a.sample }
35
+ let(:request_count) { (1..(request_limit-10)).to_a.sample }
36
+
37
+ let(:rack_attack_throttle_data) do
38
+ { "#{throttle}" => { count: request_count, limit: request_limit } }
39
+ end
40
+
41
+ before(:each) do
42
+ get "/", {}, { "#{Rack::Attack::RateLimit::RACK_ATTACK_KEY}" => rack_attack_throttle_data }
43
+ end
44
+
45
+ it 'should include RateLimit headers' do
46
+ last_response.header.key?('X-RateLimit-Limit').should be true
47
+ last_response.header.key?('X-RateLimit-Remaining').should be true
48
+ end
49
+
50
+ it 'should return correct rate limit in header' do
51
+ last_response.header['X-RateLimit-Limit'].to_i.should eq request_limit
52
+ end
53
+
54
+ it 'should return correct remaining calls in header' do
55
+ last_response.header['X-RateLimit-Remaining'].to_i.should eq (request_limit-request_count)
56
+ end
57
+ end
58
+
59
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + "../lib")
2
+
3
+ require 'rspec'
4
+ require 'rack/test'
5
+ require 'rack/attack/rate-limit'
6
+
7
+ RSpec::configure do |config|
8
+
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.formatter = :documentation
11
+ config.tty = true
12
+ config.color_enabled = true
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-attack-rate-limit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Byck
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack-test
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: ! ' Add RateLimit headers for Rack::Attack throttling '
95
+ email:
96
+ - jasonbyck@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .travis.yml
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/rack/attack/rate-limit.rb
108
+ - lib/rack/attack/rate-limit/version.rb
109
+ - rack-attack-rate-limit.gemspec
110
+ - spec/rack/attack/rate-limit_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/jbyck/rack-attack-rate-limit
113
+ licenses:
114
+ - MIT
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 1.8.23
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Add RateLimit headers for Rack::Attack throttling
137
+ test_files:
138
+ - spec/rack/attack/rate-limit_spec.rb
139
+ - spec/spec_helper.rb