ActionController::InvalidAuthenticityToken in RegistrationsController#create

前端 未结 8 1473
后悔当初
后悔当初 2020-11-29 19:39

Hi I am using Devise for my user authentication suddenly my new user registration was not working.

this was error I am getting.

ActionController::In         


        
8条回答
  •  青春惊慌失措
    2020-11-29 20:19

    Browser Caching HTML Issue (2020)

    If you've tried all the remedies on this page and you're still having an issue with InvalidAuthenticityToken exceptions, it may be related to the browser caching HTML. There's an issue on Github with 100s of comments along with some reproducible code. In a nutshell, here's what was happening to me as it relates to HTML caching:

    1. User browses to website. Rails sets a signed session cookie on the first GET request. See config/initializers/session_store.rb for config options. This session cookie stores useful information, including a CSRF token that is used to decrypt and validate the authenticity of the request. Important: By default, the session cookie will expire when the browser window closes.
    2. User browses to a page containing a form. For me, I was receiving the most exceptions on my login page.
    3. Rails embeds a hidden CSRF token in this form, and submits this token along with the form data. Important: This token is embedded in the HTML.
    4. ActionController grabs the CSRF token from the params object and validates it with the CSRF token from the cookie using the verified_request? method in Rails 4.2+.

    Many browsers are now implementing HTML caching, so that when you open a page the HTML is loaded without a request. Unfortunately, when the browser is closed the session cookie is destroyed, so if the user closes the browser while on a form (such as a login page), then the first request will not contain a CSRF token thus throwing an InvalidAuthenticityError.

    Two common solutions

    1. Extend the expiry of your session cookie beyond the browser window.
    2. Detect in the browser if the session cookie is missing (via a proxy cookie), and if it is missing refresh the page.

    1. Extending the session cookie expiry

    As noted in this Github comment, Django takes this approach:

    Django puts adds the token in its own cookie called CSRF_COOKIE. This is a persistent cookie that expires in a year. If subsequent requests are made, the cookie's expiry is updated.

    In Rails:

    # config/initializers/session_store.rb 
    Rails.application.config.session_store :cookie_store, expire_after: 14.days
    

    With many things security related, there's concern that this could create vulnerabilities, but I have not been able to locate any examples of how an attacker could exploit this.

    2. Using javascript to refresh a page

    This approach involves setting a separate token that can be read by the browser, and if that token is not present, refreshing the page. Thus, when the browser loads the cached HTML (without the session cookie), executes the JS on the page, the user can be redirected or refresh the HTML.

    For example, setting a cookie for each non-protected request:

    # app/controllers/application_controller.rb
    class ApplicationController < ActionController::Base
      after_action :set_csrf_token
    
      def set_csrf_token
        cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
      end
    end
    

    Checking for this cookie in JS:

    const hasCrossSiteReferenceToken = () => document.cookie.indexOf('XSRF-TOKEN') > -1;
    
    if (!hasCrossSiteReferenceToken()) {
        location.reload();
    }
    

    This will force the browser to refresh.

    Conclusion

    I hope this helps some folks out there; this bug cost me days of work. If you're still having issues, consider reading up on:

    • Cloudflare Blog: The Curious Case of Caching CSRF Tokens
    • Cloudflare Flexible SSL mode breaks Rails 5 CSRF
    • The prepend: true bug in Devise, well described here.

提交回复
热议问题