fix: 防止密码管理器把首页搜索框当成账号字段填入

现象:搜索框被自动填入保存的邮箱或密码。

根因是页面此前完全没有 <form> 标签——登录、注册、重置密码三处的
输入框与按钮都裸放在 div 中。密码管理器找不到表单边界,只能靠启发式
猜测,于是把页面上的其他文本框(首页搜索框)也纳入填充范围。

修复分两部分:

一、为三个认证模态框补上真正的 <form>

- loginForm:邮箱用 autocomplete="username",密码用 current-password
- registerForm:密码用 new-password,验证码用 one-time-code,
  验证码与图形验证码标 off
- forgotPasswordForm:新密码用 new-password

有了明确的表单边界,密码管理器的填充范围被限定在正确的表单内。

注意随之而来的副作用:按钮改为 type="submit" 后,回车或点击会触发
浏览器原生表单提交导致页面刷新。已在 auth.js 中统一拦截三个表单的
submit 事件。

二、为搜索框叠加多重忽略标识

Chrome 等对 autocomplete="off" 经常忽略,因此同时加上:

- type="search" 与 role="searchbox",语义明确
- name="post-search-query",不易被判定为账号字段
- autocorrect / autocapitalize / spellcheck 关闭
- data-form-type="other"(1Password 识别)
- data-1p-ignore(1Password)与 data-lpignore="true"(LastPass)

实测(Puppeteer,1280×900):
- 页面含 loginForm / registerForm / forgotPasswordForm 三个表单,
  登录表单内邮箱与密码字段齐备
- 搜索框 type=search、name=post-search-query、role=searchbox,
  两个密码管理器的忽略属性均存在
- 在登录框内按回车:未发生页面导航(framenavigated 为 false),
  登录成功且弹窗正常关闭——确认 submit 拦截生效
- 搜索框未受影响,仍可正常使用

完整测试套件 25/25 通过。
This commit is contained in:
Furrium Network committed 2026-09-22 22:03:21 +08:00
1 parent fdf90f4b1d
commit c26e834982
2 files changed
+54 -25

No files matched your search

+10
View File
@@ -2,6 +2,16 @@
// 🔐 认证
// ============================================================
// 三个认证表单的提交拦截。
// 输入框与按钮原本裸放在 div 中(页面没有 form 标签),密码管理器因此
// 找不到表单边界,会靠启发式猜测把首页搜索框也当成账号字段填入。
// 现改用真正的 <form> 包裹,需要拦下默认提交行为——否则按回车或点按钮
// 会触发浏览器原生提交导致页面刷新。
['loginForm', 'registerForm', 'forgotPasswordForm'].forEach(id => {
const form = document.getElementById(id);
if (form) form.addEventListener('submit', e => e.preventDefault());
});
// 登录事件绑定
document.getElementById('loginBtn').addEventListener('click', () => {
document.getElementById('loginModal').classList.add('active');