rate_attack 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 62d8123378e1d10f1fb64cf0ae7a2077232d2e9091e591afffdb3b35dd35aa51
4
+ data.tar.gz: 4ac90f4aebcaf8cbaea04db026eec4193ccefd339c440e32d7ea6c9777904052
5
+ SHA512:
6
+ metadata.gz: 07facb40efc82a0b3de414ad8580bebe004e416b31f5d9955c1b624dcb8bad827bcd1bc7e1ffa84c1214313a03e2580a9732c4ce7b8369819c8070dca5079fa0
7
+ data.tar.gz: 57e33ad628dcae24c622c45140444e403a015fe5ed8628f97e1dfdb988ba0091d67151bcdc30eea5862e633e1f94e62bacd94b31daeff5770a617bf5d65ec8a2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,117 @@
1
+ require:
2
+ - rubocop-performance
3
+
4
+ inherit_mode:
5
+ merge:
6
+ - Exclude
7
+
8
+ AllCops:
9
+ TargetRubyVersion: 2.7
10
+ DisabledByDefault: true
11
+ Exclude:
12
+ - "examples/instrumentation.rb"
13
+ - "gemfiles/**/*"
14
+
15
+ Bundler:
16
+ Enabled: true
17
+
18
+ Gemspec:
19
+ Enabled: true
20
+
21
+ Layout:
22
+ Enabled: true
23
+
24
+ Layout/EmptyLinesAroundAttributeAccessor: # (0.83)
25
+ Enabled: true
26
+
27
+ Layout/SpaceAroundMethodCallOperator: # (0.82)
28
+ Enabled: true
29
+
30
+ Layout/LineLength:
31
+ Max: 120
32
+
33
+ Lint:
34
+ Enabled: true
35
+
36
+ Lint/DeprecatedOpenSSLConstant: # (0.84)
37
+ Enabled: true
38
+
39
+ Lint/RaiseException: # (0.81)
40
+ Enabled: true
41
+
42
+ Lint/StructNewOverride: # (0.81)
43
+ Enabled: true
44
+
45
+ Naming:
46
+ Enabled: true
47
+ Exclude:
48
+ - "lib/rack/attack/path_normalizer.rb"
49
+
50
+ Performance:
51
+ Enabled: true
52
+
53
+ Security:
54
+ Enabled: true
55
+
56
+ Style/BlockDelimiters:
57
+ Enabled: true
58
+ IgnoredMethods: [] # Workaround rubocop bug: https://github.com/rubocop-hq/rubocop/issues/6179
59
+
60
+ Style/ClassAndModuleChildren:
61
+ Enabled: true
62
+ Exclude:
63
+ - "spec/**/*"
64
+
65
+ Style/ConditionalAssignment:
66
+ Enabled: true
67
+
68
+ Style/Encoding:
69
+ Enabled: true
70
+
71
+ Style/ExpandPathArguments:
72
+ Enabled: true
73
+
74
+ Style/EmptyMethod:
75
+ Enabled: true
76
+
77
+ Style/FrozenStringLiteralComment:
78
+ Enabled: true
79
+
80
+ Style/HashSyntax:
81
+ Enabled: true
82
+
83
+ Style/MultilineTernaryOperator:
84
+ Enabled: true
85
+
86
+ Style/NestedTernaryOperator:
87
+ Enabled: true
88
+
89
+ Style/OptionalArguments:
90
+ Enabled: true
91
+
92
+ Style/ParallelAssignment:
93
+ Enabled: true
94
+
95
+ Style/RaiseArgs:
96
+ Enabled: true
97
+
98
+ Style/RedundantBegin:
99
+ Enabled: true
100
+
101
+ Style/RedundantFreeze:
102
+ Enabled: true
103
+
104
+ Style/RedundantPercentQ:
105
+ Enabled: true
106
+
107
+ Style/RedundantSelf:
108
+ Enabled: true
109
+
110
+ Style/Semicolon:
111
+ Enabled: true
112
+
113
+ Style/SingleLineMethods:
114
+ Enabled: true
115
+
116
+ Style/SpecialGlobalVars:
117
+ Enabled: true
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
data/Appraisals ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise "rack_2" do
4
+ gem "rack", "~> 2.0"
5
+ end
6
+
7
+ appraise 'rails_6-1' do
8
+ gem 'railties', '~> 6.1.0'
9
+ end
10
+
11
+ appraise 'rails_6-0' do
12
+ gem 'railties', '~> 6.0.0'
13
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ [@wilkosz]: https://github.com/wilkosz
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at joshua@wilkosz.com.au. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rate-attack.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,121 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rate_attack (0.0.1)
5
+ rack (>= 2.0, < 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actionpack (6.1.3.1)
11
+ actionview (= 6.1.3.1)
12
+ activesupport (= 6.1.3.1)
13
+ rack (~> 2.0, >= 2.0.9)
14
+ rack-test (>= 0.6.3)
15
+ rails-dom-testing (~> 2.0)
16
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
17
+ actionview (6.1.3.1)
18
+ activesupport (= 6.1.3.1)
19
+ builder (~> 3.1)
20
+ erubi (~> 1.4)
21
+ rails-dom-testing (~> 2.0)
22
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
23
+ activesupport (6.1.3.1)
24
+ concurrent-ruby (~> 1.0, >= 1.0.2)
25
+ i18n (>= 1.6, < 2)
26
+ minitest (>= 5.1)
27
+ tzinfo (~> 2.0)
28
+ zeitwerk (~> 2.3)
29
+ appraisal (2.4.0)
30
+ bundler
31
+ rake
32
+ thor (>= 0.14.0)
33
+ ast (2.4.2)
34
+ builder (3.2.4)
35
+ concurrent-ruby (1.1.8)
36
+ crass (1.0.6)
37
+ diff-lcs (1.4.4)
38
+ erubi (1.10.0)
39
+ i18n (1.8.10)
40
+ concurrent-ruby (~> 1.0)
41
+ loofah (2.9.1)
42
+ crass (~> 1.0.2)
43
+ nokogiri (>= 1.5.9)
44
+ method_source (1.0.0)
45
+ minitest (5.14.4)
46
+ nokogiri (1.11.3-x86_64-darwin)
47
+ racc (~> 1.4)
48
+ parallel (1.20.1)
49
+ parser (3.0.1.0)
50
+ ast (~> 2.4.1)
51
+ racc (1.5.2)
52
+ rack (2.2.3)
53
+ rack-test (1.1.0)
54
+ rack (>= 1.0, < 3)
55
+ rails-dom-testing (2.0.3)
56
+ activesupport (>= 4.2.0)
57
+ nokogiri (>= 1.6)
58
+ rails-html-sanitizer (1.3.0)
59
+ loofah (~> 2.3)
60
+ railties (6.1.3.1)
61
+ actionpack (= 6.1.3.1)
62
+ activesupport (= 6.1.3.1)
63
+ method_source
64
+ rake (>= 0.8.7)
65
+ thor (~> 1.0)
66
+ rainbow (3.0.0)
67
+ rake (13.0.3)
68
+ regexp_parser (2.1.1)
69
+ rexml (3.2.5)
70
+ rspec (3.10.0)
71
+ rspec-core (~> 3.10.0)
72
+ rspec-expectations (~> 3.10.0)
73
+ rspec-mocks (~> 3.10.0)
74
+ rspec-core (3.10.1)
75
+ rspec-support (~> 3.10.0)
76
+ rspec-expectations (3.10.1)
77
+ diff-lcs (>= 1.2.0, < 2.0)
78
+ rspec-support (~> 3.10.0)
79
+ rspec-mocks (3.10.2)
80
+ diff-lcs (>= 1.2.0, < 2.0)
81
+ rspec-support (~> 3.10.0)
82
+ rspec-support (3.10.2)
83
+ rubocop (1.12.1)
84
+ parallel (~> 1.10)
85
+ parser (>= 3.0.0.0)
86
+ rainbow (>= 2.2.2, < 4.0)
87
+ regexp_parser (>= 1.8, < 3.0)
88
+ rexml
89
+ rubocop-ast (>= 1.2.0, < 2.0)
90
+ ruby-progressbar (~> 1.7)
91
+ unicode-display_width (>= 1.4.0, < 3.0)
92
+ rubocop-ast (1.4.1)
93
+ parser (>= 2.7.1.5)
94
+ rubocop-performance (1.10.2)
95
+ rubocop (>= 0.90.0, < 2.0)
96
+ rubocop-ast (>= 0.4.0)
97
+ ruby-progressbar (1.11.0)
98
+ thor (1.1.0)
99
+ timecop (0.9.4)
100
+ tzinfo (2.0.4)
101
+ concurrent-ruby (~> 1.0)
102
+ unicode-display_width (2.0.0)
103
+ zeitwerk (2.4.2)
104
+
105
+ PLATFORMS
106
+ ruby
107
+
108
+ DEPENDENCIES
109
+ appraisal (~> 2.4)
110
+ bundler (>= 1.17, < 3.0)
111
+ rack-test (~> 1.0)
112
+ railties (>= 6.0)
113
+ rake (~> 13.0)
114
+ rate_attack!
115
+ rspec (~> 3.0)
116
+ rubocop (~> 1.12.1)
117
+ rubocop-performance (~> 1.10.1)
118
+ timecop (~> 0.9.4)
119
+
120
+ BUNDLED WITH
121
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Joshua Wilkosz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 wilkosz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # RateAttack
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rate_attack`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rate_attack'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rate_attack
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rateattack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rate-attack/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Rate::Attack project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rate-attack/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rate_attack"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,149 @@
1
+ require 'rack'
2
+
3
+ require "rate_attack/version"
4
+ require 'rate_attack/cache'
5
+ require 'rate_attack/configuration'
6
+ require 'rate_attack/request'
7
+ require 'rate_attack/railtie' if defined?(::Rails)
8
+
9
+ class RateAttack
10
+ class Error < StandardError; end
11
+
12
+ class << self
13
+ attr_accessor :enabled
14
+ attr_reader :configuration
15
+
16
+ def cache
17
+ @cache ||= Cache.new
18
+ end
19
+ end
20
+
21
+ # Set defaults
22
+ @enabled = true
23
+ @configuration = Configuration.new
24
+
25
+ attr_reader :configuration
26
+
27
+ def initialize(app)
28
+ @app = app
29
+ @configuration = self.class.configuration
30
+ end
31
+
32
+ def call(env)
33
+ return @app.call(env) if !self.class.enabled || env["rate_attack.called"]
34
+
35
+ env["rate_attack.called"] = true
36
+
37
+ # request = RateAttack::Request.new(env)
38
+ s_path = sanitized_path(env['PATH_INFO'])
39
+ if s_path =~ /rateattack/
40
+ handle_rateattack_ratelimit_info(env)
41
+ else
42
+ r_path = Regexp.new(s_path)
43
+ route = wrapped_routes.filter { sanitized_path(_1.path) =~ r_path }&.first
44
+ route ? handle_rateattack_request(env, route) : @app.call(env)
45
+ end
46
+ end
47
+
48
+ def rateattack_request_key(env, info)
49
+ request = ActionDispatch::Request.new env
50
+ keys = []
51
+ if info[:uniqueness].is_a?(Array)
52
+ keys =
53
+ info[:uniqueness].map do |item|
54
+ item.to_s == 'ip' ? guess_ip(request) : env[item.to_s]
55
+ end
56
+ end
57
+ keys.insert(0, 'RateAttack')
58
+ keys.push(info[:action] || sanitized_path(env['PATH_INFO']))
59
+ keys.join('::')
60
+ end
61
+
62
+ def get_route_ratelimit_info(env, rateattack_config)
63
+ count =
64
+ Rails
65
+ .cache
66
+ .read(rateattack_request_key(env, rateattack_config), raw: true)
67
+ .to_i
68
+ {
69
+ limit: rateattack_config[:limit],
70
+ count: count,
71
+ remaining: (rateattack_config[:limit] - count),
72
+ action: (rateattack_config[:action] || sanitized_path(env['PATH_INFO'])),
73
+ }
74
+ end
75
+
76
+ def sanitized_path(path)
77
+ path.gsub(%r{^\/}, '').gsub(/\(.:format\)/, '')
78
+ end
79
+
80
+ def handle_rateattack_ratelimit_info(env)
81
+ ra_ratelimit_infos =
82
+ wrapped_routes.map do
83
+ get_route_ratelimit_info(env, _1.defaults[:rateattack])
84
+ end
85
+
86
+ [200, {}, StringIO.new({ data: ra_ratelimit_infos }.to_json)]
87
+ end
88
+
89
+ def handle_rateattack_request(env, route)
90
+ limit_key = rateattack_request_key(env, route.defaults[:rateattack])
91
+ current_info = get_route_ratelimit_info(env, route.defaults[:rateattack])
92
+
93
+ if (current_info[:limit] - current_info[:count]) <= 0
94
+ return [
95
+ 429,
96
+ set_rate_limit_and_merge_headers(current_info),
97
+ StringIO.new(I18n.t('general.ratelimit.exceeded'))
98
+ ]
99
+ end
100
+
101
+ @status, @headers, @response = @app.call(env)
102
+
103
+ if @status >= 200 && @status <= 400
104
+ current_info[:count] =
105
+ Rails.cache.increment(
106
+ limit_key,
107
+ expires_in: route.defaults[:rateattack][:duration],
108
+ )
109
+ end
110
+
111
+ [
112
+ @status,
113
+ set_rate_limit_and_merge_headers(current_info, headers: @headers),
114
+ @response,
115
+ ]
116
+ end
117
+
118
+ def wrapped_routes
119
+ Rails.application.routes.routes.filter { _1.defaults[:rateattack] }.compact
120
+ .map { ActionDispatch::Routing::RouteWrapper.new(_1) }
121
+ end
122
+
123
+ def remove_prefix_postfix_slash(str)
124
+ str.gsub(%r{^\/}, '').gsub(%r{\/$}, '')
125
+ end
126
+
127
+ def set_rate_limit_and_merge_headers(rateattack_info, headers: {})
128
+ headers.merge(
129
+ {
130
+ I18n.t('general.ratelimit.headers.remaining') => [
131
+ rateattack_info[:limit] - rateattack_info[:count],
132
+ 0,
133
+ ].max.to_s,
134
+ I18n.t('general.ratelimit.headers.limit') =>
135
+ rateattack_info[:limit].to_s,
136
+ },
137
+ )
138
+ end
139
+
140
+ def guess_ip(request)
141
+ ip = request.remote_ip || request.ip
142
+ ip = '127.0.0.1' if ip == '::1'
143
+ begin
144
+ IPAddress.valid?(ip)
145
+ rescue StandardError
146
+ nil
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RateAttack
4
+ class Cache
5
+ attr_accessor :prefix
6
+
7
+ def initialize
8
+ self.store = ::Rails.cache if defined?(::Rails.cache)
9
+ @prefix = 'rate_attack'
10
+ end
11
+
12
+ attr_reader :store
13
+
14
+ def store=(store)
15
+ @store = store
16
+ end
17
+
18
+ def count(unprefixed_key, period)
19
+ key, expires_in = key_and_expiry(unprefixed_key, period)
20
+ do_count(key, expires_in)
21
+ end
22
+
23
+ def read(unprefixed_key)
24
+ store.read("#{prefix}:#{unprefixed_key}")
25
+ end
26
+
27
+ def write(unprefixed_key, value, expires_in)
28
+ store.write("#{prefix}:#{unprefixed_key}", value, expires_in: expires_in)
29
+ end
30
+
31
+ private
32
+
33
+ def do_count(key, expires_in)
34
+ result = store.increment(key, 1, expires_in: expires_in)
35
+ # NB: Some stores return nil when incrementing uninitialized values
36
+ if result.nil?
37
+ store.write(key, 1, expires_in: expires_in)
38
+ end
39
+ result || 1
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipaddr"
4
+
5
+ class RateAttack
6
+ class Configuration
7
+
8
+ def initialize
9
+ set_defaults
10
+ end
11
+
12
+ def clear_configuration
13
+ set_defaults
14
+ end
15
+
16
+ private
17
+
18
+ def set_defaults
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RateAttack
4
+ class Railtie < ::Rails::Railtie
5
+ initializer "rate_attack.middleware" do |app|
6
+ app.middleware.use(RateAttack)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # RateAttack::Request is the same as ::Rack::Request by default.
4
+ class RateAttack
5
+ class Request < ::Rack::Request
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class RateAttack
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,45 @@
1
+ require_relative 'lib/rate_attack/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rate_attack"
5
+ spec.version = RateAttack::VERSION
6
+ spec.license = "MIT"
7
+
8
+ spec.authors = ["wilkosz"]
9
+ spec.email = ["joshua@wilkosz.com.au"]
10
+
11
+ spec.summary = %q{Throttle feature usage}
12
+ spec.description = %q{A rack middleware for ratelimiting and tracking feature utilization}
13
+ spec.homepage = "https://github.com/wilkosz/rateattack-rails"
14
+
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.2")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage + "/blob/master/CHANGELOG.md"
21
+ spec.metadata["changelog_uri"] = spec.homepage
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_runtime_dependency 'rack', ">= 2.0", "< 3"
34
+
35
+ spec.add_development_dependency "appraisal", '~> 2.4'
36
+ spec.add_development_dependency "bundler", ">= 1.17", "< 3.0"
37
+ spec.add_development_dependency 'rack-test', "~> 1.0"
38
+ spec.add_development_dependency 'rake', "~> 13.0"
39
+ spec.add_development_dependency 'rspec', "~> 3.0"
40
+ spec.add_development_dependency "rubocop", "~> 1.12.1"
41
+ spec.add_development_dependency "rubocop-performance", "~> 1.10.1"
42
+ spec.add_development_dependency "timecop", "~> 0.9.4"
43
+
44
+ spec.add_development_dependency 'railties', '>= 6.0'
45
+ end
metadata ADDED
@@ -0,0 +1,220 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rate_attack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - wilkosz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: appraisal
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.4'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.4'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '1.17'
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '3.0'
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '1.17'
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '3.0'
67
+ - !ruby/object:Gem::Dependency
68
+ name: rack-test
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.0'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.0'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '13.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '13.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '3.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '3.0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: rubocop
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: 1.12.1
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: 1.12.1
123
+ - !ruby/object:Gem::Dependency
124
+ name: rubocop-performance
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: 1.10.1
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: 1.10.1
137
+ - !ruby/object:Gem::Dependency
138
+ name: timecop
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: 0.9.4
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: 0.9.4
151
+ - !ruby/object:Gem::Dependency
152
+ name: railties
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '6.0'
158
+ type: :development
159
+ prerelease: false
160
+ version_requirements: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '6.0'
165
+ description: A rack middleware for ratelimiting and tracking feature utilization
166
+ email:
167
+ - joshua@wilkosz.com.au
168
+ executables: []
169
+ extensions: []
170
+ extra_rdoc_files: []
171
+ files:
172
+ - ".gitignore"
173
+ - ".rspec"
174
+ - ".rubocop.yml"
175
+ - ".travis.yml"
176
+ - Appraisals
177
+ - CHANGELOG.md
178
+ - CODE_OF_CONDUCT.md
179
+ - Gemfile
180
+ - Gemfile.lock
181
+ - LICENSE
182
+ - LICENSE.txt
183
+ - README.md
184
+ - Rakefile
185
+ - bin/console
186
+ - bin/setup
187
+ - lib/rate_attack.rb
188
+ - lib/rate_attack/cache.rb
189
+ - lib/rate_attack/configuration.rb
190
+ - lib/rate_attack/railtie.rb
191
+ - lib/rate_attack/request.rb
192
+ - lib/rate_attack/version.rb
193
+ - rate_attack.gemspec
194
+ homepage: https://github.com/wilkosz/rateattack-rails
195
+ licenses:
196
+ - MIT
197
+ metadata:
198
+ homepage_uri: https://github.com/wilkosz/rateattack-rails
199
+ source_code_uri: https://github.com/wilkosz/rateattack-rails/blob/master/CHANGELOG.md
200
+ changelog_uri: https://github.com/wilkosz/rateattack-rails
201
+ post_install_message:
202
+ rdoc_options: []
203
+ require_paths:
204
+ - lib
205
+ required_ruby_version: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: 2.7.2
210
+ required_rubygems_version: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ requirements: []
216
+ rubygems_version: 3.1.4
217
+ signing_key:
218
+ specification_version: 4
219
+ summary: Throttle feature usage
220
+ test_files: []