-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing Authenticity Token in Remote Forms w/ File Upload Field(s) #22807
Comments
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
Wow, I've been tracking this down for hours, and it was painful. It's exactly as @mvastola describes it—CSRF fails when a form (1) has file inputs and (2) has remote: true. My instinct is that this should be fixed in jquery-ujs. I documented my opinion on a fix here: rails/jquery-ujs#451 (comment) An easy workaround is to add <%= form_for @user, remote: true, authenticity_token: true do |f| %>
<%= f.file_field :image %>
<% end %> That workaround is easy if you're not fragment caching forms, and ignores the documentation's statement that you should only use the
Perhaps another way to "fix" this is to always include the authenticity token, and to instruct users who care about fragment caching to use |
One more thing, then I will let wiser people advise... While debugging this behavior in my app, the most confusing thing going on was that setting It seems like the nuance that per-form CSRF tokens are never inserted on forms with I'm happy to writeup a PR with doc updates. Just trying to help. Thanks for your work on Rails, I <3 it. |
I just wanted to drop a brief note here and mention that I just ran into this issue. I noticed the ActionController::InvalidAuthenticityToken error when submitting a I like @aguynamedben's suggestion to include the token by default in order to support fallback. |
This is still happening in Rails 5.0.1 |
This issue has been automatically closed because of inactivity. If you can still reproduce this error on the Thank you for all your contributions. |
@rails-bot, please re-open. As @onomojo stated just yesterday, he can still reproduce this issue. (I personally haven't tried.) I don't know how you're configured, but I define inactivity as a lack of activity. A comment = activity. :- |
Let's go with this solution |
Hmm. Better yet, we should go with the solution commented rails/jquery-ujs#451 (comment). Could someone work on it? |
@rafaelfranca Okay. I'm in the middle of a huge sprint right now. What timeframe would be ideal to complete the work? I could maybe do it in 2-4 weeks from now. Thanks for your feedback on this, I'd like to give back by doing this. |
We waited more than three moths already so I think 2-4 weeks is fine 😄. |
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
That's certainly one way to fix the bug. Pretend it doesn't exist. |
Go drunk, @rails-bot[bot], you're home. |
I can confirm that this bug still exists on Rails 5.0.6. |
I can confirm, this still happens on rails 5.2.1 |
I've got the same issue with rails 5.2.1.
|
This same thing happened moving from |
Don't need to set verbosely:
|
This is how I solved the issue. Remove remote: true from the form and then submit the form using jquery. |
This is more-or-less a cross-post of rails/jquery-ujs#451.
This issue deals with
form_for
tags containing one or morefile_field
s where:remote => true
.config.action_view.embed_authenticity_token_in_remote_forms
defaults tofalse
to facilitate fragment caching (which makes total sense), but thejquery-ujs
gem is currently coded to permit (without alteration -- meaning it will submit as non-ajax) the submit event of any form if:data-remote
attribute in<form>
tag is set totrue
; and,<form>
tag contains one or more<input type="file" />
children; and,<input type="file" />
tag has a file selected for upload(Note that, by design,
jquery-ujs
does not provide a method to submit file uploads via AJAX. It only provides a non-AJAX fallback. To submit a remote form with file uploads via AJAX, one must include javascript that catches the submit action beforejquery-ujs
does.)Long story short, there is an issue in that
jquery-ujs
cannot currently fulfill its role to provide a non-AJAX fallback since there is no hidden input element in the<form>
provided byactionview
(andjquery-ujs
is not using the page's meta tags as it does for remote requests) containing an authenticity token, so none isPOST
ed anInvalidAuthenticityToken
is raised in response to the form submission.The question du jour is where this bug should be fixed: should a hidden input element always be inserted by
actionview
in remoteform_for
s in which afile_field
is present (regardless ofconfig.action_view.embed_authenticity_token_in_remote_forms
)? (This is the solution suggested by thejquery-ujs
owner.) Alternatively, shouldjquery-ujs
create a hiddenauthenticity_token
input tag inside the<form>
dynamically (with the value from the page's meta tags) immediately before it is about to fulfill its fallback role (my inclination).Please advise/discuss/etc. (I'm happy to contribute a PR resolving the issue in whatever manner is mutually agreeable, but obviously either
rails
orjquery-ujs
must be willing to merge it.)The text was updated successfully, but these errors were encountered: