6

I've upgraded to Rails 4.1.0 today. Cross-site request forgery (CSRF) protection now covers GET requests with JavaScript responses, too.

I have a few remote GET links in the app that are hit by the bots and are now throwing ActionController::InvalidCrossOriginRequest exception.

So I added another rescue_from line to application_controller:

rescue_from ActionController::InvalidCrossOriginRequest, with: :render_400

Here's the render_400 method:

def render_400
    render(nothing: true, status: 400) and return
end

I'm still getting AbstractController::DoubleRenderError even though I added and return as you can see above.

It happens only with the ActionController::InvalidCrossOriginRequest exception. Others like e.g. ActionController::BadRequest and not resulting in AbstractController::DoubleRenderError.

1 Answer 1

7

The underlying reason is that some part of the response_body is assigned before the error is triggered.

You could try clearing the response body before calling render in the exception handler.

def render_400
  # Clear the previous response body to avoid a DoubleRenderError
  # when redirecting or rendering another view
  self.response_body = nil

  render(nothing: true, status: 400)
end
2
  • 2
    I'm having the problem with Rails 5.1 and this solution doesn't work :/
    – Tom Rossi
    Nov 24, 2017 at 16:35
  • Adding @_response_body = nil works in Rails 6.
    – adriandz
    Aug 26, 2022 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.