Skip to content

Commit

Permalink
Activate Rack::Attack, add /system limits
Browse files Browse the repository at this point in the history
  • Loading branch information
Kagemaru committed Dec 30, 2023
1 parent 474a5d7 commit 7e93532
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# require "action_mailbox/engine"
# require "action_text/engine"

require 'rack/attack'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
Expand Down Expand Up @@ -39,6 +41,8 @@ class Application < Rails::Application
config.active_job.queue_adapter = :delayed_job
config.active_job.queue_name_prefix = "decidim_zuerich_#{Rails.env}"

config.middleware.use Rack::Attack

Raven.configure do |config|
config.dsn = ENV['SENTRY_DSN']
config.current_environment = ENV['SENTRY_CURRENT_ENV']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'rack/attack'
require 'ipaddr'

Rack::Attack.enabled = ENV.fetch('ENABLE_RACK_ATTACK', Rails.env.production?.to_s).in?(%w[true 1])
# Rack::Attack.enabled = ENV.fetch('ENABLE_RACK_ATTACK', Rails.env.production?.to_s).in?(%w[true 1])
Rack::Attack.enabled = true


safelist_ips = ENV.fetch('RACK_ATTACK_SAFELIST_IPS', '').split(',').map(&:strip)

Expand All @@ -12,10 +14,17 @@
Rack::Attack.safelist_ip(ip_or_subnet)
end

Rack::Attack.blocklist('secure admin logins') do |req|
Rack::Attack::Allow2Ban.filter(req.ip, maxretry: 5, findtime: 10.minutes, bantime: 1.hour) do
req.post? && req.path.include?('system')
end
end

ActiveSupport::Notifications.subscribe(/rack_attack/) do |name, start, finish, request_id, payload|
# request object available in payload[:request]
request = payload[:request]
Rails.logger.warn "RACK ATTACK #{name} - #{request.ip} - #{request.url}"
# require 'pry'; binding.pry
Rails.logger.warn "RACK ATTACK MATCH: #{name}: #{request.env['rack.attack.matched']} | ip: #{request.ip} | url: #{request.url}"
end

# Used for rack-attack throttling debugging
Expand Down

0 comments on commit 7e93532

Please sign in to comment.