19

Or How does Firefox determine where the password/username goes?

If I change name, id, title, class of an input element Firefox keeps filling it with password or email.

4 Answers 4

35

If I understand Firefox's source code correctly, the browser first looks for password fields in forms. If form contains more than 3 password fields, the autofill function ignores that form.

After 1 to 3 password fields are found, the browser looks for login field. The browser does a backward search starting from first password field and assumes that the login field is the first found field of the type text or email or url or tel or number.

Next step depends if we check the forms on page load or when submitting the form. If we check during page load and there is a login field and exactly ONE password field, the case is simple and the browser can fill out the form.

Other cases (form submit or more than 1 password field) do some “smart” logic to determine which password field contains new password and which one the old password, probably to update stored passwords). If you're interested in details, download the source code and open toolkit/components/passwordmgr/nsLoginManager.js file. Functions to check are _fillForm, _getFormFields and _getPasswordFields.

Just to summarize, Firefox doesn't need any ID, name or class attributes to guess which field is login or password. It just relies on types and the order of form fields.

5
  • 2
    That's correct. It should also be mentioned that this works only with fields in the original HTML, not with JS generated stuff. Commented May 24, 2012 at 22:07
  • Thanks a lot. It's weird since Firefox stores Fieldname in signon.sqlite. Did not have the time and patience to search in the source code, but i'll take a look.
    – user1202495
    Commented May 25, 2012 at 6:38
  • 1
    Your answer is very helpful thanks for looking through FF's code and explaining it. I tried what you explained and it verifies what you outlined. This behaviour was puzzling me because I hoped to remove autocomplete of fields by changing their names, because autocomplete=off does not prevent from autocompleting on data that was stored before an autocomplete=off was enabled.
    – eloone
    Commented Jan 23, 2014 at 16:10
  • 4
    This is annoying, d**n it ! Does anyone have a solution for preventing this behaviour ? I have a signin form with a password field, the mobile text input (password previous sibling) gets filled with the current user's e-mail adress :/
    – Stphane
    Commented May 22, 2015 at 9:48
  • Firefox's code has changed in the meantime. If anybody is looking for the behaviour described above, it can be viewed here: dxr.mozilla.org/mozilla-central/source/toolkit/components/…
    – Rafael
    Commented Apr 23, 2020 at 19:43
8

I tried a simple solution that is working so far. Create 2 hidden fields and the browser will autofill those.

<input type="text" style="display: none">
<input type="password" style="display: none">
0
5

Looks like using a disabled input text between login and password inputs does the trick well :

<input type="text" disabled="disabled" style="display:none">
3

Are those the only two elements on the form? Firefox is likely storing the structure of the form (two input boxes, one flagged as normal, one flagged as password) and filling in saved information without respect to the ID of the input elements.

Try this: add an extra input element to the form and see what happens. Either Firefox will not fill in anything, or you'll find your name in the first field and the password field filled in, while the second input element is blank.