Commit graph

116 commits

Author SHA1 Message Date
Gonzalo Rodriguez
6af29fb44e Avoid 'defined?' buggy behavior in ruby 2.5.0. Fixes #253
'defined?' is buggy in ruby 2.5.0, which under certain circumstances
users using rack-attack can hit. See issue #253.

I reported (https://bugs.ruby-lang.org/issues/14407) and
fixed (https://github.com/ruby/ruby/pull/1800) the issue in
ruby already, but i guess i would take some time before there's
a new ruby release including that fix.

So for now we would need to circumvent this bug by using
'const_defined?' instead of 'defined?' for this particular case.

More details:

Anyone using:
  * ruby 2.5.0
  * redis
  * rack-attack without redis-store and using at least one throttle
  * having a toplevel class named Store

will hit this ruby 2.5.0 bug https://bugs.ruby-lang.org/issues/14407

That's because of the following buggy behavior of 'defined?' under ruby
2.5:

```
$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

$ irb
> class Redis
> end
=> nil
> class Store
> end
=> nil
> defined?(::Redis::Store)
=> "constant"
> ::Redis::Store
  NameError (uninitialized constant Redis::Store
    Did you mean?  Store)
```
2018-01-29 12:25:18 -03:00
Tieg Zaharia
d7cc49117f
Merge pull request #267 from grzuy/freeze_constants
Do not allow mutating constants
2018-01-25 14:51:06 -05:00
Gonzalo Rodriguez
b7eb7851cf Cleanup unnecessary self references 2018-01-25 15:52:20 -03:00
Gonzalo Rodriguez
7286517c2f Do not allow mutating constants 2018-01-25 12:22:40 -03:00
Corey Farwell
f91f3a403c
Merge pull request #264 from grzuy/drop_support_for_rails_3
Drop support for unmaintaned Rails 3
2018-01-23 17:45:29 -05:00
Gonzalo Rodriguez
218a320a3d Fixes warning instead of hiding it
Warning was: 'warning: instance variable @data not initialized'
2018-01-23 16:56:42 -03:00
Gonzalo Rodriguez
34ee066eac Drop support for Rails 3 2018-01-23 16:12:16 -03:00
Eric Garside
5a7114e126 Fixing whitespace issue 2018-01-05 17:38:19 -05:00
Mattias Lundell
c37b477d15 Fix alignment of columns 2017-10-11 13:55:19 +02:00
Marc G Gauthier
98b0ab866d Typo in comment 2017-02-21 17:07:26 +01:00
Aaron Suggs
ebfa081e6d Fix args to deprecated methods
Fixes #197
2016-08-11 13:39:22 -04:00
Aaron Suggs
e5cf56ae46 v5.0.0
No beta.

And update Changelog (for the last time)
2016-08-09 12:59:43 -04:00
Aaron Suggs
e8433f7693 Bump to version v5.0.0.beta1 2016-07-04 21:42:44 -04:00
Aaron Suggs
f5f08d56e5 More safelist/blocklist refactoring
- Add Rack::Attack namespace to deprecation warning.
- Add deprecated Rack::Attack.blacklisted_response attr methods.
2016-07-04 21:42:41 -04:00
Renée Hendricksen
e1a0c804e1 suggesting changing whitelist/blacklist language to less controversial safelist/blocklist language
add deprication warnings

fix the method signatures
2016-07-01 21:44:45 -04:00
Aaron Suggs
cf89457ced bump v4.4.1 2016-02-16 17:37:59 -05:00
Aaron Suggs
9d90610bbe Refactor StoreProxy to avoid autoloading MemCacheStore
In v4.4.0, checking `defined?(ActiveSupport::Cache::MemCacheStore)`
could trigger an error loading dalli, which isn’t needed.

This fixes that bug, and prevents similar bugs by checking
`store.class.to_s` rather than `defined?(klass) && store.is_a?(klass)`.

Writing an automated test to ensure that dalli is truly optional is
difficult, but I was able to recreate the dalli load error in v4.4.0 by
running:

    gem uninstall dalli
    ruby -Ilib -ractive_support/all -ractive_support/cache/redis_store
-rrack/attack -e 'p Rack::Attack::StoreProxy.build(Redis::Store.new)'

Fixes #163
2016-02-16 16:59:24 -05:00
Aaron Suggs
d8b9e2f111 v4.4.0
Update changelog
2016-02-10 15:05:43 -05:00
Vincent Boisard
585d1fd02c Revert "refactor unwieldy Rack::Attack::StoreProxy.build method"
This reverts commit 8d124d868e.
2016-01-12 11:45:44 +01:00
Vincent Boisard
c34bace773 style: remove extraneous whitespace 2016-01-07 22:45:11 +01:00
Vincent Boisard
8d124d868e refactor unwieldy Rack::Attack::StoreProxy.build method 2016-01-07 21:16:35 +01:00
Vincent Boisard
32df84df54 fix: check whether client class is defined before checking client itself 2015-12-29 10:19:13 +01:00
Vincent Boisard
297ef4a2ae Merge branch 'master' of github.com:kickstarter/rack-attack 2015-12-29 10:10:36 +01:00
Aaron Suggs
4ec58e36de Version v4.3.1 2015-12-18 11:43:14 -05:00
Aaron Suggs
76c2e31430 Normalize request paths when using Rails' ActionDispatch
The issue
---

When using rack-attack with a rails app, developers expect the request
path to be normalized. In particular, trailing slashes are stripped so
a request path "/login/" becomes "/login" by the time you're in
ActionController.

Since Rack::Attack runs before ActionDispatch, the request path is not
yet normalized. This can cause throttles and blacklists to not work as
expected.

E.g., a throttle:

    throttle('logins', ...) {|req| req.path == "/login" }

would not match a request to '/login/', though Rails would route
'/login/' to the same '/login' action.

The solution
---

This patch looks if ActionDispatch's request normalization is loaded,
and if so, uses it to normalize the path before processing throttles,
blacklists, etc.

If it's not loaded, the request path is not modified.

Credit
---
Thanks to Andres Riancho at Include Security for reporting this issue.
2015-12-18 11:12:11 -05:00
Vincent Boisard
d880bd88e0 fix: workaround MemCacheClient + MemCache backend by using a dedicated proxy 2015-12-16 16:57:54 +01:00
Vincent Boisard
faa0638719 fix: Do not attempt to process Memcache clients with DalliProxy 2015-12-08 18:55:06 +01:00
Vincent Boisard
397a7ce7b4 feature: support for ActiveSupport::MemCacheStore 2015-12-08 10:53:53 +01:00
Stan Hu
91947b83a4 Support the ability to reset Fail2Ban count and ban flag
Closes #113
2015-05-22 09:47:32 -04:00
Aaron Suggs
0ec73d956b Merge pull request #119 from stanhu/add-delete-interface
Support delete method for Redis
2015-04-16 14:11:58 -04:00
Aaron Suggs
1dd29f66b8 Update changelog for #118; bump to v4.3.0 2015-04-16 14:06:46 -04:00
Stan Hu
ff15447f3a Support delete method for Redis 2015-03-15 12:13:44 -07:00
Stan Hu
dc1b6bf687 Pass raw: true to prevent Ruby de/serialization. This is to make it possible
to implement something like:

```store.write(key, 0, :expires_in => expires_in)```

See #113
2015-03-15 07:18:18 -07:00
Aaron Suggs
f9b7721e59 bump version 4.2.0 2014-10-26 15:33:09 -04:00
Aaron Suggs
e7efe1cf5f Merge pull request #91 from gsamokovarov/call-responses
Invoke Rack:::{blacklisted,throttled}_response with #call
2014-10-02 14:25:42 -04:00
Aaron Suggs
a1cedbbc7e Merge pull request #93 from gsamokovarov/dont-rescue-nil
Avoid rescue nil in the default throttled response
2014-09-26 15:07:35 -04:00
Genadi Samokovarov
a161176142 Invoke {blacklisted,throttled}_response with #call
I have a response which is a class. While I can still have my class
implement `#[]`, it does look a bit off. On the other side, having
objects, responding to #call, that are not procs is pretty common.

So I propose to invoke the responses with `#call` to let users override
it with response objects, that respond to `#call` instead of `#[]`.
2014-09-25 23:21:02 +02:00
Genadi Samokovarov
f737dbb78c Avoid rescue nil in the default throttled response
It has a couple of cons:

1. If we slip a typo in the whole line, we won't easily catch it.  Can
   you guys spot the problem problem in the following line? Chasing such
   issues is quite tricky.

```ruby
retry_after = evn['rack.attack.match_data'][:period] rescue nil
```

2. Throwing and catching an exception is quite slower than a new hash
   allocation, so there is a speed benefit too.

We are guaranteed from Rack that env is a `Hash`, so we can even use
`Hash#fetch`.

```ruby
retry_after = env.fetch('rack.attack.match_data', {})[:period]
```

This reads better, but always allocates the default value hash, when the
other version allocates it only when needed. If you prefer `Hash#fetch`,
I'm fine with that, as long as we avoid `rescue nil`.
2014-09-25 18:35:53 +02:00
Genadi Samokovarov
d9a5587676 Allow throttle period option to be a proc
I need to filter requests on a period I need to get dynamically out of
information I have in the requests. Currently, I can work out the limit,
as it can be a `Proc`, however I can't do that with the period.

This PR adds support for that. Tried to do it in a way that doesn't
brake backwards compatibility, as periods are coerced to numbers during
`Rack::Throttle` initialization.
2014-09-19 19:32:39 +02:00
Aaron Suggs
1db5a00431 version 4.1.1 2014-09-08 21:34:28 -04:00
Aaron Suggs
074e8e5aa4 Add 1 second buffer to expiry to correct throttles
Fixes #69.

There was a race condition when `Time.now.to_i` changes between when
`epoch_time` is computed in line 18, and the cache request is made (and
the `key` is expired).

I.e., a throttle check starts at t0, but doesn’t reach the cache until
t1, the cache will have expired the throttle count. The request will
likely be allowed, even if the request exceeded the limit.

This has the effect of keeping keys in cache about 1 second longer than
strictly necessary. But the extra cache space seems like a good
trade-off for correct throttling.
2014-09-02 14:30:57 -04:00
Aaron Suggs
7820986d9c Merge pull request #74 from zmillman/readme-request
Improve documentation of Rack::Attack::Request
2014-08-01 14:43:53 -04:00
Zach Millman
5c78f84be2 Improve documentation of Rack::Attack::Request
https://github.com/kickstarter/rack-attack/issues/73
2014-06-17 11:37:25 -07:00
Aaron Suggs
1a3cddb468 Prep for v4.1.0 2014-05-22 13:59:26 -04:00
Aaron Suggs
105c3ba575 Merge pull request #67 from chiliburger/track_only
Limit and period options for track
2014-05-22 13:55:54 -04:00
Paul Coates
e8d98a7ad3 Changed track checker to track filter. Made track filter tests more clear. 2014-05-22 10:11:23 -07:00
Aaron Suggs
6f7bd66dfa Default responses: add Content-Type header
Fixes #68
2014-05-21 18:35:28 -04:00
Paul Coates
1ebe1c3517 Added limit and period options to track. Delegates [] to Throttle if they are present otherwise Check. 2014-05-19 11:11:01 -07:00
Aaron Suggs
f9eabed6d4 Rack::Attack.clear! resets tracks
Fixes flaky tests like
https://travis-ci.org/kickstarter/rack-attack/builds/25438931
2014-05-18 20:46:11 -04:00
Aaron Suggs
0e599a4dd6 version 4.0.1 2014-05-14 13:37:30 -04:00