OneChillDude
OneChillDude

Reputation: 8006

Block IP's In a rails app on heroku

I have a rails app on heroku, and there are about 10 requests per second, which is strange because no one is using the app right now. All of the requests are for URI's that are clearly attempting to exploit security vulnerabilities, for example.

http://myapp.com/etc/passwd

and things like that.

How can I block this person from accessing my app? Is there a quick fix for this?

Upvotes: 5

Views: 3357

Answers (2)

user1322092
user1322092

Reputation: 4270

My response is a little late but since you had later asked about dynamically adding IPs, might as well share.

From this Github Issue. Perhaps you can use the presence/existence of an IP in your cache store to determine if it should be blocked (vs manually adding the IP and re-deploying the app).

Rack::Attack.blacklist('block <ip>') do |req|
  # if variable `block <ip>` exists in cache store, then we'll block the request
  Rails.cache.fetch("block #{req.ip}").present?
end

From the app or from the console, you can write to your cache store.

Rails.cache.write('block 1.2.3.4', true, expires_in: 5.days)

Upvotes: 3

catsby
catsby

Reputation: 11342

Perhaps rack-attack will do what you want?

# Block requests from 1.2.3.4
Rack::Attack.blacklist('block 1.2.3.4') do |req|
  # Request are blocked if the return value is truthy
  '1.2.3.4' == req.ip
end

Upvotes: 7

Related Questions