@@ -378,6 +378,7 @@ func NewFuncMap() []template.FuncMap {
378378 "MermaidMaxSourceCharacters" : func () int {
379379 return setting .MermaidMaxSourceCharacters
380380 },
381+ "DisableFormAutofill" : disableFormAutofill ,
381382 }}
382383}
383384
@@ -965,3 +966,39 @@ func mirrorRemoteAddress(m models.RemoteMirrorer) remoteAddress {
965966
966967 return a
967968}
969+
970+ func disableFormAutofill () template.HTML {
971+ /*
972+ Why we need to disable form autofill:
973+ 1. Many pages contain different password inputs for different usages, eg: repo setting, autofill will make a mess.
974+ 2. We have `areYouSure` confirm dialog if a user leaves a pages without submit.
975+ Autofill will make the form changed even if the user didn't input anything. Then the user keeps seeing annoying confirm dialog.
976+
977+ In history, Gitea put `<input class="fake" type="password">` in forms to bypass the autofill,
978+ but there were still many forms suffered the autofill problem.
979+
980+ Now we improve it.
981+
982+ Solutions which do NOT work:
983+ 1. Adding `autocomplete=off` doesn't help. New Chrome completely ignores it.
984+ 2. Use a JavaScript to run in a few seconds later after the page is loaded to process the autofilled inputs, it doesn't work.
985+ Because for security reason, the inputs won't be filled before the user makes an interaction in the page.
986+ So we can not predict the correct time to run the JavaScript code.
987+
988+ Solutions which work:
989+ 1. Some hacky methods like: https://github.com/matteobad/detect-autofill
990+ 2. This solution: use invisible inputs. Be aware of:
991+ (a) The inputs must be at the beginning of the form, and can not be hidden.
992+ (b) The input for username must have a valid name.
993+ (c) There should be no negative word (eg: fake) in the `name` attribute.
994+ (d) Chrome seems to use a weighted algorithm to choose an input to fill text, so the using "username" as input name is better than using "user".
995+ We make the names of these dummy inputs begin with an underline to indicate it is for special usage,
996+ and these dummy form values won't be used by backend code.
997+ */
998+ return `
999+ <div style="position: absolute; width: 1px; height: 1px; overflow: hidden; z-index: -10000;">
1000+ <input type="text" name="_autofill_dummy_username" class="ays-ignore">
1001+ <input type="password" name="_autofill_dummy_password" class="ays-ignore">
1002+ </div>
1003+ `
1004+ }
0 commit comments