Our production server has been producing invalid authenticity token errors for several months now. The errors are produced on almost all forms sending (PUT|POST|DELETE) requests. Sometimes the error occurs, sometimes they don't. There appears to be no rhyme or reason as to why they occur. The error itself does not occur often but it is a worry for us. Below is an example of what a typical form that causes this error looks like.
<form class="button_to" method="post" action="/lesson_progress_trackers/333">
<input type="hidden" name="_method" value="patch">
<input class="finish-lesson-button" type="submit" value="Done!">
<input type="hidden" name="authenticity_token" value="Qd3FsJZY2UXR9vahuFmaY5rrqA+J5xzGpl4cGI2Vwerx8PZPQtDMugz6oqoe3iviC+/U5zTYPdeX3apwbap09E==">
<input type="hidden" name="completed" value="true">
</form>
Here's what I've discovered so far.
protect_from_forgery with: :exception
in our application controller.Ultimately I want to figure out how to solve this. My first step is to reproduce the error successfully, but I can't even do that. My question is this: what can I do to get me on my way to figuring out what's causing this? I am running out of options. Thanks!
Resolution. This error can be due to corrupted cookie in your browser. Clear your browsers cache and cookies, restart the browser and try to log in. If the error remains, the problem is that your browser has blocked any cookies from or because OCLCs Zendesk User Portal.
The authenticity token is designed so that you know your form is being submitted from your website. It is generated from the machine on which it runs with a unique identifier that only your machine can know, thus helping prevent cross-site request forgery attacks.
Rails CSRF Token The server generates these tokens, links them to the user session, and stores them in the database. This token is then injected into any form presented to the client as a hidden field. When the client correctly submits the form for validation, it passes the token back to the server.
Dunno if this is too late to be useful, but I had the same problem. I was able to reproduce by:
InvalidAuthenticityToken
exception occurs.I think that this happened for me because the two tabs shared a single session, the session that was created when the new tab was opened. However, the old tab still had the csrf token from the old session in the login form. When the new session cookie and the old csrf token were submitted together, they did not match and therefore the error is thrown.
I'm not sure how to actually fix this, other than handling the error more gracefully so that the user doesn't see a confusing error page.
BTW, I am using devise, but I don't think that it is specific to Devise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With