You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9728ab1
Browse filesBrowse the repository at this point in the historyBrowse files
fix(plugin-auth): answer a locked account like a wrong password, clear the lock on reset, count attempts atomically (#31419)
<!-- claude-projects-attribution: {"github_login":"os-zhuang"} -->
_Requested by **Sam** · [project
thread](https://claude.ai/code/project/chan_01GsBogozeZG5sdpAARrN7HP?thread=cmsg_01GsBogozeZG5sdpAARrN7HPWpc3NTXt4TesUX4RAeTzBw)_Closes#28381
Clause-②: yes (widening)
Metadata-protocol: none
## 维护者速读
-
改了什么:被锁定的账号用密码登录时,得到的回答与密码错误完全相同(状态码、正文、耗时一致);完成密码重置会清除锁定;每次尝试在校验密码之前就原子计数,并发尝试越不过阈值;控制台的登录错误文案说明「多次失败会暂停一段时间,重置密码可立即登录」(全部
11 种控制台语言)。
- 为什么改:原来的 `403 ACCOUNT_LOCKED`
只有真实存在的账号才会出现,等于告诉任何人某个邮箱是否注册过;重置密码后锁还在,用户换了密码仍登不进;并发尝试能越过阈值。
- 风险与回滚:登录路径的行为变化,默认对锁定账号不再给出专门的提示;想保留原来 403 的部署设 `lockoutRefusal:
'explicit'` 即可;绕过 better-auth 端点管道的调用无法给出统一回答,会得到 403 并记一条降级日志。无数据迁移,回滚
= revert 本 PR。cloud 目前在两个平面上自行改写这个回答,本 PR 不动 cloud,改写由 cloud 在之后的 pin
轮次里撤掉。
-
先看:`packages/plugins/plugin-auth/src/auth-manager-sign-in-lockout.ts`(计数与上锁的原子写入、成功清零、密码通过后的拒绝退回计数)、`packages/plugins/plugin-auth/src/sign-in-lockout-refusal.ts`(锁定账号如何得到与密码错误相同的回答)、`packages/plugins/plugin-auth/src/auth-manager-auth-instance.ts`(重置清锁的接线)。
- 两分钟验证:`pnpm --filter @objectstack/plugin-auth exec vitest run
--maxWorkers=2 src/account-lockout-resolved-account.test.ts
src/password-reset-ends-sessions.test.ts`,预期两个文件全部通过。
- 需要你做的:无,席位落地。
## What changes
- **A locked account is answered like a wrong password.** A password
sign-in for an account the ADR-0069 D2 lockout holds is now refused `401
INVALID_EMAIL_OR_PASSWORD`, with the same body and the same
password-check cost a wrong password gets. The lock still holds against
the correct password, and the account's `login_failed` audit row still
records the reason `ACCOUNT_LOCKED`. A deployment that wants the
explicit answer sets the new `AuthManagerOptions.lockoutRefusal:
'explicit'` (default `'uniform'`) and is answered `403 ACCOUNT_LOCKED`
as before. A sign-in that runs outside better-auth's endpoint pipeline
cannot be answered uniformly; it is refused `403 ACCOUNT_LOCKED` and
logged as a degradation, so the lock still holds.
- **A completed password reset clears the lock.** A reset through the
emailed link or the phone code clears `failed_login_count` and
`locked_until`, so the new password signs in at once. An incomplete
reset clears nothing. The second factor's own lock is not cleared by a
reset; the administrator's Unlock action still clears both.
- **Concurrent attempts cannot exceed the threshold.** Each attempt is
counted before its password is checked, in one atomic write that also
stamps the lock when the count reaches the threshold. A successful
sign-in still clears the count, and a refusal that comes after the
password passed (an organization's IP list, a ban, an unverified
address) still neither counts nor clears.
- **The console names the pause.**
`auth.login.errors.invalidCredentials` now says repeated failures pause
sign-in for a while and that resetting the password signs in right away,
in all 11 console locales (`en`, `zh-Hans`, `zh-Hant`, `ja`, `ar`, `de`,
`es`, `fr`, `ko`, `pt-BR`, `ru`), so `check:i18n-drift` holds.
**Clause-② for the opt-in key.** `lockoutRefusal` is a new optional key
on `AuthManagerOptions`, the constructor options of the published
`@objectstack/plugin-auth`. It widens what that package accepts, so the
line reads `yes (widening)`. It is not a settings key and nothing in
`packages/spec` changes, so `Metadata-protocol` stays `none` and the
changeset is `patch`.
## Tests
- `account-lockout-resolved-account.test.ts` (real sign-in endpoint): a
locked account answers exactly what a wrong password answers, the
correct password included (status, body, and one password derivation
each, counted with the scrypt testkit for an open account's wrong
password, a locked account's correct and wrong password, and an unknown
account); input refusals match too; a locked refusal opens no session
and writes the `ACCOUNT_LOCKED` audit row; `lockoutRefusal: 'explicit'`
answers `403 ACCOUNT_LOCKED`; of 30 concurrent wrong passwords, exactly
the threshold reach the password check, the rest meet the lock, one
`account_locked` row is written, and the lock then holds against the
correct password.
- `password-reset-ends-sessions.test.ts` (real ObjectQL + sqlite): a
reset by emailed link and by phone code each clear the lock and the new
password signs in at once; an incomplete reset leaves the lock.
- `auth-manager.test.ts`: the lockout unit block, rewritten for the new
gate (no-op at threshold 0, the uniform and explicit refusals, the
refusal and its log line outside the pipeline, a locked attempt counted
without moving its expiry, the compare-and-set write below and at the
threshold, a re-read after a lost write, success clearing). Its fake
engine now opens `update` with `assertEngineUpdateDispatch`, so every
write the block makes is one the real engine accepts.
- `auth-account-event-audit.test.ts` and
`durability-swallow-repair.test.ts`: updated to the uniform answer and
the renamed gate.
- NULL counter (seat review): `password-reset-ends-sessions.test.ts` ›
"ADR-0069 D2: an account whose failed_login_count is NULL is counted and
cleared through ObjectQL + SqlDriver" (better-sqlite3; the column is
read back as NULL first) and `account-lockout-resolved-account.test.ts`
› "ADR-0069 D2: an account whose failed_login_count is NULL or absent is
counted and cleared like one at 0" (in-memory, NULL and absent): the
correct password signs in and clears the counter to 0; a wrong password
counts to 1 and the correct one still signs in after it. The
compare-and-set matches a NULL counter on both engines, so no
normalisation was needed. SqlDriver compiles every dialect through one
filter compiler; only sqlite was run.
Reverse verification: with the implementation committed, the plugin-auth
source files were restored from `eff90af6` and the two endpoint-level
files run against them: 10 failed and 25 passed (the uniform answer
under every spelling and input, the reset clearing in all three cases,
and the concurrency pin). `git checkout HEAD -- …` restored the
implementation, with `git diff HEAD` empty, and both files are green
again.
## Acceptance notes
- **The opt-in is config-only.** `lockoutRefusal` is read from the
constructor options; there is no settings key and no UI for it.
- **cloud is untouched.** cloud rewrites this answer on both planes
today; dropping that rewrite is a cloud card after a pin round carries
this change.
- **Where the concurrency pin runs.** The 30-attempt pin runs on the
in-memory engine, whose `update` honours `where` + `multi` and returns
the row count; the reset pins run on real ObjectQL + sqlite.
- **Files outside `plugin-auth/**` and the console copy.** Three
generated or ratchet files move with this diff, each written by its own
tool: `content/docs/permissions/tenant-audit-census.mdx` and
`docs/audits/2026-08-tenant-audit-write-call-sites.counts.md` (`node
scripts/tenant-audit-census.mjs --write`; two `sys_user` write sites now
read as a typed engine receiver, so the page's two hand-written
erased-receiver figures go from 64 to 62, with the total unchanged at
270), and `scripts/engine-double-contract.pinned.json`
(`check-engine-double-contract.mjs --write`; one more pinned fake in
`auth-manager.test.ts`). The seat agreed to the census page before it
was written.
- **Test-typecheck debt shrank.** Typing the lockout block's engine mock
removed 16 recorded test type errors in `auth-manager.test.ts`;
`test-typecheck-debt.json` was re-recorded downward with the package's
own `gen:test-typecheck-debt`.
- **The wrong-password code is not in the ADR-0112 ledger.**
`INVALID_EMAIL_OR_PASSWORD` is better-auth's code and reaches the wire
unregistered. Stamping it from plugin-auth would need a ledger row in
`packages/spec`, which this card does not touch, so the one path that
would have stamped it (outside the endpoint pipeline) uses the
registered `ACCOUNT_LOCKED` instead. Registering the code would let that
path answer uniformly too; left for the seat to schedule.
- **Body order.** The two attribution lines the session adds sit above
`Closes#28381`.
## Verification
The readings below were taken at head `42421e44`. At head `00c88e7e`
(NULL-counter tests and the 7 locale packs), under the lock: plugin-auth
typecheck OK and 240/240 files, 4267 tests; ui i18n, console auth,
`check-i18n-call-site-keys` and `check-i18n-en-drift`: 115/115 files,
2094 tests; the changeset, engine-double, NUL and i18n-drift gates
green. Wall-clock figures come from a shared box.
<details><summary>plugin-auth build, typecheck and full suite; console
copy tests</summary>
`bash scripts/pm/os-verify-lock.sh -c 'pnpm --filter
@objectstack/plugin-auth build && pnpm --filter @objectstack/plugin-auth
typecheck && pnpm --filter @objectstack/plugin-auth exec vitest run
--maxWorkers=2 && cd ui && npx vitest run --maxWorkers=2 packages/i18n/
packages/app-shell/src/console/auth/
scripts/__tests__/check-i18n-call-site-keys.test.ts
--testTimeout=120000'`
```
check:test-typecheck: OK — 10 file(s) / 72 error(s) / 23 pinned signature(s) held in test-typecheck-debt.json
Test Files 240 passed (240)
Tests 4261 passed | 10 skipped (4271)
Test Files 114 passed (114)
Tests 2029 passed | 13 skipped (2042)
os-verify-lock: VERDICT command-exit 0
```
</details>
<details><summary>Derived gates</summary>
`node scripts/pm/dispatch-gates.mjs --commands` derived 116 commands for
this change set. They ran with 4 more whose rosters sit beside touched
paths (`check:changeset-fixed`, `check:error-code-casing`,
`check:filter-alias-parity`, `check:error-status-conformance`), and each
exit code was captured before any pipe.
- 119 of 120 exited 0.
- `pnpm --dir ui exec vitest run scripts/` exited 1 because four tests
in `check-i18n-call-site-keys.test.ts` and
`check-vi-mock-specifiers.test.ts` each hit "Test timed out in 15000ms"
while three gates ran at once. Run under the lock with
`--testTimeout=180000`, those two files pass 187/187.
- `node scripts/pm/dispatch-gates.mjs --ran`: 116 derived families
accounted for, 0 not measured.
- `node scripts/check-nul-bytes.mjs`: OK.
Five gates needed a change on this branch. `check:test-typecheck`: debt
re-recorded downward. `check:tenant-audit-census`: page regenerated.
`check:engine-double-contract`: the ledger records one more pinned fake.
`check:dispatcher-error-vocabulary` and `check:query-options-erasure`:
code fixed, no ledger touched.
</details>
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QUs3BtDNDGBRyYSpvMa9Qi)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01QUs3BtDNDGBRyYSpvMa9Qi
<!-- signed: repo:cloud#3 · os-zhuang · session_01QUs3BvRcDHXqRU4TSVE4u6
-->
---------
Co-authored-by: Claude <noreply@anthropic.com>
fix(plugin-auth): a locked account is answered like a wrong password, a password reset clears the lock, and attempts are counted before they are checked
8
+
9
+
Clause-②: yes (widening)
10
+
Metadata-protocol: none
11
+
12
+
-**A locked account answers like a wrong password.** A password sign-in for an account the ADR-0069 D2 lockout holds was refused `403 ACCOUNT_LOCKED`. Only an account that exists can be locked, so that answer told anyone who typed an address whether it has an account. It is now refused exactly as a wrong password is: `401 INVALID_EMAIL_OR_PASSWORD`, the same body, and the same password check before the answer. The lock still holds against the correct password, and the account's `login_failed` audit row still records the reason `ACCOUNT_LOCKED`. A deployment that wants the explicit answer sets the new `AuthManagerOptions.lockoutRefusal: 'explicit'` (default `'uniform'`), and is answered `403 ACCOUNT_LOCKED` as before. A sign-in reached outside better-auth's endpoint pipeline cannot be answered uniformly; it is refused `403 ACCOUNT_LOCKED` and logged as a degradation.
13
+
-**A completed password reset clears the lock.** A reset through the emailed link or the phone code now clears `failed_login_count` and `locked_until`, so the new password signs in at once. Before, the lock stayed until it ran out or an administrator unlocked it. The second factor's own lock is not cleared by a reset; the administrator's Unlock action still clears both.
14
+
-**Attempts that arrive together cannot outrun the lock.** Each attempt is now counted before its password is checked, in one atomic write that also sets the lock when the count reaches the threshold, so at most the threshold's number of attempts reach the password check before the lock holds. A successful sign-in still clears the count, and a refusal that comes after the password passed (an organization's IP list, a ban, an unverified address) still neither counts nor clears. Attempts on a locked account are now counted too; the lock's expiry does not move.
15
+
-**The console names the pause.** The sign-in refusal copy (`auth.login.errors.invalidCredentials`) now says that repeated failures pause sign-in for a while and that resetting the password signs in right away, in all 11 console locales (`en`, `zh-Hans`, `zh-Hant`, `ja`, `ar`, `de`, `es`, `fr`, `ko`, `pt-BR`, `ru`).
0 commit comments