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 4ed2092
Browse filesBrowse the repository at this point in the historyBrowse files
fix(plugin-auth): audit the forced-reset flag and its clear under the person who caused them (#31820)
<!-- claude-projects-attribution: {"github_login":"os-steve"} -->
_Requested by **Steve** · [project
thread](https://claude.ai/code/project/chan_01FdL1XeUyvVNxLreh3F4Qi9?thread=cmsg_01FdL1XeUyvVNxLreh3F4Qi9NNU55MLPfTn3FA66UzcuCo)_Fixes#31804
Clause-②: no. Only the audit row's content changes: no route, option or
field. Measured on base 5b7269f and head: both doors answer the same
status and body and write the same values (CONTROL pins below).
Metadata-protocol: none
`domain:server#3` · dev session `session_01ADHkmShTSNgBjG5FV5GMXD`
## 维护者速读
- 改了什么:管理员重设密码(`/auth/admin/set-user-password`)写
`must_change_password=true`、用户自己改密码(`/auth/change-password`)写
`must_change_password=false`,这两次写入现在带上调用者,`sys_audit_log` 的 `actor` 和
`user_id` 分别记管理员和用户本人(模拟登录时另记 `impersonated_by`)。
- 为什么改:卡 #31804。main 上这两行审计的 `actor`、`user_id` 都是空,看不出是谁改的密码状态。
-
风险与回滚:只加归属,不改授权:两次写入仍以系统身份执行,写的值和两个入口的响应都不变。改密码的归属只给账号本人:没有会话(邮件链接重设)或会话属于别人时,仍记系统。附带:管理员建用户(`create-user`)的同一次写入也会记到管理员名下。回滚即还原本
PR。
- 先看:`admin-user-endpoints.ts` 的 `stampMustChangePassword` 与
`runAdminSetUserPassword` 里的
`runAttributedToUser`;`auth-manager-password-session-policy.ts` 的
`stampPasswordChangedAt`。
- 两分钟验证:`pnpm --filter @objectstack/plugin-auth exec vitest run
--maxWorkers=2 src/password-reset-ends-sessions.test.ts -t 'a
password-state write'`,预期 4 条全绿。
- 需要你做的:无,席位落地。
## What changes
**Step 0, on `origin/main` 5b7269f.** A scratch probe (not committed)
drove both doors over a real `ObjectQL` on better-sqlite3 with
plugin-audit's real `installAuditWriters` and read the `sys_audit_log`
rows for the user. The card's reading reproduces on `main`:
| Write | Door | `actor` | `user_id` |
|---|---|---|---|
| `must_change_password` false → true | admin set-user-password | null |
null |
| `last_login_at` (sign-in stamp) | sign-in | the user | the user |
| `password_changed_at` + `must_change_password` true → false |
change-password | null | null |
The same probe on this head: the set row names the administrator
(`actor` = `user_id` = the admin), the clear row names the user, the
sign-in stamp row and the sign-up stamp row are unchanged.
**How #30989 carried identity into the audit writer** (the mechanism
this PR reuses; no second one):
| Hop | Where | What it carries |
|---|---|---|
| request scope | `auth-manager-request-routing.ts`
`runWithAuthActorScope` around `auth.handler`; a framework route opens
it itself with `runAttributedToUser(actor, …)` | one `AsyncLocalStorage`
scope per request |
| who | `auth-manager-before-hook.ts` `setAuthActorResolver` (the
session's user and `impersonatedBy`); SCIM: the verified connection
(`scimRequestConnectionId`) | resolved lazily, once per request |
| write context | `auth-actor-attribution.ts` `authSystemWriteContext()`
| `isSystem: true` plus `attributedUserId` / `impersonatedBy` /
`performedBy`, or `actor: svc:scim:…` for SCIM |
| engine → audit | ObjectQL `buildProvenance`; plugin-audit
`audit-writers.ts` | `user_id` = `session.userId ??
provenance.attributedUserId`; `actor` = that, else `session.actor` |
Both writes built their context as a bare `{ isSystem: true, positions:
[], permissions: [] }`, so nothing reached the attribution channel.
- **The forced reset** (`admin-user-endpoints.ts`):
`stampMustChangePassword` builds its context as `{ ...SYSTEM_CTX,
...(await authSystemWriteContext()) }`, and `runAdminSetUserPassword`
runs the stamp inside `runAttributedToUser(actor, …)` with the actor the
route admitted (`gateAdmin`, which adds `impersonatedBy` from an
impersonation session). Only the stamp is wrapped: the credential write
and the revocation keep their contexts.
- **The user's change** (`auth-manager-password-session-policy.ts`
`stampPasswordChangedAt`): the context is `{ ...SYSTEM_CTX,
...attribution }` when the seam's `attributedUserId` is the stamped
account itself, else the bare system context. On `/change-password` the
stamped account is the session's own
(`passwordChangeAfterEffectsAccount`), so the clear names the user. The
same stamp runs after `/reset-password` and `/sign-up/email`: the
emailed-link reset carries no session and stays the system's, and a
request whose session names another account never credits that account's
holder.
- **Authorization is unchanged.** Both writes keep `isSystem: true`,
`positions: []` and `permissions: []`; the attribution keys are read by
no security middleware (`auth-actor-attribution.ts` header). No
field-protection or read-only strip is crossed: the written fields and
values are byte-identical.
- **Bounded rider, declared.** `stampMustChangePassword` is also the
create-user door's stamp, and that route already runs inside
`runAttributedToUser(actor, …)` (`auth-plugin.ts`), so the
administrator-created user's `must_change_password` set is now credited
to the creating administrator too. Same defect class, same line, no
other claim on the file, no new gate family; pinned below.
## Tests
New pins, in the existing harnesses (no new test file):
- `password-reset-ends-sessions.test.ts`, top-level `describe('a
password-state write names the person who caused it')`, real `ObjectQL`
+ better-sqlite3 + `AuthManager.handleRequest`, observed on the raw
engine `update` (the #30989 observation point,
`scim-transaction-scope.test.ts`):
- forced reset: the `must_change_password: true` write carries
`attributedUserId` = the administrator, `isSystem: true`, no `userId`;
CONTROL: 200, `data.mustChangePassword: true`, the patch is exactly `{
id, must_change_password: true }`, the stored flag is set;
- forced reset from an impersonation session: `impersonatedBy` rides
beside the administrator;
- change-password: the clear carries `attributedUserId` = the user;
CONTROL: 200, patch keys `id`, `must_change_password`,
`password_changed_at`, stored flag cleared; CONTROL: the sign-in stamp
is the user's, as before;
- negative pin: a stamp inside another account's scope stays the
system's, inside the holder's own scope it is credited; CONTROL: the
emailed-link `/reset-password` stamp stays the system's.
- `admin-user-endpoints.test.ts`, top-level `describe` for the
create-user rider: inside the route's scope the stamp context is the
system's plus `attributedUserId`; outside it, the bare system context.
No existing test was changed.
<details><summary>Ablations (each mutation landed on disk, went red,
restored: blob == HEAD, git diff HEAD empty)</summary>
`node scripts/ablation-replace.mjs`, tests import `src` (no dist leg):
1. `admin-user-endpoints.ts`: `context: { ...SYSTEM_CTX, ...(await
authSystemWriteContext()) },` → `context: SYSTEM_CTX,` — the
forced-reset pin and the impersonation pin red (`attributedUserId:
"admin_password_state"` expected, `null` received; `impersonatedBy:
"admin_at_keyboard"` expected, `null` received); the create-user rider
pin red on the same mutation.
2. `auth-manager-password-session-policy.ts`: the attributed-context
ternary → the bare `SYSTEM_CTX` — the change-password pin and the
own-holder half of the negative pin red (`attributedUserId` = the user
expected, `null` received).
3. same file: `attribution.attributedUserId === userId ?` →
`attribution.attributedUserId !== undefined ?` — the negative pin red
(`attributedUserId: null` expected, `"someone_else"` received).
A first attempt at (2) with the bare replacement `SYSTEM_CTX` was
refused by the tool before running (the replacement text already occurs
in the file) and ran nothing; it was re-run with a unique spelling.
</details>
## Acceptance notes
- The real-writer measurement is step 0's scratch probe: a committed
plugin-auth test cannot import plugin-audit without a new devDependency
(`package.json`, lockfile) or a `CROSS_PACKAGE_TEST_INPUTS` +
`turbo.json` entry, both outside this card's file surface. The committed
pins observe the engine context, as #30989's plugin-auth half does;
plugin-audit's `audit-writers.test.ts` already pins
`provenance.attributedUserId` → `user_id` / `actor`.
## Verification
All at head `d47e8cd6` (base `5b7269ff`; `origin/main` has since moved
by commits touching none of `plugin-auth`, `plugin-audit` or `objectql`,
so no merge).
- `pnpm --filter @objectstack/plugin-auth test` — Test Files 246 passed
(246), Tests 4411 passed | 10 skipped (4421); lock VERDICT command-exit
0.
- `pnpm --filter @objectstack/plugin-auth typecheck` (after a full
closure build with declarations) — exit 0; `check:test-typecheck` OK,
the ledger held (10 files / 72 errors / 23 signatures, unchanged).
- Package lint, `eslint --no-inline-config packages/plugins/plugin-auth`
(the package has no `lint` script) — exit 0.
- New pins alone: `vitest run src/password-reset-ends-sessions.test.ts
-t 'a password-state write'` 4/4; `src/admin-user-endpoints.test.ts`
100/100.
<details><summary>Derived gate list (dispatch-gates --commands, 68
families) and --ran reconciliation</summary>
`node scripts/pm/dispatch-gates.mjs --ran FILE` — 68 derived, 67 run, 1
NOT-MEASURED, 0 UNRUN. Every run family exited 0.
- `check-plugin-teardown-shape.mjs --self-test` first exited 3 (fixture
commit unreachable on the shallow clone); after `git fetch --unshallow
origin main` it exited 0 (48 cases).
- `check:dts-closure` first exited 1: the first closure build ran with
`OS_SKIP_DTS=1`; after a forced build with declarations it exited 0 (81
packages, 256/256 declaration files).
- NOT MEASURED: `check:dual-build-cjs-loads` (exit 3, PREREQUISITE NOT
MET: 43 packages outside this card's build closure have no `dist`). CI's
run is the reading.
</details>
Dogfood battery: n/a (no `!` in the title, no `(narrowing)` arm).
---
_Generated by [Claude
Code](https://claude.ai/code/session_01ADHkmShTSNgBjG5FV5GMXD)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
fix(plugin-auth): the forced-reset flag and its clear are audited under the person who caused them
6
+
7
+
Clause-②: no
8
+
Metadata-protocol: none
9
+
10
+
An administrator's forced password reset (`POST /api/v1/auth/admin/set-user-password`) and the user's own password change (`POST /api/v1/auth/change-password`) each write `sys_user.must_change_password`, and plugin-audit recorded both writes with a null `actor` and `user_id`, as if the system had made them. Both writes now carry their caller on the attribution seam every identity write already uses (`authSystemWriteContext`), so the `sys_audit_log` row for the set names the administrator (and, from an impersonation session, the administrator at the keyboard as `impersonated_by`), and the row for the clear names the user.
11
+
12
+
-**Unchanged.** Both writes still run as the system: what they may write, the values they write and both doors' answers are the same. The sign-in stamp row is unchanged. A password change is credited only to the account's own holder: a request whose session names nobody (the emailed-link reset) or another account leaves the row the system's, as before.
13
+
-**Also attributed.** The administrator-created user's `must_change_password` set (`POST /api/v1/auth/admin/create-user`) goes through the same write and is now credited to the creating administrator.
0 commit comments