Skip to content

Rails 7.1 CSRF / request_forgery_protection broken on jruby-rack session storage #295

Open
@chadlwilson

Description

@chadlwilson

Rails 7.1 changed its CSRF protection approach in rails/rails#44283 to allow storage outside the session. As a side effect of this approach, Rails now stores the token temporarily inside the request.env and then hijacks commit_session within the Rails abstract_store to actually store the CSRF token inside the session just prior to commit. FWIW, this also broke Devise: rails/rails#52244

rails/rails@f2c66ce#diff-5207bc67aa19cddd5a4997dec3c69fa4ba541750fbd881c6f7e30b27df9ea9ddR70-R73

rails/rails@f2c66ce#diff-60b77e427ea7ba142faa477fac10b8d0134cede4e35a3b1953c425200fadf1acR433-R435

This hijacking seems to not work with jruby-rack and the CSRF token is lost, causing all subsequent POSTs etc to fail. I believe essentially we have lost "abstract_store compatibility" with this change in Rails.

I have a hack which fixes it, but need to find the "right" way to do this, potentially that is slightly easier to maintain and gets things a bit readier for Rack 3.x.....

def commit_session(req, status, headers, body)
  session = req.env[::Rack::RACK_SESSION]
  options = req.env[::Rack::RACK_SESSION_OPTIONS]
  session[:_csrf_token] = req.env["action_controller.csrf_token"] if req.env["action_controller.csrf_token"]

@kares are you perhaps able to help make a 1.2-stable branch I can target for a PR as at ff790fb ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions