I'm working in a legacy rails application, I'm trying to clean up some CSRF vulnerabilities. If I remove the hidden CSRF field from the form I can still successfully submit the form.
The only indication that something is amiss is a warning in the logs: WARNING: Can't verify CSRF token authenticity.
There are some pages where the protect_from_forgery will catch the request and the app will crash if there is no csrf token but its hit and miss depending on the page: eg. the login page works without the token but the update user page does not.
I've tried coming up with a custom strategy (suggested by Marc Gauthier) for protect_from_forgery, something like:
protect_from_forgery with: :MyStrategy
class MyStrategy
byebug
def initialize(controller)
@contriller = controller
end
def handle_unverified_request
puts "HELLO!"
Rails.logger.warn [
"handle_unverified_request",
"#{@controller.controller_name}-#{@controller.action_name}"
].join(" - ")
end
end
This didn't seem to do anything when starting the app the byebug call will pause but I never get the puts message or the error log.
I've also tried the normal strategies such as with: :exception but nothing changes, some pages it works and some it doesn't, but they are consistent.