Skip to content

Add autocomplete="off" to all generated hidden fields (fixes #42610)#43280

Merged
rafaelfranca merged 2 commits intorails:mainfrom
ryanfb:hidden_autocomplete_off
Sep 22, 2021
Merged

Add autocomplete="off" to all generated hidden fields (fixes #42610)#43280
rafaelfranca merged 2 commits intorails:mainfrom
ryanfb:hidden_autocomplete_off

Conversation

@ryanfb
Copy link
Copy Markdown
Contributor

@ryanfb ryanfb commented Sep 21, 2021

Summary

Firefox has a longstanding bug where it may populate hidden inputs without autocomplete="off" with completely random values. Since Rails uses hidden fields extensively for e.g. CSRF protection and non-standard HTTP methods via _method, Firefox users interacting with otherwise-fine Rails apps will see random "Invalid Authenticity Token" errors and form inputs getting interpreted as the incorrect HTTP method, among other unexpected behavior. Adding autocomplete="off" does not appear to have any negative consequences for other browsers, and is valid HTML. There's more discussion and links at: #42610

I recently bundled my workaround for this into a gem for Rails 6.1 apps, rails-hidden_autocomplete, which I've now reworked into this PR so that it can benefit all Rails users & developers, since this bug is currently extremely frustrating to diagnose and fix in real-world apps (see also podqueue/rails-hidden_autocomplete#2).

Other Information

I appreciate that this change might need to be gated behind a new framework default for ActionView, which I'd be happy to work on adding if that's the consensus.

@rails-bot rails-bot bot added the actionview label Sep 21, 2021
@rafaelfranca
Copy link
Copy Markdown
Member

Action text tests are broken. Can you take a look?

@rails-bot rails-bot bot added the actiontext label Sep 22, 2021
@ryanfb
Copy link
Copy Markdown
Contributor Author

ryanfb commented Sep 22, 2021

Action text tests are broken. Can you take a look?

Should be fixed by 58127ec.

@rafaelfranca rafaelfranca merged commit aa449a8 into rails:main Sep 22, 2021
rafaelfranca added a commit that referenced this pull request Sep 22, 2021
Add autocomplete="off" to all generated hidden fields (fixes #42610)
andyundso added a commit to simplificator/datatrans that referenced this pull request Feb 21, 2022
andyundso added a commit to simplificator/datatrans that referenced this pull request Feb 21, 2022
andyundso added a commit to simplificator/datatrans that referenced this pull request Feb 21, 2022
kevindew added a commit to alphagov/whitehall that referenced this pull request Mar 11, 2022
Starting in Rails 6.1.5 hidden inputs have autocomplete="off" set as an
attribute, thus the test HTML needs to be updated.

This is explained further in: rails/rails#43280
brendon added a commit to brendon/rails that referenced this pull request Oct 31, 2024
rails#43280 introduced an enforced `autocomplete="off"` to all hidden inputs generated by Rails to fix a [firefox bug](https://bugzilla.mozilla.org/show_bug.cgi?id=520561).

Unfortunately it's also a legitimate use-case to specify an `autocomplete` with a value such as `username` and a value on a hidden input. This hints to the browser that (in this example) the username of a password reset form is what we've provided as the value and the password manager can store it as such.

This commit only sets `autocomplete="off"` if another `autocomplete` value isn't provided.

Supersedes rails#47798 which didn't seem to be as elegant a solution and didn't have proper tests.
byroot pushed a commit to brendon/rails that referenced this pull request Nov 15, 2024
rails#43280 introduced an enforced `autocomplete="off"` to all hidden inputs generated by Rails to fix a [firefox bug](https://bugzilla.mozilla.org/show_bug.cgi?id=520561).

Unfortunately it's also a legitimate use-case to specify an `autocomplete` with a value such as `username` and a value on a hidden input. This hints to the browser that (in this example) the username of a password reset form is what we've provided as the value and the password manager can store it as such.

This commit only sets `autocomplete="off"` if another `autocomplete` value isn't provided.

Supersedes rails#47798 which didn't seem to be as elegant a solution and didn't have proper tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants