Details
> - The user will not be redirected to their original page after signing-in (because setting the redirect path is a state-changing action, and it is dangerous to let an unauthorized request changing the state – an attacker could control the path where an user is automatically redirected to.)
> - The implementation is finicky, and may introduce security errors. For instance, a naive implementation that catches the exception and redirect_to the sign-in page will prevent Devise from running a cleanup code – which means the user will still be logged, and the CSRF protection is bypassed. However a well-tested implementation that lets Devise code run should avoid these pittfalls.
>
> ### Using a long-lived cookie for CSRF tokens
>
> Instead of storing the CSRF token in the session cookie (which is deleted when the browser is closed), we can instead store it in a longer-lived cookie. For this we need to patch Rails.
>
> Pros:
> - It solves 1., because when the user submits a stale form, even if the session cookie because stale, the long-lived CSRF cookie is still valid.
>
> Cons:
> - It doesn't solve 2., because when Safari retries a POST request, it sends none of the cookies (not even long-lived ones).
> - Patching Rails may introduce security issues (now or in the future)
Broken behavior due to session expiration + template cache
Although pretty unlikely, you should make sure that your current setup for cache/session expiration is compatible. The upgrade can break the addition of products to the cart if both:
- The "Add to cart" form is being cached (usually along with the variant information).
- A user session is reset at every or every few requests.
The token validation depends on the issuing and consuming sessions being the same. If a product page is cached with the token in it, it can become stale on a subsequent rendering if the session changes.
To check that you're safe, after having upgraded locally, go through the following steps:
- Enable cache on dev mode:
bin/rails dev:cache
- Visit the page for a variant with stock.
- Reload that page several times.
- Click on the "Add to cart" button.
- Remember to rerun
bin/rails dev:cacheto turn off cache again.
No error or session reset should happen.
Otherwise, you can try with:
- Revisiting how your session gets expired.
- Changing the caching strategy to exclude the token.
Using weaker CSRF protection strategies
It's also important to understand that a complete fix will only be in place when using the :exception forgery protection strategy. The solidus_frontend engine can't do pretty much anything otherwise. Using weaker CSRF strategies should be an informed and limited decision made by the application team. After the upgrade:
- An app using
:null_sessionshould also be safe, but there will be side effects. That strategy runs with a null object session. As such, no order and no user is found on it. A newcartstate order is created in the database, associated with no user. Next time the user visits the site, they won't find any difference in its cart state.
- An app using
:reset_sessionis not entirely safe. That strategy resets the session. That means that registered users will be logged out. Next time a user visits, they'll see the cart with the items added during the CSRF attack, although it won't be associated with their account in the case of registered users.
Reversing the update
If you still want to deploy the upgraded version before changing your application code (if the latter is needed), you can add the following workaround to your config/application.rb (however, take into account that you'll keep being vulnerable):
config.after_initialize do
Spree::OrdersController.skip_before_action :verify_authenticity_token, only: [:populate]
end
Workarounds
If an upgrade is not an option, you can work around the issue by adding the following to config/application.rb:
config.after_initialize do
Spree::OrdersController.protect_from_forgery with: ApplicationController.forgery_protection_strategy.name.demodulize.underscore.to_sym, only: [:populate]
end
However, go through the same safety check detailed on "Upgrade notes" above.
References
For more information
If you have any questions or comments about this advisory:
- Open an issue or a discussion in Solidus.
- Email us at [[email protected]](mailto:[email protected])
- Contact the core team on Slack