Reputation: 11107
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!
Upvotes: 13
Views: 2304
Reputation: 342
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.
Upvotes: 3