Skip to content

Add Rails 7.1 cookie metadata compatibility#24

Open
jivko-chobanov wants to merge 1 commit into
cconstantin:masterfrom
jivko-chobanov:rails-7-1-cookie-compat
Open

Add Rails 7.1 cookie metadata compatibility#24
jivko-chobanov wants to merge 1 commit into
cconstantin:masterfrom
jivko-chobanov:rails-7-1-cookie-compat

Conversation

@jivko-chobanov

Copy link
Copy Markdown

Fixes #23.

This updates plug_rails_cookie_session_store to handle the Rails 7.1 cookie message format while remaining compatible with older Rails behavior.

What changed:

  • add purpose support
  • add metadata_format support (:legacy and :serializer)
  • wrap outgoing cookies with Rails-compatible metadata when configured
  • decode both legacy metadata-wrapped payloads and serializer-metadata payloads
  • keep fallback decoding for older cookie payloads

Notes:

  • this PR keeps the library changes generic to Rails cookie compatibility
  • App-specific Phoenix/Rails CSRF handling is intentionally left outside this library. In a shared-session setup, Rails and Phoenix can collide if both frameworks use the same session key for CSRF state, because the stored token formats are not interchangeable and one side may overwrite the other. The practical fix is to separate CSRF storage at the app layer, for example by giving Rails a custom protect_from_forgery store: implementation that reads/writes a dedicated key like :_csrf_token_rails, while Phoenix keeps using its default Plug-managed CSRF key.

Context:
Rails 7.1.5.1 cookies could be read by Phoenix via plug_rails_cookie_session_store, but when Phoenix wrote them back, Rails no longer recognized the payload correctly because the library did not handle Rails' metadata wrapping expectations.

I tested this change in a real Phoenix/Rails integration setup by pointing Phoenix to this forked branch and validating the shared session behavior.

@cconstantin

Copy link
Copy Markdown
Owner

@jivko-chobanov thanks for this. Can you also update the test suite, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In Rails 7.1 the cookie format has changed compared to 7.0. Breaks Phx->Rails, but not Rails->Phx transition.

2 participants