rack-attack icon indicating copy to clipboard operation
rack-attack copied to clipboard

Not working for rails 5.0.1 api only app

Open vamsipavanmahesh opened this issue 9 years ago • 9 comments

I have put the rack-attack in the gem file. In the application.rb did config.middleware.use Rack::Attack

class Rack::Attack
  
  throttle('req/ip', :limit => 2, :period => 2.minutes) do |req|
    req.ip # unless req.path.start_with?('/assets')
  end

end

The above is config/intializers/rack-attack.rb

Is there something else I need to do here? I did the same thing with rails 4.2 app, it works fine there. But here it does not seem to work

vamsipavanmahesh avatar Apr 01 '17 10:04 vamsipavanmahesh

Well what's broken? Is there an error message that gors with it?

grepsedawk avatar Apr 01 '17 14:04 grepsedawk

Well with the above config, I should get 429 status code if I make more than 2 requests for 2 minutes. But it's not stopping the client from making more than 2 requests

vamsipavanmahesh avatar Apr 02 '17 03:04 vamsipavanmahesh

Have you tried restarting your rails server?

grepsedawk avatar Apr 02 '17 21:04 grepsedawk

Yes I did .. but still it's the same, do you mind telling if it's working for you.

vamsipavanmahesh avatar Apr 03 '17 04:04 vamsipavanmahesh

After playing, this worked for me:

config/initializers/rack-attack.rb:

class Rack::Attack
  Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new 

  throttle('api/ip', limit: 2, period: 2.minutes) do |req|
    req.ip
  end
end

grepsedawk avatar Apr 03 '17 04:04 grepsedawk

Looks like the docs do mention this, actually:

Optionally configure the cache store for throttling or fail2ban filtering

It is worded weird though.

grepsedawk avatar Apr 03 '17 04:04 grepsedawk

@vamsipavanmahesh usually you want to state that the above resolved your issue before you close it, so people stuffing the internet at a later date can use the same technique. (The only reason I know this resolved your issue is because I talked to you outside of GitHub)

grepsedawk avatar Apr 03 '17 16:04 grepsedawk

Okies, lesson learnt .. the above solution worked!!

vamsipavanmahesh avatar Apr 03 '17 16:04 vamsipavanmahesh

After playing, this worked for me:

config/initializers/rack-attack.rb:

class Rack::Attack
  Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new 

  throttle('api/ip', limit: 2, period: 2.minutes) do |req|
    req.ip
  end
end

I Implement the solution and working fine for me Thanks

gambhirmakwana1992 avatar Jan 07 '21 08:01 gambhirmakwana1992