Repository navigation
Commit ea1edf9
fix(plugin-auth): a single-use auth value is redeemed at most once, even across processes sharing a database (#34511)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9CEjuTu4HVEtNH1742UngyH)_
Clause-②: no
Metadata-protocol: none
## 维护者速读
-
**改了什么**:认证适配器兑现一次性认证值(邮箱验证、重置密码、魔法链接、一次性验证码等)时,只把值交给自己的删除确实删掉那一行的那次调用,其余调用一律得到
`null`。
- **为什么改**:规则是「一次性认证值至多兑现一次,即使多个进程共享同一个数据库」,也是 better-auth `consumeOne`
契约的要求。
- **风险与回滚**:单次兑现行为不变;数据引擎的删除若既不答「删没删」也不答「删了几行」,兑现会报错而不是放行。回滚即还原本
PR,改动只在一个源文件。
- **先看**:`packages/plugins/plugin-auth/src/objectql-adapter.ts` 的
`consumeOne` 与新函数 `removedByThisDelete`;再看 `objectql-adapter.test.ts`
末尾新增的 describe。
- **两分钟验证**:依赖构建后跑 `pnpm --filter @objectstack/plugin-auth exec vitest
run src/objectql-adapter.test.ts -t 'redeemed at most once'`,预期 4
个用例全部通过。
- **需要你做的**:无,席位落地。
## What changes
**Rule: a single-use auth value is redeemed at most once, even across
processes sharing a database.**
- `packages/plugins/plugin-auth/src/objectql-adapter.ts`: `consumeOne`
hands the stored row back only when this call's own by-id delete removed
it, and answers `null` otherwise, as better-auth's `consumeOne` contract
requires. The new `removedByThisDelete` reads the data engine's delete
answer: `true` (or a count of 1) is this call's removal; `false`, a
count of 0, or the engine's `RECORD_NOT_FOUND` mean the row is gone and
was not removed by this call; any other answer is refused by name rather
than read as either.
- `packages/plugins/plugin-auth/src/objectql-adapter.test.ts`: one new
top-level `describe` with 4 pins over the real stack (two `ObjectQL`
engines, each with its own `@objectstack/driver-sql` connection to one
SQLite file, each with its own factory-wrapped adapter).
- `.changeset/auth-single-use-redeemed-once.md`:
`@objectstack/plugin-auth` patch, rule only.
No engine, driver, spec or exported signature changes.
## Tests
The new `describe` ("a single-use auth value is redeemed at most once,
even across processes sharing a database"), 4 pins:
1. of two concurrent redemptions on two engines over one database,
exactly one receives the value (the other gets `null`);
2. a redemption of a value already redeemed elsewhere receives `null`;
3. a single redemption receives the value and removes it (a second
redemption gets `null`);
4. redeeming a value that does not exist receives `null` and removes
nothing.
Ablation (the decision line replaced by the behaviour before this PR, on
disk, through `scripts/ablation-replace.mjs`): 2 of 4 pins red;
restored: 4 of 4 green, `git diff HEAD` empty.
## Acceptance notes
- Read from code, not measured: every driver in `packages/drivers`
answers its by-id delete from the store's own removal result, which is
what the engine passes through and this adapter now reads. Measured: the
SQL driver over SQLite (the pins above).
- The pins drive the adapter's `consumeOne` directly, the method
better-auth calls for each single-use value.
## Verification
All readings are at head `a3aa7c8f`. The branch merged `main` once (at
`3bf9c5c4`, merge commit `2e4eed29`). Before the fix, on `origin/main`
`bf769229`, the same 4 pins read 2 red and 2 green.
<details><summary>plugin-auth vitest (full package)</summary>
`vitest run --maxWorkers=2` in `packages/plugins/plugin-auth` under
`scripts/pm/os-verify-lock.sh`, at `HEAD=a3aa7c8f`:
`Test Files 280 passed (280)` · `Tests 5767 passed | 10 skipped (5777)`
· `VERDICT command-exit 0`.
</details>
<details><summary>plugin-auth typecheck</summary>
`pnpm --filter @objectstack/plugin-auth typecheck` (tsc, the examples
project and `check:test-typecheck`) under
`scripts/pm/os-verify-lock.sh`: `VERDICT command-exit 0`.
`check:test-typecheck: OK — @objectstack/plugin-auth's test layer
compiles under packages/plugins/plugin-auth/tsconfig.test.json; 10
file(s) / 71 error(s) / 23 pinned signature(s) held in
test-typecheck-debt.json` (the existing ledger, unchanged).
</details>
<details><summary>Gates derived by dispatch-gates.mjs
--commands</summary>
`node scripts/pm/dispatch-gates.mjs --commands` derived 69 families from
the 3 changed paths; all 69 run at `a3aa7c8f`, each exit code captured
before any pipe. `--ran` reconciliation: `69 derived, 69 run, 0
NOT-MEASURED, 0 UNRUN` (a derived zero: every row recorded an exit
code).
- `node scripts/check-adr-0087-registration.mjs --base origin/main` ::
exit 0
- `node scripts/check-adr-0087-registration.mjs --self-test` :: exit 0
- `node scripts/check-changeset-no-major.mjs --base origin/main` :: exit
0
- `node scripts/check-closing-keyword-parity.mjs` :: exit 0
- `node scripts/check-closing-keyword-parity.mjs --self-test` :: exit 0
- `node scripts/check-comment-mask-adoption.mjs` :: exit 0
- `node scripts/check-comment-mask-adoption.mjs --self-test` :: exit 0
- `node scripts/check-comment-mask-corpus.mjs` :: exit 0
- `node scripts/check-dev-prereqs.mjs --self-test` :: exit 0
- `node scripts/check-dts-emitted.mjs --self-test` :: exit 0
- `node scripts/check-empty-changeset.mjs --base origin/main` :: exit 0
- `node scripts/check-empty-changeset.mjs --self-test` :: exit 0
- `node scripts/check-issue-citations.mjs` :: exit 0
- `node scripts/check-plugin-teardown-shape.mjs` :: exit 0
- `node scripts/check-plugin-teardown-shape.mjs --self-test` :: exit 0
- `node scripts/check-tenant-audit-census.mjs` :: exit 0
- `node scripts/check-tenant-audit-census.mjs --self-test` :: exit 0
- `node scripts/ci/assert-turbo-remote-cache.mjs --self-test` :: exit 0
- `node scripts/ci/plan-test-core-shards.mjs --self-test` :: exit 0
- `node scripts/ci/prune-turbo-cache.mjs --self-test` :: exit 0
- `node scripts/ci/snapshot-next.mjs --self-test` :: exit 0
- `node scripts/docs-audit/check-affected-docs.mjs` :: exit 0
- `node scripts/docs-audit/check-drift-comment.mjs` :: exit 0
- `node scripts/pm/check-governed-queue-guard.mjs --self-test` :: exit 0
- `node scripts/pm/release-rehearsal-clone.mjs --self-test` :: exit 0
- `node scripts/release-pending-publish.mjs --self-test` :: exit 0
- `pnpm --filter @objectstack/spec run check:duration-unit-keys` :: exit
0
- `pnpm --filter @objectstack/spec run check:error-code-provenance` ::
exit 0
- `pnpm check:adr-anchors` :: exit 0
- `pnpm check:adr-symbol-anchors` :: exit 0
- `pnpm check:changeset-gate-self-tests` :: exit 0
- `pnpm check:ci-filter-parity` :: exit 0
- `pnpm check:closed-ceremony-text` :: exit 0
- `pnpm check:cross-package-test-inputs` :: exit 0
- `pnpm check:dispatcher-error-vocabulary` :: exit 0
- `pnpm check:doc-authoring` :: exit 0
- `pnpm check:driver-memory-census` :: exit 0
- `pnpm check:dts-closure` :: exit 0
- `pnpm check:dual-build-cjs-loads` :: exit 0
- `pnpm check:engine-double-contract` :: exit 0
- `pnpm check:gitlink-declared` :: exit 0
- `pnpm check:issue-citations` :: exit 0
- `pnpm check:keyed-text-bounds` :: exit 0
- `pnpm check:lean-entry-closure` :: exit 0
- `pnpm check:logger-receiver-detach` :: exit 0
- `pnpm check:nul-bytes` :: exit 0
- `pnpm check:object-def-param-keys` :: exit 0
- `pnpm check:objectql-double-limit` :: exit 0
- `pnpm check:optional-error-sink` :: exit 0
- `pnpm check:org-identifier` :: exit 0
- `pnpm check:page-declaration-shape` :: exit 0
- `pnpm check:platform-object-tenancy-census` :: exit 0
- `pnpm check:published-files` :: exit 0
- `pnpm check:query-options-erasure` :: exit 0
- `pnpm check:refd-timer-probe` :: exit 0
- `pnpm check:registry-log-declared` :: exit 0
- `pnpm check:rest-log-spy-declared` :: exit 0
- `pnpm check:slot-lookup` :: exit 0
- `pnpm check:sourcemap-no-sources-content` :: exit 0
- `pnpm check:system-context-census` :: exit 0
- `pnpm check:test-source-alias` :: exit 0
- `pnpm check:tier-file-adoption` :: exit 0
- `pnpm check:type-check-coverage` :: exit 0
- `pnpm check:type-check-debt` :: exit 0
- `pnpm check:undeclared-dep-imports` :: exit 0
- `pnpm check:vouch-for-reference-callers` :: exit 0
- `pnpm check:watch-hint-literal` :: exit 0
- `pnpm check:where-matcher` :: exit 0
- `pnpm check:wildcard-fallthrough` :: exit 0
</details>
<details><summary>Ablation</summary>
`node scripts/ablation-replace.mjs` on
`packages/plugins/plugin-auth/src/objectql-adapter.ts`: the decision
line replaced by a bare `await deletion;` (anchor 1 to 0, blob
`6902c52d6194` to `02672d5b7d47`), then `vitest run
src/objectql-adapter.test.ts -t 'redeemed at most once'` under the
verify lock: `Tests 2 failed | 2 passed`, `VERDICT command-exit 1`.
Restored: blob equals HEAD (`6902c52d6194`), `git diff HEAD` empty; the
whole file then reads `Tests 51 passed (51)`, `VERDICT command-exit 0`.
The subject is imported from source (`./objectql-adapter`), so no build
sits between the mutation and the run.
</details>
<!-- signed: security#1 · os-steve · session_01ADHkmRiaNzHVY81pCjZrpv
-->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 23a07c8 commit ea1edf9
3 files changed
Lines changed: 202 additions & 5 deletions
File tree
- .changeset
- packages/plugins/plugin-auth/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
4 | 10 | | |
5 | 11 | | |
6 | 12 | | |
| |||
753 | 759 | | |
754 | 760 | | |
755 | 761 | | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1218 | 1218 | | |
1219 | 1219 | | |
1220 | 1220 | | |
1221 | | - | |
1222 | | - | |
1223 | | - | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
1224 | 1227 | | |
1225 | 1228 | | |
1226 | 1229 | | |
| |||
1237 | 1240 | | |
1238 | 1241 | | |
1239 | 1242 | | |
1240 | | - | |
| 1243 | + | |
| 1244 | + | |
1241 | 1245 | | |
1242 | 1246 | | |
1243 | 1247 | | |
| |||
1290 | 1294 | | |
1291 | 1295 | | |
1292 | 1296 | | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
| 1308 | + | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
1293 | 1330 | | |
1294 | 1331 | | |
1295 | 1332 | | |
| |||
0 commit comments