/rack_attack.rb Secret
Last active
August 8, 2022 00:23
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rack::Attack.blocklist('block all outer routes ') do |request| | |
# Requests are blocked if the return value is truthy | |
routes_array = Rails.application.routes.routes.flat_map { |r| r.path.spec.to_s }.uniq.map { |path| path&.gsub('(.:format)', '') } | |
# allow assets url | |
if request.path.start_with?('/assets') | |
false | |
else | |
!routes_array.include?(request.path) | |
end | |
end | |
# display custom error page | |
Rack::Attack.blocklisted_response = lambda do |_env| | |
html = ActionView::Base.empty.render(file: 'public/500.html') | |
[403, {'Content-Type' => 'text/html'}, [html]] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment