undefined method env for Hash
Since I updated rack-attack version to version 6.0.0, I get this error: undefined method env for #<Hash:0x0000557f66261930> on this part of my code:
ActiveSupport::Notifications.subscribe(/rack_attack/) do |_, _, _, _, req|
if %i[throttle blocklist].include?(req.env['rack.attack.match_type'])
Rails.logger.warn("Rack_Attack: #{req.env['rack.attack.match_type']} "\
"#{req.ip} #{req.request_method} #{req.fullpath}")
end
end
I also have some tests, that previously it was successful, returning 403, but now it returns 500. `require spec_helper
describe 'Rack::Attack', :dbless, type: :feature do include FeatureHelper
it 'blocks blacklisted domains' do visit '/apple-touch-icon' expect(page.status_code).to eq 403 end end `
If I just downgrade the version to the previous one everything just works as expected. I couldn't find any break changes apart from subscribing to /rack_attack/ instead of "rack.attack". Can someone help me to solve this issue?
Version 6 now yields a Hash object (called payload) with the :request key. The value of payload[:request] is the Request object we previously yielded directly.
So instead of:
ActiveSupport::Notifications.subscribe(/rack_attack/) do |_, _, _, _, req|
if %i[throttle blocklist].include?(req.env['rack.attack.match_type'])
Rails.logger.warn("Rack_Attack: #{req.env['rack.attack.match_type']} "\ "#{req.ip} #{req.request_method} #{req.fullpath}")
end
end
you should now use:
ActiveSupport::Notifications.subscribe(/rack_attack/) do |_, _, _, _, payload|
req = payload[:request]
if %i[throttle blocklist].include?(req.env['rack.attack.match_type'])
Rails.logger.warn("Rack_Attack: #{req.env['rack.attack.match_type']} "\ "#{req.ip} #{req.request_method} #{req.fullpath}")
end
end
Reasoning behind the change: #255 Commit with the change: 8802ebfbaf532dab1336da3a92039feb49c7f3a7
@grzuy looks like this isn't in the CHANGELOG.md
Indeed. Missing in the CHANGELOG.md. My bad.
Thank you @chrisokamoto for reporting and @lmansur for the comment.
Fixed in the CHANGELOG.md now.
In short, as @lmansur mentioned, the change on your app would be something like:
- ActiveSupport::Notifications.subscribe("rack.attack") do |name, start, finish, request_id, req|
+ ActiveSupport::Notifications.subscribe("rack.attack") do |name, start, finish, request_id, payload|
+ req = payload[:request]
Ok cool!! Thanks guys!! it worked :)
Is rack.attack.match_data available in V6? It seems to be referenced in other threads, but its not coming through for me on the notifications logging block?
It should be, @gregblass .
How are you trying to access it?
I see its only there for throttle events, and not for blocklist.
How would I find out if this is the first request for a blocklist notification? I only want to log the first one to our database and not overwhelm it if we're getting spammed.