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

undefined method env for Hash

Open chrisokamoto opened this issue 7 years ago • 7 comments

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?

chrisokamoto avatar May 23 '19 16:05 chrisokamoto

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

lmansur avatar May 23 '19 16:05 lmansur

Indeed. Missing in the CHANGELOG.md. My bad. Thank you @chrisokamoto for reporting and @lmansur for the comment.

Fixed in the CHANGELOG.md now.

grzuy avatar May 23 '19 19:05 grzuy

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]

grzuy avatar May 23 '19 19:05 grzuy

Ok cool!! Thanks guys!! it worked :)

chrisokamoto avatar May 24 '19 07:05 chrisokamoto

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?

gregblass avatar Apr 17 '20 17:04 gregblass

It should be, @gregblass .

How are you trying to access it?

lmansur avatar Apr 17 '20 17:04 lmansur

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.

gregblass avatar Apr 17 '20 17:04 gregblass