Commit graph

40 commits

Author SHA1 Message Date
Vincent Boisard
c34bace773 style: remove extraneous whitespace 2016-01-07 22:45:11 +01:00
Vincent Boisard
297ef4a2ae Merge branch 'master' of github.com:kickstarter/rack-attack 2015-12-29 10:10:36 +01: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
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
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
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
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
hakanensari
5d72c6e5f9 Move individual proxy classes to separate files 2014-04-15 16:19:43 +01:00
Tristan Dunn
16f1cfc578 Add a custom request class to allow for helper methods.
Fixes #58.
2014-04-04 14:41:59 -05:00
Steve Hodgkiss
93421efa5a Tidy up defaults. We don't need to use ||= because this runs when the class gets loaded, and we won't have user supplied defaults yet. 2014-04-02 17:12:28 +11:00
Steve Hodgkiss
332dd4ff9e Delegate to class methods with forwardable 2014-04-02 17:08:46 +11:00
Steve Hodgkiss
c3a077442a Make Rack::Attack a class and Rack::Attack.new return an instance of that class, rather than Rack::Attack. 2014-03-28 14:53:51 +11:00
Carsten Zimmermann
1095f85242 Change response body to 'Forbidden' 2014-02-06 23:29:44 +01:00
Carsten Zimmermann
97a43f7e66 Return 403 Forbidden instead of 401
401 Unauthorized suggests that the requests can be
retried with appropriate credentials. 403 explicitly
states that the request should not be repeated.

See #41
2014-02-06 21:32:51 +01:00
Jordan Moncharmont
ef59c5182a Allow2Ban
An alternate to fail2ban that allows clients until they hit the
thresholds, then blocks them.  Think of it like a throttle where you can
block for more than one period.
2013-09-27 17:18:52 -07:00
Alex Volkovitsky & Sachin Maharjan
cfbee2c552 Use correct HTTP status codes for blackist and throttle 2013-06-28 10:55:28 -07:00
madlep
3f1c98a868 Fail2Ban helper
based on gist from @ktheory https://gist.github.com/ktheory/5723534

Modified slightly to use fail2ban `filter` terminology to simplify
Rack::Attack initializer configuration (only one block is requred for
this approach instead of 2)
2013-06-12 15:51:13 +10:00
madlep
6c259ea9be delegate Redis custom logic to StoreProxy
this removes ugly `if redis blah` code from cache
2013-06-12 15:03:39 +10:00
Aaron Suggs
917d47758e Add newlines to default responses 2013-03-04 21:06:15 -05:00
Aaron Suggs
27a13f6971 Clarify algorithm 2013-01-17 12:06:16 -05:00
Aaron Suggs
0ca7b8cfac whitespace 2013-01-17 12:06:16 -05:00
Aaron Suggs
b577dae1c0 use each_value instead of each 2013-01-11 14:32:20 -05:00
Aaron Suggs
80367e1e4a Add Rack::Attack.track.
track will fire notifications, but not alter request processing
2013-01-10 19:02:49 -05:00
Aaron Suggs
eeb238b78d Use autoload instead of require 2013-01-10 18:36:31 -05:00
Aaron Suggs
6784f21612 Better cache interface 2012-08-06 14:16:08 -04:00
Aaron Suggs
9c4dccac48 Make cache & notifier attr_accessors
Easier config for non-rails apps
2012-08-06 12:17:31 -04:00
Aaron Suggs
8e59e84e00 Response header values must be strings 2012-08-02 12:15:16 -04:00
Aaron Suggs
ccdc1f993a Change instrumentation API for simpler notifications 2012-07-30 15:44:22 -04:00
Aaron Suggs
61a046a203 Cleanup instrumentation interface 2012-07-30 09:34:30 -04:00
Aaron Suggs
cf49b00c00 DRY up initial values 2012-07-28 22:09:56 -04:00
Aaron Suggs
c90a0182eb Allow custom responses based on env 2012-07-28 19:51:24 -04:00
Aaron Suggs
dc2e402812 Initial working version 2012-07-27 17:40:11 -04:00
Aaron Suggs
e166e87fb9 Add throttle support 2012-07-27 17:22:49 -04:00
Aaron Suggs
9284a08cc3 Whitelists support 2012-07-26 17:29:09 -04:00
Aaron Suggs
7fab5df499 WIP 2012-07-24 19:59:46 -04:00
Aaron Suggs
140ea86b7c Initial commit 2012-07-24 19:40:55 -04:00