@fuzdev/fuz_app 0.182.0 → 0.183.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/README.md +2 -0
  2. package/dist/actions/CLAUDE.md +4 -4
  3. package/dist/auth/CLAUDE.md +58 -34
  4. package/dist/auth/account_routes.d.ts +5 -3
  5. package/dist/auth/account_routes.d.ts.map +1 -1
  6. package/dist/auth/account_routes.js +71 -66
  7. package/dist/auth/admin_actions.d.ts.map +1 -1
  8. package/dist/auth/admin_actions.js +22 -17
  9. package/dist/auth/audit_emitter.d.ts +75 -84
  10. package/dist/auth/audit_emitter.d.ts.map +1 -1
  11. package/dist/auth/audit_emitter.js +83 -71
  12. package/dist/auth/audit_log_schema.d.ts +10 -5
  13. package/dist/auth/audit_log_schema.d.ts.map +1 -1
  14. package/dist/auth/audit_log_schema.js +22 -6
  15. package/dist/auth/bootstrap_account.d.ts +31 -13
  16. package/dist/auth/bootstrap_account.d.ts.map +1 -1
  17. package/dist/auth/bootstrap_account.js +34 -15
  18. package/dist/auth/bootstrap_routes.d.ts +1 -1
  19. package/dist/auth/bootstrap_routes.d.ts.map +1 -1
  20. package/dist/auth/bootstrap_routes.js +7 -14
  21. package/dist/auth/deps.d.ts +8 -5
  22. package/dist/auth/deps.d.ts.map +1 -1
  23. package/dist/auth/deps.js +3 -2
  24. package/dist/auth/resolved_auth.d.ts +7 -7
  25. package/dist/auth/resolved_auth.d.ts.map +1 -1
  26. package/dist/auth/resolved_auth.js +4 -4
  27. package/dist/auth/role_grant_offer_actions.d.ts +25 -1
  28. package/dist/auth/role_grant_offer_actions.d.ts.map +1 -1
  29. package/dist/auth/role_grant_offer_actions.js +40 -33
  30. package/dist/auth/role_grant_offer_queries.d.ts +60 -4
  31. package/dist/auth/role_grant_offer_queries.d.ts.map +1 -1
  32. package/dist/auth/role_grant_offer_queries.js +79 -5
  33. package/dist/auth/role_grant_queries.d.ts +3 -52
  34. package/dist/auth/role_grant_queries.d.ts.map +1 -1
  35. package/dist/auth/role_grant_queries.js +5 -80
  36. package/dist/auth/self_service_role_actions.d.ts.map +1 -1
  37. package/dist/auth/self_service_role_actions.js +11 -22
  38. package/dist/auth/session_queries.d.ts +6 -3
  39. package/dist/auth/session_queries.d.ts.map +1 -1
  40. package/dist/auth/session_queries.js +20 -11
  41. package/dist/auth/signup_routes.d.ts.map +1 -1
  42. package/dist/auth/signup_routes.js +90 -38
  43. package/dist/db/sql_columns.d.ts +1 -1
  44. package/dist/db/sql_columns.js +1 -1
  45. package/dist/http/CLAUDE.md +15 -10
  46. package/dist/http/db_routes.d.ts +7 -22
  47. package/dist/http/db_routes.d.ts.map +1 -1
  48. package/dist/http/db_routes.js +72 -72
  49. package/dist/http/pending_effects.d.ts +42 -4
  50. package/dist/http/pending_effects.d.ts.map +1 -1
  51. package/dist/http/pending_effects.js +34 -4
  52. package/dist/server/app_server.d.ts +43 -2
  53. package/dist/server/app_server.d.ts.map +1 -1
  54. package/dist/server/app_server.js +57 -3
  55. package/dist/testing/CLAUDE.md +255 -786
  56. package/dist/testing/adversarial_404.d.ts +6 -1
  57. package/dist/testing/adversarial_404.d.ts.map +1 -1
  58. package/dist/testing/adversarial_404.js +38 -17
  59. package/dist/testing/app_server.d.ts +20 -140
  60. package/dist/testing/app_server.d.ts.map +1 -1
  61. package/dist/testing/app_server.js +45 -124
  62. package/dist/testing/audit_drift_guard.d.ts +11 -6
  63. package/dist/testing/audit_drift_guard.d.ts.map +1 -1
  64. package/dist/testing/audit_drift_guard.js +15 -8
  65. package/dist/testing/cross_backend/audit_cross_helpers.d.ts +12 -9
  66. package/dist/testing/cross_backend/audit_cross_helpers.d.ts.map +1 -1
  67. package/dist/testing/cross_backend/audit_cross_helpers.js +13 -14
  68. package/dist/testing/cross_backend/capabilities.d.ts +9 -6
  69. package/dist/testing/cross_backend/capabilities.d.ts.map +1 -1
  70. package/dist/testing/cross_backend/capabilities.js +11 -7
  71. package/dist/testing/cross_backend/capability_mounts.d.ts +103 -0
  72. package/dist/testing/cross_backend/capability_mounts.d.ts.map +1 -0
  73. package/dist/testing/cross_backend/capability_mounts.js +423 -0
  74. package/dist/testing/cross_backend/cell_moderate.js +2 -2
  75. package/dist/testing/cross_backend/cell_relations.d.ts.map +1 -1
  76. package/dist/testing/cross_backend/cell_relations.js +12 -11
  77. package/dist/testing/cross_backend/conformance_principal.d.ts +54 -0
  78. package/dist/testing/cross_backend/conformance_principal.d.ts.map +1 -0
  79. package/dist/testing/cross_backend/conformance_principal.js +283 -0
  80. package/dist/testing/cross_backend/conformance_table.d.ts +2 -50
  81. package/dist/testing/cross_backend/conformance_table.d.ts.map +1 -1
  82. package/dist/testing/cross_backend/conformance_table.js +2 -270
  83. package/dist/testing/cross_backend/credential_deadline.d.ts.map +1 -1
  84. package/dist/testing/cross_backend/credential_deadline.js +2 -0
  85. package/dist/testing/cross_backend/credential_wire.d.ts +1 -1
  86. package/dist/testing/cross_backend/credential_wire.d.ts.map +1 -1
  87. package/dist/testing/cross_backend/db_state_diff.d.ts +72 -0
  88. package/dist/testing/cross_backend/db_state_diff.d.ts.map +1 -0
  89. package/dist/testing/cross_backend/db_state_diff.js +444 -0
  90. package/dist/testing/cross_backend/declared_skips.d.ts +68 -0
  91. package/dist/testing/cross_backend/declared_skips.d.ts.map +1 -0
  92. package/dist/testing/cross_backend/declared_skips.js +132 -0
  93. package/dist/testing/cross_backend/differential.d.ts +11 -2
  94. package/dist/testing/cross_backend/differential.d.ts.map +1 -1
  95. package/dist/testing/cross_backend/differential.js +35 -11
  96. package/dist/testing/cross_backend/differential_normalize.d.ts +12 -0
  97. package/dist/testing/cross_backend/differential_normalize.d.ts.map +1 -1
  98. package/dist/testing/cross_backend/differential_normalize.js +16 -0
  99. package/dist/testing/cross_backend/in_process_setup.d.ts +22 -12
  100. package/dist/testing/cross_backend/in_process_setup.d.ts.map +1 -1
  101. package/dist/testing/cross_backend/in_process_setup.js +151 -134
  102. package/dist/testing/cross_backend/login_security.d.ts +12 -0
  103. package/dist/testing/cross_backend/login_security.d.ts.map +1 -1
  104. package/dist/testing/cross_backend/login_security.js +21 -4
  105. package/dist/testing/cross_backend/request_target_conformance_wire.d.ts +0 -2
  106. package/dist/testing/cross_backend/request_target_conformance_wire.d.ts.map +1 -1
  107. package/dist/testing/cross_backend/request_target_conformance_wire.js +3 -1
  108. package/dist/testing/cross_backend/revocation_drivers.d.ts +18 -5
  109. package/dist/testing/cross_backend/revocation_drivers.d.ts.map +1 -1
  110. package/dist/testing/cross_backend/revocation_drivers.js +18 -10
  111. package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts +1 -1
  112. package/dist/testing/cross_backend/role_grant_cross_helpers.d.ts.map +1 -1
  113. package/dist/testing/cross_backend/role_grant_offer_lifecycle.d.ts.map +1 -1
  114. package/dist/testing/cross_backend/role_grant_offer_lifecycle.js +33 -1
  115. package/dist/testing/cross_backend/role_grant_participation.js +4 -4
  116. package/dist/testing/cross_backend/rpc_adversarial.js +1 -1
  117. package/dist/testing/cross_backend/session_cap.d.ts +2 -2
  118. package/dist/testing/cross_backend/session_cap.d.ts.map +1 -1
  119. package/dist/testing/cross_backend/session_cap.js +22 -11
  120. package/dist/testing/cross_backend/setup.d.ts +31 -1
  121. package/dist/testing/cross_backend/setup.d.ts.map +1 -1
  122. package/dist/testing/cross_backend/setup.js +32 -1
  123. package/dist/testing/cross_backend/spine_suites.d.ts +123 -12
  124. package/dist/testing/cross_backend/spine_suites.d.ts.map +1 -1
  125. package/dist/testing/cross_backend/spine_suites.js +454 -57
  126. package/dist/testing/cross_backend/spine_surface_constants.d.ts +1 -1
  127. package/dist/testing/cross_backend/spine_surface_constants.d.ts.map +1 -1
  128. package/dist/testing/cross_backend/spine_surface_constants.js +1 -0
  129. package/dist/testing/cross_backend/sse_round_trip.d.ts +9 -1
  130. package/dist/testing/cross_backend/sse_round_trip.d.ts.map +1 -1
  131. package/dist/testing/cross_backend/sse_round_trip.js +123 -27
  132. package/dist/testing/cross_backend/testing_backdoor.d.ts.map +1 -1
  133. package/dist/testing/cross_backend/testing_backdoor.js +4 -2
  134. package/dist/testing/cross_backend/testing_reset_actions.d.ts +59 -27
  135. package/dist/testing/cross_backend/testing_reset_actions.d.ts.map +1 -1
  136. package/dist/testing/cross_backend/testing_reset_actions.js +66 -42
  137. package/dist/testing/cross_backend/transport_composition.d.ts +1 -1
  138. package/dist/testing/cross_backend/transport_composition.d.ts.map +1 -1
  139. package/dist/testing/cross_backend/transport_composition.js +7 -3
  140. package/dist/testing/cross_backend/ws_round_trip.d.ts +5 -0
  141. package/dist/testing/cross_backend/ws_round_trip.d.ts.map +1 -1
  142. package/dist/testing/cross_backend/ws_round_trip.js +54 -8
  143. package/dist/testing/data_exposure.d.ts.map +1 -1
  144. package/dist/testing/data_exposure.js +3 -0
  145. package/dist/testing/db.d.ts +0 -32
  146. package/dist/testing/db.d.ts.map +1 -1
  147. package/dist/testing/db.js +0 -86
  148. package/dist/testing/db_entities.d.ts +14 -0
  149. package/dist/testing/db_entities.d.ts.map +1 -1
  150. package/dist/testing/db_entities.js +17 -0
  151. package/dist/testing/db_invariant_queries.d.ts +259 -0
  152. package/dist/testing/db_invariant_queries.d.ts.map +1 -0
  153. package/dist/testing/db_invariant_queries.js +400 -0
  154. package/dist/testing/db_invariants.d.ts +106 -0
  155. package/dist/testing/db_invariants.d.ts.map +1 -0
  156. package/dist/testing/db_invariants.gen.sql.d.ts +11 -0
  157. package/dist/testing/db_invariants.gen.sql.d.ts.map +1 -0
  158. package/dist/testing/db_invariants.gen.sql.js +13 -0
  159. package/dist/testing/db_invariants.js +141 -0
  160. package/dist/testing/db_invariants.sql +152 -0
  161. package/dist/testing/describe_db.d.ts +36 -0
  162. package/dist/testing/describe_db.d.ts.map +1 -0
  163. package/dist/testing/describe_db.js +71 -0
  164. package/dist/testing/integration.d.ts +6 -9
  165. package/dist/testing/integration.d.ts.map +1 -1
  166. package/dist/testing/integration.js +9 -13
  167. package/dist/testing/integration_helpers.d.ts +1 -1
  168. package/dist/testing/integration_helpers.d.ts.map +1 -1
  169. package/dist/testing/rate_limiting.d.ts +1 -1
  170. package/dist/testing/rate_limiting.d.ts.map +1 -1
  171. package/dist/testing/rate_limiting.js +3 -2
  172. package/dist/testing/role_grant_helpers.d.ts +1 -1
  173. package/dist/testing/role_grant_helpers.d.ts.map +1 -1
  174. package/dist/testing/round_trip.d.ts.map +1 -1
  175. package/dist/testing/round_trip.js +3 -0
  176. package/dist/testing/rpc_attack_surface.d.ts +8 -6
  177. package/dist/testing/rpc_attack_surface.d.ts.map +1 -1
  178. package/dist/testing/rpc_attack_surface.js +69 -6
  179. package/dist/testing/rpc_round_trip.d.ts +1 -1
  180. package/dist/testing/rpc_round_trip.d.ts.map +1 -1
  181. package/dist/testing/rpc_round_trip.js +3 -0
  182. package/dist/testing/sse_round_trip.d.ts +1 -1
  183. package/dist/testing/sse_round_trip.d.ts.map +1 -1
  184. package/dist/testing/sse_round_trip.js +13 -4
  185. package/dist/testing/stubs.d.ts +4 -2
  186. package/dist/testing/stubs.d.ts.map +1 -1
  187. package/dist/testing/stubs.js +10 -5
  188. package/dist/testing/test_app.d.ts +166 -0
  189. package/dist/testing/test_app.d.ts.map +1 -0
  190. package/dist/testing/test_app.js +124 -0
  191. package/package.json +1 -1
package/README.md CHANGED
@@ -21,6 +21,8 @@ For more see the <a href="https://github.com/fuzdev/fuz_app/discussions">discuss
21
21
  fuz_app is part of the Fuz stack
22
22
  ([fuz.dev](https://www.fuz.dev/), [@fuzdev](https://github.com/fuzdev)).
23
23
 
24
+ AI disclosure: this codebase is LLM-generated with uneven human review.
25
+
24
26
  > ⚠️ This is a pre-alpha release, not ready for production.
25
27
  > There are no known security vulnerabilities,
26
28
  > and security has been \_the\_ primary focus for the initial release,
@@ -741,10 +741,10 @@ the twin of the Rust `ConnectionCloseTarget::from_audit_event` — then calls
741
741
  (`auth/audit_log_schema.ts`), and a row missing its key (an empty string
742
742
  included) maps to nothing:
743
743
 
744
- - `session` (`session_revoke`) → `{kind: 'session', session_hash: metadata.session_id}`
744
+ - `session` (`session_revoke`, `logout`) → `{kind: 'session', session_hash: metadata.session_id}`
745
745
  - `token` (`token_revoke`) → `{kind: 'token', api_token_id: metadata.token_id}`
746
746
  - `account` (`session_revoke_all`, `token_revoke_all`, `password_change`,
747
- `logout`, `account_delete`, `account_purge`) →
747
+ `account_delete`, `account_purge`) →
748
748
  `{kind: 'account', account_id: target_account_id ?? account_id}`
749
749
  - `role` (`role_grant_revoke`) → `{kind: 'role', account_id: target_account_id ??
750
750
  account_id, role: metadata.role}` when the revoked grant was global
@@ -1000,8 +1000,8 @@ flush against other frames: frames on an admitted socket are not serialized
1000
1000
  `onMessage` — `@hono/node-server` doesn't), so a message's effects can still be
1001
1001
  in flight when the caller reads its reply and when the next message
1002
1002
  dispatches. A test that reads the audit log after a WS reply awaits
1003
- `_testing_drain_effects` first, over an emitter built with
1004
- `track_inflight: true` (`src/lib/testing/CLAUDE.md`). The deferred thunks are
1003
+ `_testing_drain_effects` first, which awaits the emitter's in-flight writes
1004
+ (`src/lib/testing/CLAUDE.md`). The deferred thunks are
1005
1005
  invoked first and the eager writes awaited after, so a revocation's close never
1006
1006
  waits on a slow audit INSERT — held behind one, the socket would stay open on a
1007
1007
  credential already committed gone and dispatch its next frame. The deferred
@@ -38,7 +38,7 @@ over the pool so rows persist when request transactions roll back. See root
38
38
  - `auth/password_argon2.ts` — `hash_password`, `verify_password`, `verify_dummy`, `argon2_password_deps`.
39
39
  - `auth/api_token.ts` — `API_TOKEN_PREFIX` (`secret_fuz_token_`), `ApiTokenId` (the public id, `pipe(string, regex(API_TOKEN_ID_REGEX))`, a `@fuzdev/fuz_schema` schema), `hash_api_token`, `generate_api_token`, and the shape precheck `is_api_token_shaped` (`API_TOKEN_REGEX`: the prefix plus exactly `API_TOKEN_BODY_LENGTH` = 43 base64url chars).
40
40
  - `auth/daemon_token.ts` — `DaemonToken`, `DAEMON_TOKEN_REGEX` + the shape precheck `is_daemon_token_shaped` (43 base64url chars), `DAEMON_TOKEN_HEADER` (`X-Daemon-Token`), `generate_daemon_token`, `validate_daemon_token`, `DaemonTokenState`.
41
- - `auth/bootstrap_account.ts` — `bootstrap_account` (one-shot, `bootstrap_lock`-protected) + `read_bootstrap_token`, the read both it and `check_bootstrap_status` use: the secure read less one trailing LF, a token under `BOOTSTRAP_TOKEN_MIN_BYTES` (32) refused as unreadable with an error log. One transaction holds the whole bootstrap: the lock, the account and grants, the session (`create_capped_session`), and last the burn of the token (`token_store.burn_token`) — a failed burn throws out of the transaction (`BootstrapTokenBurnError`) and answers `token_file_undeletable` with nothing created; the success carries `session_cookie_value`, the signed cookie the route sets. `BootstrapAccountCreated` is what `on_bootstrap` receives.
41
+ - `auth/bootstrap_account.ts` — `bootstrap_account` (one-shot, `bootstrap_lock`-protected) + `read_bootstrap_token`, the read both it and `check_bootstrap_status` use: the secure read less one trailing LF, a token under `BOOTSTRAP_TOKEN_MIN_BYTES` (32) refused as unreadable with an error log. Takes its deps (`audit` among them) and the request's context (`BootstrapAccountContext`: the pool `db`, the effect queues, `client_ip` — a `RouteContext` satisfies it). One transaction holds the whole bootstrap: the lock, the account and grants, the session (`create_capped_session`) and its `Set-Cookie` line (formatted there, as login and signup format theirs, so a refused value rolls the bootstrap back), the `bootstrap` success audit row, and last the burn of the token (`token_store.burn_token`) — a failed burn throws out of the transaction (`BootstrapTokenBurnError`) and answers `token_file_undeletable` with nothing created; the success carries `set_cookie`, the line the route sets. `BootstrapAccountCreated` is what `on_bootstrap` receives.
42
42
  - `auth/bootstrap_token_store.ts` — `BootstrapTokenStore` (`path`, `read_token`, `burn_token`), the one seam to the token file, and `create_file_bootstrap_token_store(deps, path)` over the runtime's secure read and burn. Its read asks the secure read for a single hard link (`require_single_link`): a token file with a second link would outlive its burn under the other name, so it is refused as unreadable, at boot and on a request alike. Twin of the Rust `BootstrapTokenStore` trait, which carries no path (its availability check is handed one).
43
43
 
44
44
  Cross-cutting notes that don't live on any single symbol:
@@ -83,9 +83,9 @@ All take `deps: QueryDeps = {db}` first; `query_validate_api_token` adds `log` a
83
83
  - `auth/account_queries.ts` — account CRUD, actor resolution, password update with verify-write race guard, paged `query_admin_account_list`, and the account-row locks every writer of an account takes first (§Row locks): `query_account_lock_verified` (login) and `query_account_lock_live` (token create), `query_account_lock_live_many` (offer and invite create), `query_account_lock_for_offer_accept`, `query_account_lock_for_revoke`, and the purge's `query_account_lock_admins_for_purge` then `query_account_lock_for_purge`.
84
84
  - `auth/actor_lookup_queries.ts` — batched `actor` ⨝ `account` for the labels arc; active actors of active accounts only.
85
85
  - `auth/actor_search_queries.ts` — case-insensitive prefix search on `actor.name`, scope-filtered when not admin; active actors of active accounts only.
86
- - `auth/role_grant_queries.ts` — idempotent create, IDOR-guarded revoke (with in-tx supersede), scope-aware lookup, role/account predicates, `query_role_grant_revoke_for_scope` parent-scope cascade.
87
- - `auth/role_grant_offer_queries.ts` — offer create/decline/retract/list/history/sweep, atomic `query_accept_offer` with sibling supersede (a recipient deleted in flight answers the dead credential's 401), and the offer-row lockers `query_role_grant_offer_lock_for_account_removal` / `_for_revoke` over `offer_rows_locked_in_id_order` (`role_grant_sql.ts`; §Row locks); error classes `RoleGrantOfferSelfTargetError` / `_AlreadyTerminalError` / `_ExpiredError` / `_NotFoundError` / `_ActorAccountMismatchError` / `_ActorMismatchError`.
88
- - `auth/session_queries.ts` — server-side sessions (blake3-hashed), `create_capped_session` (the one session minter: the row, the cap's evictions, and the signed cookie value — Hono-free, so the bootstrap transaction mints through it; twin of the Rust `create_capped_session`), `query_session_revoke_by_hash_unscoped` (logout only), `query_session_enforce_limit` (transaction-required; returns the evicted sessions' ids — their token hashes — so the caller closes their connections after the commit), `query_session_cleanup_expired` (returns the swept sessions' ids for the same close — `cleanup_expired_sessions` is its caller). No touch/renewal query — `expires_at` is an absolute cap set at mint (`AUTH_SESSION_LIFETIME_MS`). `query_session_is_live(id, account_id)` is the re-read `account_token_create` runs after the account lock: the session that authorized the mint must still exist.
86
+ - `auth/role_grant_queries.ts` — idempotent create, scope-aware lookup, role/account predicates, `query_role_grant_revoke_for_scope` parent-scope cascade.
87
+ - `auth/role_grant_offer_queries.ts` — offer create/decline/retract/list/history/sweep, atomic `query_accept_offer` with sibling supersede (a recipient deleted in flight answers the dead credential's 401), the IDOR-guarded `query_revoke_role_grant` (every revoke's one supersede rule: the revokee's incoming siblings and every offer its actor made, in-tx), and the offer-row lockers `query_role_grant_offer_lock_for_account_removal` / `_for_revoke` over `offer_rows_locked_in_id_order` (`role_grant_sql.ts`; §Row locks); error classes `RoleGrantOfferSelfTargetError` / `_AlreadyTerminalError` / `_ExpiredError` / `_NotFoundError` / `_ActorAccountMismatchError` / `_ActorMismatchError`.
88
+ - `auth/session_queries.ts` — server-side sessions (blake3-hashed), `create_capped_session` (the one session minter: the row, the cap's evictions, and the signed cookie value — Hono-free, so the bootstrap transaction mints through it; twin of the Rust `create_capped_session`), `query_session_revoke_by_hash_unscoped` (logout only), `query_session_enforce_limit` (transaction-required; returns the evicted sessions' ids — their token hashes — so the caller closes their connections after the commit), `query_session_cleanup_expired` (returns the swept sessions' ids for the same close — `cleanup_expired_sessions` is its caller). No touch/renewal query — `expires_at` is an absolute cap set at mint (`AUTH_SESSION_LIFETIME_MS`), on the database clock: `query_create_session` writes `NOW() + INTERVAL '2592000 seconds'` (`AUTH_SESSION_LIFETIME_SQL` — elapsed seconds, as a day interval would follow DST in the session time zone), the Rust insert's statement, so it sits a whole lifetime past the row's `created_at` (a test needing another expiry, past or future, inserts through `testing/db_entities.ts`'s `create_test_session_row`). `query_session_is_live(id, account_id)` is the re-read `account_token_create` runs after the account lock: the session that authorized the mint must still exist.
89
89
  - `auth/api_token_queries.ts` — token validation (`query_validate_api_token`, which records nothing) and the separate fire-and-forget usage record (`query_api_token_touch`, which the bearer leg runs only once the token's account is found live), `query_api_token_live_account` (the by-id, no-touch re-check read; shares the `API_TOKEN_IS_LIVE` predicate with the validate query so the two can't drift), IDOR-guarded revoke, `query_api_token_enforce_limit` (transaction-required; returns the evicted tokens' ids for the same post-commit close).
90
90
  - `auth/invite_queries.ts` — invite create/find/claim/list/delete; `query_invite_claim_unscoped` (scoping enforced upstream by `_find_unclaimed_match_for_update`, which runs inside the signup tx with `FOR UPDATE` so find + claim are atomic).
91
91
  - `auth/app_settings_queries.ts` — load/update for the single-row settings table.
@@ -103,8 +103,10 @@ without a `satisfies keyof Row` clause, since `AppSettings` deliberately
103
103
  doesn't carry `id`). Module direction: `role_grant_offer_queries.ts` imports
104
104
  `role_grant_queries.ts` (the accept path writes through
105
105
  `query_create_role_grant` / `query_role_grant_by_id`) and not the reverse —
106
- the revoke cascades in `role_grant_queries.ts` still _write_ offer rows, which
106
+ the scope teardown in `role_grant_queries.ts` still _writes_ offer rows, which
107
107
  is exactly why the offer const and the shared CTE tail live in the DDL module.
108
+ `query_revoke_role_grant` lives with the offer queries (as in the Rust twin):
109
+ it takes the offer locks and both supersedes.
108
110
  Every const is pinned to the live schema by
109
111
  `src/test/db/column_projections.db.test.ts`. The Rust `fuz_auth` twin carries
110
112
  the same nine consts (names and order), projected through
@@ -127,14 +129,14 @@ upstream in `auth/signup_routes.ts` via `query_invite_find_unclaimed_match_for_u
127
129
  `audit_factory` callback over `create_audit_emitter`; closes over the pool +
128
130
  its registered listeners + optional `AuditLogConfig`. Its methods:
129
131
 
130
- - `emit(ctx, input)` — async, always awaited: a success row inside a request transaction is written in it (a failed INSERT rejects); any other row is a fire-and-forget pool write pushed to `ctx.pending_effects`; a success row's listener fan-out goes on `ctx.post_commit_effects`
132
+ - `emit(ctx, input)` — async, always awaited: a success row is written in the request's transaction (a failed INSERT rejects, and so does a success row with no transaction open); a failure row is a fire-and-forget pool write pushed to `ctx.pending_effects`; a success row's listener fan-out goes on `ctx.post_commit_effects`
131
133
  - `emit_role_grant_target(ctx, auth, input)` — lifts `actor_id` / `account_id` / `ip` boilerplate for role-grant-shape events; `input` is an `AuditEmitRoleGrantInput`
132
- - `emit_pool(input)` — awaitable pool write for code paths without a request transaction (ad-hoc maintenance scripts, a post-commit thunk; not for success audits paired with a mutation — those write in-tx and `notify` post-commit). Writes, then notifies, whatever the outcome
134
+ - `emit_pool(input)` — awaitable pool write for code paths without a request (ad-hoc maintenance scripts; not for success audits paired with a mutation — those write in-tx). Writes, then notifies, whatever the outcome; a success row written here counts toward `get_audit_success_outside_transaction`, which `install_audit_drift_guard` holds at zero
133
135
  - `notify(event)` — fan out an already-written row to listeners (used by in-tx audit batches like `query_accept_offer.audit_events`)
134
136
  - `add_listener(listener)` — listener registration, returning a remover for that registration (idempotent; the same function registered twice is two registrations, each with its own remover). Twin of Rust `fuz_auth` `AuditEmitter::add_listener` in firing order and snapshot-at-notify
135
137
  - `listener_count()` — registered-listener count, for tests / diagnostics
136
- - `drain_inflight()` — test-binary barrier: waits until no `emit` write is in flight, writes started while waiting included. Only an emitter built with `track_inflight: true` tracks; otherwise it resolves at once (production never tracks). Backs `_testing_drain_effects` (twin of Rust `AuditEmitter::drain_inflight` / `new_with_inflight_tracking`)
137
- - `is_tracking_inflight()` — whether the emitter was built with `track_inflight: true`, i.e. whether `drain_inflight` can wait on anything. `_testing_drain_effects` reports it as `tracked`, and `cross_drain_effects` (every `list_audit_events` read) refuses `false` (twin of Rust `AuditEmitter::is_tracking_inflight`)
138
+ - `drain_inflight()` — waits until no pool write is in flight, writes started while waiting included. Every emitter counts them. `AppServer.close` awaits it, bounded by `shutdown_drain_timeout_ms`, before the database closes; it backs `_testing_drain_effects` (twin of Rust `AuditEmitter::drain_inflight`)
139
+ - `inflight_count()` — pool writes in flight now; what a shutdown that gives up at its bound reports (twin of Rust `AuditEmitter::inflight_count`)
138
140
 
139
141
  Listeners are closure-private; they change only through `add_listener` and
140
142
  its removers. `create_app_server` registers the audit-log SSE listener and the
@@ -147,25 +149,27 @@ the next event, and one removed mid-fan-out still hears the in-flight event
147
149
  (converges with the Rust twin's cloned vec).
148
150
 
149
151
  **Where a row is written.** `emit` is async — always `await` it. A **success**
150
- row emitted inside a request transaction (`ctx.db.in_transaction`: an action
151
- with `side_effects`, a route with `transaction: true`) is written in that
152
- transaction, so the change and its row commit together; a failed INSERT
153
- rejects `emit` and the handler's transaction rolls back. A **failure** row, and
154
- a success row with no request transaction open (`/login`, `/password`,
155
- signup, bootstrap — which emit after their own transaction commits), is a
156
- fire-and-forget pool write, fail-open (logged, counted by
152
+ row is written in the transaction of the change it reports
153
+ (`ctx.db.in_transaction`: an action with `side_effects`, a route with
154
+ `transaction: true` — `/logout`, `db_admin_row_delete` — or the transaction
155
+ `/login`, `/password`, signup and bootstrap open around just their writes
156
+ through `transaction_with_post_commit_rollback`, the row last before the
157
+ commit), so the change and its row commit together; a failed INSERT rejects
158
+ `emit` and the transaction rolls back. With no transaction open `emit` refuses
159
+ a success row (throws, writes nothing, and counts it for the drift guard). A
160
+ **failure** row is a fire-and-forget pool write, fail-open (logged, counted by
157
161
  `get_audit_write_failures`). The Rust spine's split is the same
158
- (`route_audit_emit`: `emit_in_tx` for an action's success row, `emit` on the
159
- pool otherwise); the TS `/logout` runs in a route transaction, so its row is
160
- in-transaction where Rust's is pooled. `db_admin_row_delete` is written on the
161
- pool after its commit on both twins (TS: `emit_pool` from a post-commit
162
- thunk).
162
+ (`route_audit_emit`: `emit_in_tx` for every success row, REST and db-admin
163
+ included, `emit` on the pool for failure rows). `seq` follows INSERT order,
164
+ not commit order.
163
165
 
164
166
  **When listeners hear about a row.** The fan-out depends on the outcome: a
165
167
  **failure** row is announced as soon as it is written — the attempt happened
166
168
  whatever the transaction does; a **success** row is announced only after the
167
- request's transaction commits (a thunk on `ctx.post_commit_effects`; after the
168
- handler returns, on a route without one) and never when the handler throws. The listeners act on a success row — the revocation guards
169
+ request's transaction commits (a thunk on `ctx.post_commit_effects`) and never
170
+ when it rolls back — a handler-owned transaction truncates the queue itself
171
+ (`transaction_with_post_commit_rollback`), so a handler that catches its
172
+ failure and answers a refusal announces nothing. The listeners act on a success row — the revocation guards
169
173
  close connections, the audit stream broadcasts it — so announcing one before
170
174
  its commit would close while the revoked credential still reads as valid, and
171
175
  a rolled-back revocation would close anyway. A route that runs without a
@@ -193,13 +197,17 @@ they track the same config. Sample via `get_*`; `reset_*` are test-only.
193
197
  Another counter lives with the emitter (`auth/audit_emitter.ts`,
194
198
  `get_audit_write_failures` / `reset_audit_write_failures`): rows the bound
195
199
  emitter's pool write failed to insert. That write is fail-open too — logged,
196
- counted, the request proceeds — so the count is how a lost row is seen.
200
+ counted, the request proceeds — so the count is how a lost row is seen. A
201
+ third, `get_audit_success_outside_transaction` /
202
+ `reset_audit_success_outside_transaction`, counts success rows written
203
+ outside a transaction (`emit` with none open, which also throws, and a success
204
+ `emit_pool`); `install_audit_drift_guard` holds all of them at zero per test.
197
205
 
198
206
  ### Routes
199
207
 
200
208
  - `auth/account_routes.ts` — `POST /login` / `/logout` / `/password`, `GET /api/account/status`. Constants: `DEFAULT_MAX_SESSIONS = 5`, `DEFAULT_MAX_TOKENS = 10`, `DEFAULT_LOGIN_FAIL_FLOOR_MS = 250`, `DEFAULT_LOGIN_FAIL_JITTER_MS = 25`.
201
209
  - `auth/bootstrap_routes.ts` — `POST /bootstrap` + `check_bootstrap_status` (the boot report: the lock read first; a token file left on a bootstrapped database and an unreadable one on an open window each warn; a missing lock row reads unavailable); `BootstrapStatus` runtime ref (`{available}`); `OnBootstrap`, the post-commit callback over what was created. A failed burn answers 500 `token_file_undeletable` with the window left open; the lock race loser closes its window with no audit row and no charge.
202
- - `auth/signup_routes.ts` — `POST /signup` (open or invite-gated).
210
+ - `auth/signup_routes.ts` — `POST /signup` (open or invite-gated). The `signup` success row is written last in the signup transaction; every unclassified failure writes a `signup` failure row with reason `internal_error` — one before the transaction began (the settings load, the hash, a refused `BEGIN`) carries neither `invite_id` nor `open_signup`, as the Rust spine's pre-transaction row. A failure row's emit that throws is logged, never answered in place of the original error.
203
211
  - `auth/audit_log_routes.ts` — optional `GET /audit/stream` (SSE); list/history are on the RPC surface. Takes `stream: {registry, log}` (an `AuditLogSse` satisfies it). The handler admits the stream in two phases — register pending on the `SseRegistry`, re-read the credential (`revalidate_resolved_auth`) and the acting actor's role grants, admit — so a revocation racing the request opens no stream: 401 / 403 from the re-reads, or a 200 whose body is the connect comment alone when the pending registration was closed.
204
212
  - `auth/auth_guard_resolver.ts` — `fuz_auth_guard_resolver` injected into `apply_route_specs` so the framework stays auth-agnostic. Maps each `RouteAuth` axis to a guard and is the single site every route spec traverses, so it also carries the two invariants the auth shape alone can't (a `required_scope` that isn't a well-formed capability, and one on an unrestricted route).
205
213
 
@@ -241,9 +249,10 @@ tokens (force re-auth everywhere), then clears the session cookie. Declares
241
249
  `credential_types: ['session']` (see ../../../docs/security.md
242
250
  §Credential-channel gating).
243
251
 
244
- **The routes close what they revoke, after the commit.** `/logout` and
245
- `/password` queue an account-wide close, and `/login` queues a session close
246
- for each session its cap evicted (`queue_connection_close` on
252
+ **The routes close what they revoke, after the commit.** `/password` queues an
253
+ account-wide close, `/logout` a session close for the session it ends (the
254
+ account's other sessions and tokens stay valid, and their connections open),
255
+ and `/login` a session close for each session its cap evicted (`queue_connection_close` on
247
256
  `deps.connection_closer`). The queue runs once the route's transaction has
248
257
  committed and is dropped if the handler throws.
249
258
 
@@ -558,10 +567,16 @@ TSDoc. The pattern that spans emit sites:
558
567
  `_supersede`) populate `target_actor_id` only when the offer was
559
568
  actor-targeted at create time (`role_grant_offer.to_actor_id` set).
560
569
  - **Account-shape events** (login, logout, signup, bootstrap, password
561
- change, session/token revoke, app_settings update, invite events) stay
570
+ change, session/token revoke, app_settings update) stay
562
571
  account-grain on **both** `target_actor_id` and `actor_id` — the
563
572
  operation is performed by the account, and a multi-actor user must be
564
573
  able to log out without first picking an acting actor.
574
+ - **Invite events** carry the acting `actor_id` (the handlers are
575
+ actor-grain) and no target — the `invite_delete` rows an account removal
576
+ writes for the invites it voids too, the remover's. A delete of a missing
577
+ invite writes an `outcome: 'failure'` row (`reason`, `attempted_invite_id`
578
+ — a UUID probe key, unlike `invite_create`'s email and username refusals,
579
+ which write none).
565
580
 
566
581
  ### Audit event extensibility
567
582
 
@@ -751,7 +766,7 @@ API-removable — auth + daemon-token both pivot on it),
751
766
  targets the sole remaining active admin (soft-deleted admins don't count; the
752
767
  count locks its rows, so concurrent removals can't both pass), `ERROR_INVITE_ACCOUNT_EXISTS_USERNAME` /
753
768
  `_EMAIL` / `ERROR_INVITE_DUPLICATE` on invite create, `ERROR_INVITE_NOT_FOUND`
754
- on invite delete. `invite_create` empty input is rejected at the schema by a
769
+ on invite delete (with a failure-audit row). `invite_create` empty input is rejected at the schema by a
755
770
  `check` and surfaces as `invalid_params` with `error.data.issues` (DEV-only `WireIssue`s).
756
771
 
757
772
  Closure state:
@@ -814,10 +829,12 @@ the list and history inputs read an omitted input as a fresh `{}`
814
829
  the locked count `query_count_active_accounts_with_global_role_locked`,
815
830
  which leaves the revoked grant out — §Row locks). Besides the revokee's
816
831
  incoming sibling offers, the revoke supersedes every pending offer the
817
- revokee's actor made (`query_role_grant_offer_supersede_outgoing`);
832
+ revokee's actor made — the rule `query_revoke_role_grant` holds for every
833
+ revoke, the self-service disable's too;
818
834
  `account_delete` / `account_purge` supersede both directions for the whole
819
835
  account (`reason: 'account_deleted'`). Every supersede cascade returns —
820
- and audits and notifies — its rows ordered by `created_at, id`; who is
836
+ and audits and notifies — its rows ordered by `created_at, id`; a revoke's
837
+ is two such runs, its incoming siblings then its outgoing offers. Who is
821
838
  told: §WS notifications.
822
839
  - `role_grant_assign_action_spec` — global-admin-only immediate conferral (no
823
840
  consent step); input `{to_account_id, to_actor_id?, role, scope_kind?, scope_id?}`
@@ -912,13 +929,15 @@ Post-commit via `emit_after_commit` (see `http/CLAUDE.md` §Pending Effects):
912
929
  - Decline — `role_grant_offer_declined` → grantor.
913
930
  - Revoke (`role_grant_revoke`) — `role_grant_revoked` → revokee + `_supersede` per superseded offer (the revokee's incoming siblings and its own outgoing offers), each to its grantor and its recipient.
914
931
  - Delete / purge (`account_delete`, `account_purge`) — `_supersede` (`account_deleted`) per superseded offer to the live party: the recipient of each offer the account made, the grantor of each offer made to it. The removed account hears nothing (its sockets close).
915
- - Self-service disable — `_supersede` per incoming sibling, to its grantor and to the caller (its answer lists no offers).
932
+ - Self-service disable — `_supersede` per superseded offer (the caller's incoming siblings and its own outgoing offers), each to its grantor and its recipient, as the admin revoke (the caller's answer lists no offers).
916
933
 
917
934
  `queue_supersede_notifications(ctx, offers, reason, audience)` sends them in
918
935
  the cascade's row order, grantor before recipient; the `SupersedeAudience`
919
936
  names the `cause_id` each told party gets (a recipient told of its grantor's
920
937
  removal gets the grantor's actor, never its account id — TSDoc on
921
- `SupersedeAudience`).
938
+ `SupersedeAudience`). Their audit rows go through `emit_supersede_audits`
939
+ (`auth/role_grant_offer_actions.ts`), shared by both revokes and account
940
+ removal, as the Rust twin's.
922
941
 
923
942
  Spec module is `auth/role_grant_offer_notifications.ts` —
924
943
  `RemoteNotificationActionSpec`s with `@fuzdev/fuz_schema` params schemas and
@@ -1034,6 +1053,11 @@ construction (`validate_self_service_eligible_roles`): a builtin role or a name
1034
1053
  outside the `RoleName` grammar throws — self-toggling a builtin would hand any
1035
1054
  account instance-wide authority.
1036
1055
 
1056
+ A disable revokes through `query_revoke_role_grant`, so it ends what the admin
1057
+ revoke ends: the caller's pending offers of the role and every offer its actor
1058
+ made, whatever the role, each audited (`role_grant_offer_supersede`,
1059
+ `role_grant_revoked`, the grant as cause) and told to both parties.
1060
+
1037
1061
  Deps are `RevokingActionFactoryDeps`: a disable that revokes queues a close of
1038
1062
  the caller's connections gated on the role (`queue_connection_close`,
1039
1063
  `{kind: 'role'}`), after the commit and independent of the audit write.
@@ -140,14 +140,16 @@ export interface AccountRouteOptions extends AuthSessionRouteOptions {
140
140
  * every account surface serves it, matching the Rust `account_router`.
141
141
  * Self-service session/token management is on `auth/account_actions.ts`.
142
142
  *
143
- * `/login` closes the connections of the sessions its cap evicts, and
144
- * `/logout` and `/password` close the account's, each once its revocation has
143
+ * `/login` closes the connections of the sessions its cap evicts, `/logout`
144
+ * those of the session it ends, and `/password` the account's, each once its revocation has
145
145
  * committed: `/logout` through the route transaction's post-commit queue
146
146
  * (`queue_connection_close`), `/login` and `/password` — which open their own
147
147
  * transaction around just their writes — straight after it commits
148
148
  * (`close_connections_after_commit`, which logs a failing close rather than
149
149
  * failing the committed request), so a later throw can't drop a close a
150
- * committed revocation owes. All on `deps.connection_closer`.
150
+ * committed revocation owes. All on `deps.connection_closer`. Each success
151
+ * audit row is written last in the transaction of the change it reports, so
152
+ * a refused row rolls the change back and closes nothing.
151
153
  *
152
154
  * @param deps - stateless capabilities (keyring, password, log, audit, connection_closer)
153
155
  * @param options - per-factory configuration (session_options, login_ip_rate_limiter, login_account_rate_limiter, bootstrap_status)
@@ -1 +1 @@
1
- {"version":3,"file":"account_routes.d.ts","sourceRoot":"","sources":["../../src/lib/auth/account_routes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAGN,KAAK,cAAc,EACnB,MAAM,qBAAqB,CAAC;AAyB7B,OAAO,EAAwB,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAI7E,OAAO,EAAoB,KAAK,WAAW,EAA6B,MAAM,oBAAoB,CAAC;AACnG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAclD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,gCAAgC,GAAI,UAAU,oBAAoB,KAAG,SAiFjF,CAAC;AAEF,iDAAiD;AACjD,MAAM,WAAW,oBAAoB;IACpC,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8FAA8F;IAC9F,gBAAgB,CAAC,EAAE;QAAE,SAAS,EAAE,OAAO,CAAA;KAAE,CAAC;CAC1C;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,KAAK,CAAC;AAE/C;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,uBAAuB;IACvC,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;CACxC;AAED;;GAEG;AACH,MAAM,WAAW,mBAAoB,SAAQ,uBAAuB;IACnE;;;;;;;OAOG;IACH,qBAAqB,EAAE,WAAW,GAAG,IAAI,CAAC;IAC1C,4FAA4F;IAC5F,0BAA0B,EAAE,WAAW,GAAG,IAAI,CAAC;IAC/C,2FAA2F;IAC3F,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;;;;OAOG;IACH,gBAAgB,CAAC,EAAE;QAAE,SAAS,EAAE,OAAO,CAAA;KAAE,CAAC;CAC1C;AAID;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,eAAO,MAAM,0BAA0B,GACtC,MAAM,gBAAgB,EACtB,SAAS,mBAAmB,KAC1B,KAAK,CAAC,SAAS,CAkXjB,CAAC"}
1
+ {"version":3,"file":"account_routes.d.ts","sourceRoot":"","sources":["../../src/lib/auth/account_routes.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAGN,KAAK,cAAc,EACnB,MAAM,qBAAqB,CAAC;AAyB7B,OAAO,EAAwB,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAK7E,OAAO,EAAoB,KAAK,WAAW,EAA6B,MAAM,oBAAoB,CAAC;AACnG,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAclD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,gCAAgC,GAAI,UAAU,oBAAoB,KAAG,SAiFjF,CAAC;AAEF,iDAAiD;AACjD,MAAM,WAAW,oBAAoB;IACpC,yDAAyD;IACzD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8FAA8F;IAC9F,gBAAgB,CAAC,EAAE;QAAE,SAAS,EAAE,OAAO,CAAA;KAAE,CAAC;CAC1C;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,4BAA4B,KAAK,CAAC;AAE/C;;;;;;;;;;;;GAYG;AACH,MAAM,WAAW,uBAAuB;IACvC,eAAe,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC;CACxC;AAED;;GAEG;AACH,MAAM,WAAW,mBAAoB,SAAQ,uBAAuB;IACnE;;;;;;;OAOG;IACH,qBAAqB,EAAE,WAAW,GAAG,IAAI,CAAC;IAC1C,4FAA4F;IAC5F,0BAA0B,EAAE,WAAW,GAAG,IAAI,CAAC;IAC/C,2FAA2F;IAC3F,YAAY,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B;;;;;;;OAOG;IACH,gBAAgB,CAAC,EAAE;QAAE,SAAS,EAAE,OAAO,CAAA;KAAE,CAAC;CAC1C;AAID;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,0BAA0B,GACtC,MAAM,gBAAgB,EACtB,SAAS,mBAAmB,KAC1B,KAAK,CAAC,SAAS,CAkXjB,CAAC"}
@@ -29,6 +29,7 @@ import { build_request_context, get_public_account, resolve_acting_actor } from
29
29
  import { resolved_auth_session_hash } from './resolved_auth.js';
30
30
  import { attach_route_handler } from '../http/route_spec.js';
31
31
  import { route_fail, route_ok } from '../http/route_result.js';
32
+ import { transaction_with_post_commit_rollback } from '../http/pending_effects.js';
32
33
  import { rate_limit_ip_key } from '../http/ip_canonical.js';
33
34
  import { parse_acting } from '../http/auth_shape.js';
34
35
  import { reserve_optional } from '../rate_limiter.js';
@@ -168,14 +169,16 @@ export const DEFAULT_LOGIN_FAIL_JITTER_MS = 25;
168
169
  * every account surface serves it, matching the Rust `account_router`.
169
170
  * Self-service session/token management is on `auth/account_actions.ts`.
170
171
  *
171
- * `/login` closes the connections of the sessions its cap evicts, and
172
- * `/logout` and `/password` close the account's, each once its revocation has
172
+ * `/login` closes the connections of the sessions its cap evicts, `/logout`
173
+ * those of the session it ends, and `/password` the account's, each once its revocation has
173
174
  * committed: `/logout` through the route transaction's post-commit queue
174
175
  * (`queue_connection_close`), `/login` and `/password` — which open their own
175
176
  * transaction around just their writes — straight after it commits
176
177
  * (`close_connections_after_commit`, which logs a failing close rather than
177
178
  * failing the committed request), so a later throw can't drop a close a
178
- * committed revocation owes. All on `deps.connection_closer`.
179
+ * committed revocation owes. All on `deps.connection_closer`. Each success
180
+ * audit row is written last in the transaction of the change it reports, so
181
+ * a refused row rolls the change back and closes nothing.
179
182
  *
180
183
  * @param deps - stateless capabilities (keyring, password, log, audit, connection_closer)
181
184
  * @param options - per-factory configuration (session_options, login_ip_rate_limiter, login_account_rate_limiter, bootstrap_status)
@@ -255,35 +258,38 @@ export const create_account_route_specs = (deps, options) => {
255
258
  return route_fail(REST_INVALID_CREDENTIALS);
256
259
  }
257
260
  const valid = await password.verify_password(pw, account.password_hash);
258
- // The session INSERT and the cap eviction are the route's only
259
- // writes, and only atomic together: one transaction around just
260
- // them (the route is `transaction: false`, so the reservation
261
- // waits above and the Argon2 verify held no connection). Twin of
262
- // the Rust login's transaction around `create_capped_session`. It
261
+ // The session INSERT, the cap eviction, and the `login` success
262
+ // row are the route's writes, and only atomic together: one
263
+ // transaction around just them (the route is `transaction: false`,
264
+ // so the reservation waits above and the Argon2 verify held no
265
+ // connection). Twin of the Rust login's `mint_login_session`. It
263
266
  // first locks the account row as the verify read it — live, the
264
267
  // verified hash — so a password change or a delete that committed
265
268
  // since mints nothing, and one still to commit waits and then
266
269
  // revokes this session (`query_account_lock_verified`). The
267
270
  // cookie line is formatted inside it, so a value the formatter
268
271
  // refuses rolls the session back, and rides the success, which is
269
- // answered only once the commit has landed.
272
+ // answered only once the commit has landed. The audit row is
273
+ // written last, so a refused row mints no session.
270
274
  const minted = valid
271
- ? await route.db.transaction(async (tx) => {
272
- const tx_deps = { db: tx };
273
- if (!(await query_account_lock_verified(tx_deps, account.id, account.password_hash))) {
275
+ ? await transaction_with_post_commit_rollback(route, async (tx_ctx) => {
276
+ if (!(await query_account_lock_verified(tx_ctx, account.id, account.password_hash))) {
274
277
  return null;
275
278
  }
276
279
  const session = await create_capped_session({
277
280
  keyring,
278
- deps: tx_deps,
281
+ deps: tx_ctx,
279
282
  account_id: account.id,
280
283
  session_options,
281
284
  max_sessions
282
285
  });
283
- return {
284
- evicted: session.evicted,
285
- set_cookie: session_cookie_header(session.cookie_value, session_options)
286
- };
286
+ const set_cookie = session_cookie_header(session.cookie_value, session_options);
287
+ await deps.audit.emit(tx_ctx, {
288
+ event_type: 'login',
289
+ account_id: account.id,
290
+ ip: route.client_ip
291
+ });
292
+ return { evicted: session.evicted, set_cookie };
287
293
  })
288
294
  : null;
289
295
  // A wrong password, and a right one whose account changed under
@@ -319,16 +325,11 @@ export const create_account_route_specs = (deps, options) => {
319
325
  // rechecks. Close them now the eviction has committed — inline, not
320
326
  // queued: with no route transaction, a throw later in the handler
321
327
  // would drop a queued close while the eviction stands. No audit
322
- // row: there is no eviction event, and the `login` row records
323
- // the cause.
328
+ // row of their own: there is no eviction event, and the `login`
329
+ // row records the cause.
324
330
  for (const session_hash of evicted) {
325
331
  close_connections_after_commit(connection_closer, { kind: 'session', session_hash }, deps.log);
326
332
  }
327
- await deps.audit.emit(route, {
328
- event_type: 'login',
329
- account_id: account.id,
330
- ip: route.client_ip
331
- });
332
333
  return route_ok({ ok: true }, { set_cookie: [set_cookie] });
333
334
  }
334
335
  finally {
@@ -341,28 +342,31 @@ export const create_account_route_specs = (deps, options) => {
341
342
  // the shape admits only a session credential, so this is its hash
342
343
  // (the Rust `account_logout` reads the same value)
343
344
  const session_hash = resolved_auth_session_hash(route.credential);
344
- if (session_hash !== null) {
345
- await query_session_revoke_by_hash_unscoped(route, session_hash);
346
- // Close this account's live connections once the revoke has
347
- // committed — queued, so it never runs before the commit or on
348
- // rollback, and it does not depend on the audit row below (written
349
- // in the route transaction, so it commits with the revoke). Account-wide (not session-targeted) to match the Rust
350
- // `account_logout` handler and the sibling `/password` handler
351
- // — logout is a self-initiated account-grain operation. The
352
- // audit listeners repeat the same close on the announced
353
- // `logout` row; closing is idempotent.
354
- queue_connection_close(route, connection_closer, {
355
- kind: 'account',
356
- account_id: ctx.account.id
357
- });
345
+ if (session_hash === null) {
346
+ // a 500 with nothing changed, as the Rust handler answers
347
+ throw new Error('logout: the session gate admitted a caller with no session hash');
358
348
  }
349
+ await query_session_revoke_by_hash_unscoped(route, session_hash);
350
+ // Close the live connections of the session this logout ends, once
351
+ // the revoke has committed — queued, so it never runs before the
352
+ // commit or on rollback, and it does not depend on the audit row below
353
+ // (written in the route transaction, so it commits with the revoke).
354
+ // Only that session's: logout ends one credential, and the account's
355
+ // other sessions, its API tokens and the daemon token stay valid, so
356
+ // their connections stay open. The audit listeners repeat the same
357
+ // close on the announced `logout` row, which names the session;
358
+ // closing is idempotent. The Rust `account_logout` closes the same.
359
+ queue_connection_close(route, connection_closer, { kind: 'session', session_hash });
359
360
  // Account-grain operation — no `actor_id` (which actor was
360
361
  // resolved per-request is incidental to "this account ended
361
- // its session"). Mirrors `login`.
362
+ // its session"). Mirrors `login`. `session_id` names the ended
363
+ // session, as a `session_revoke` row does — what the revocation
364
+ // guard closes by.
362
365
  await deps.audit.emit(route, {
363
366
  event_type: 'logout',
364
367
  account_id: ctx.account.id,
365
- ip: route.client_ip
368
+ ip: route.client_ip,
369
+ metadata: { session_id: session_hash }
366
370
  });
367
371
  // the clear rides the success, answered once the revoke and this
368
372
  // row have committed — a rollback clears nothing
@@ -420,20 +424,31 @@ export const create_account_route_specs = (deps, options) => {
420
424
  // operation — `updated_by` stays null (the per-request actor is
421
425
  // incidental; password is account-level state).
422
426
  //
423
- // The UPDATE and the two revocations are the route's writes, and
424
- // commit together or not at all: one transaction around just them
425
- // (the route is `transaction: false`, so the reservation waits
426
- // above and the Argon2 verify and hash held no connection). Twin of
427
- // the Rust spine's `change_password`. `null` when the UPDATE matched
428
- // nothing — a concurrent change committed first.
429
- const change = await route.db.transaction(async (tx) => {
430
- const tx_deps = { db: tx };
431
- const updated = await query_update_account_password(tx_deps, ctx.account.id, new_hash, null, ctx.account.password_hash);
427
+ // The UPDATE, the two revocations, and the `password_change`
428
+ // success row are the route's writes, and commit together or not
429
+ // at all: one transaction around just them (the route is
430
+ // `transaction: false`, so the reservation waits above and the
431
+ // Argon2 verify and hash held no connection). Twin of the Rust
432
+ // spine's `change_password`. The audit row is written last, so a
433
+ // refused row changes no password and revokes nothing. `null` when
434
+ // the UPDATE matched nothing — a concurrent change committed first.
435
+ const change = await transaction_with_post_commit_rollback(route, async (tx_ctx) => {
436
+ const updated = await query_update_account_password(tx_ctx, ctx.account.id, new_hash, null, ctx.account.password_hash);
432
437
  if (!updated)
433
438
  return null;
434
439
  // revoke all sessions and API tokens (force re-auth everywhere)
435
- const sessions_revoked = await query_session_revoke_all_for_account(tx_deps, ctx.account.id);
436
- const tokens_revoked = await query_revoke_all_api_tokens_for_account(tx_deps, ctx.account.id);
440
+ const sessions_revoked = await query_session_revoke_all_for_account(tx_ctx, ctx.account.id);
441
+ const tokens_revoked = await query_revoke_all_api_tokens_for_account(tx_ctx, ctx.account.id);
442
+ // Account-grain operation — no `actor_id`. The password is
443
+ // account-level state; which per-request actor was resolved
444
+ // has no semantic bearing on "this account changed its
445
+ // password". Mirrors `login`/`logout`.
446
+ await deps.audit.emit(tx_ctx, {
447
+ event_type: 'password_change',
448
+ account_id: ctx.account.id,
449
+ ip: route.client_ip,
450
+ metadata: { sessions_revoked, tokens_revoked, credential_type }
451
+ });
437
452
  return { sessions_revoked, tokens_revoked };
438
453
  });
439
454
  // Verify-success contract — the caller proved knowledge, so wipe
@@ -471,22 +486,12 @@ export const create_account_route_specs = (deps, options) => {
471
486
  }
472
487
  const { sessions_revoked, tokens_revoked } = change;
473
488
  // Close every live connection of this account now the password
474
- // update and the revoke-all cascade have committed — inline, not
475
- // queued: with no route transaction, a throw later in the handler
476
- // would drop a queued close while the revocation stands. It does
477
- // not depend on the fail-open audit write below; the audit
478
- // listeners repeat it on the announced `password_change` row.
489
+ // update, the revoke-all cascade, and its audit row have committed
490
+ // — inline, not queued: with no route transaction, a throw later in
491
+ // the handler would drop a queued close while the revocation
492
+ // stands. The audit listeners repeat it on the announced
493
+ // `password_change` row.
479
494
  close_connections_after_commit(connection_closer, { kind: 'account', account_id: ctx.account.id }, deps.log);
480
- // Account-grain operation — no `actor_id`. The password is
481
- // account-level state; which per-request actor was resolved
482
- // has no semantic bearing on "this account changed its
483
- // password". Mirrors `login`/`logout`.
484
- await deps.audit.emit(route, {
485
- event_type: 'password_change',
486
- account_id: ctx.account.id,
487
- ip: route.client_ip,
488
- metadata: { sessions_revoked, tokens_revoked, credential_type }
489
- });
490
495
  return route_ok({ ok: true, sessions_revoked, tokens_revoked }, { set_cookie: [clear_session_cookie_header(session_options)] });
491
496
  }
492
497
  finally {
@@ -1 +1 @@
1
- {"version":3,"file":"admin_actions.d.ts","sourceRoot":"","sources":["../../src/lib/auth/admin_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAIjG,OAAO,EAKN,KAAK,YAAY,EACjB,MAAM,kBAAkB,CAAC;AAwD1B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AA2D3D,0CAA0C;AAC1C,MAAM,WAAW,kBAAkB;IAClC;;;;;OAKG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC;CACrB;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,oBAAoB,GAChC,MAAM,yBAAyB,EAC/B,UAAS,kBAAuB,KAC9B,KAAK,CAAC,SAAS,CAmpBjB,CAAC"}
1
+ {"version":3,"file":"admin_actions.d.ts","sourceRoot":"","sources":["../../src/lib/auth/admin_actions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,EAAuC,KAAK,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAIjG,OAAO,EAKN,KAAK,YAAY,EACjB,MAAM,kBAAkB,CAAC;AAyD1B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AA2D3D,0CAA0C;AAC1C,MAAM,WAAW,kBAAkB;IAClC;;;;;OAKG;IACH,KAAK,CAAC,EAAE,YAAY,CAAC;CACrB;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,oBAAoB,GAChC,MAAM,yBAAyB,EAC/B,UAAS,kBAAuB,KAC9B,KAAK,CAAC,SAAS,CAupBjB,CAAC"}
@@ -37,6 +37,7 @@ import { has_scoped_role } from './request_context.js';
37
37
  import { query_account_has_global_role, query_account_has_active_global_role, query_count_active_accounts_with_global_role_locked } from './role_grant_queries.js';
38
38
  import { GRANT_PATH_ADMIN } from './grant_path_schema.js';
39
39
  import { query_role_grant_offer_lock_for_account_removal, query_role_grant_offer_supersede_incoming, query_role_grant_offer_supersede_outgoing } from './role_grant_offer_queries.js';
40
+ import { emit_supersede_audits } from './role_grant_offer_actions.js';
40
41
  import { queue_supersede_notifications } from './role_grant_offer_notifications.js';
41
42
  import { query_account_by_email, query_account_by_id, query_account_by_username, query_account_lock_admins_for_purge, query_account_lock_for_purge, query_account_lock_live_many, query_account_soft_delete, query_account_undelete, query_actor_soft_delete, query_actor_undelete, query_actors_by_account, query_admin_account_list, query_purge_account } from './account_queries.js';
42
43
  import { query_session_list_all_active, query_session_revoke_all_for_account } from './session_queries.js';
@@ -195,6 +196,7 @@ export const create_admin_actions = (deps, options = {}) => {
195
196
  }
196
197
  await deps.audit.emit(ctx, {
197
198
  event_type: 'invite_create',
199
+ actor_id: auth.actor.id,
198
200
  account_id: auth.account.id,
199
201
  ip: ctx.client_ip,
200
202
  metadata: { invite_id: invite.id, email, username }
@@ -209,10 +211,24 @@ export const create_admin_actions = (deps, options = {}) => {
209
211
  const auth = ctx.auth;
210
212
  const deleted = await query_invite_delete_unclaimed(ctx, input.invite_id);
211
213
  if (!deleted) {
214
+ // The probe key is a UUID, so a failure row leaks nothing an admin's
215
+ // typo would (unlike `invite_create`'s email and username refusals,
216
+ // which write none), and a real admin aiming at a missing invite is
217
+ // forensic signal. Written through the pool, so it outlives the
218
+ // rollback this refusal causes. Twin of the Rust handler's row.
219
+ await deps.audit.emit(ctx, {
220
+ event_type: 'invite_delete',
221
+ outcome: 'failure',
222
+ actor_id: auth.actor.id,
223
+ account_id: auth.account.id,
224
+ ip: ctx.client_ip,
225
+ metadata: { reason: ERROR_INVITE_NOT_FOUND, attempted_invite_id: input.invite_id }
226
+ });
212
227
  throw jsonrpc_errors.not_found('invite', { reason: ERROR_INVITE_NOT_FOUND });
213
228
  }
214
229
  await deps.audit.emit(ctx, {
215
230
  event_type: 'invite_delete',
231
+ actor_id: auth.actor.id,
216
232
  account_id: auth.account.id,
217
233
  ip: ctx.client_ip,
218
234
  metadata: { invite_id: input.invite_id }
@@ -269,20 +285,7 @@ export const create_admin_actions = (deps, options = {}) => {
269
285
  // other party — `audience`, the side the removed account is not on, with the
270
286
  // `cause_id` that side is sent; the account's own connections close with it.
271
287
  const emit_account_deleted_supersedes = async (ctx, target_account_id, superseded, audience) => {
272
- for (const offer of superseded) {
273
- await deps.audit.emit_role_grant_target(ctx, ctx.auth, {
274
- event_type: 'role_grant_offer_supersede',
275
- target_account_id: offer.to_account_id,
276
- target_actor_id: offer.to_actor_id,
277
- metadata: {
278
- offer_id: offer.id,
279
- role: offer.role,
280
- scope_id: offer.scope_id,
281
- reason: 'account_deleted',
282
- cause_id: target_account_id
283
- }
284
- });
285
- }
288
+ await emit_supersede_audits(ctx, deps.audit, superseded, 'account_deleted', target_account_id);
286
289
  queue_supersede_notifications(ctx, superseded, 'account_deleted', audience);
287
290
  };
288
291
  // Lock every pending offer the removal supersedes, both directions, in one
@@ -317,15 +320,17 @@ export const create_admin_actions = (deps, options = {}) => {
317
320
  });
318
321
  };
319
322
  // Delete the unclaimed invites the removed account's actors issued, each
320
- // audited as an `invite_delete` is. An invite records no authority, so one
321
- // outliving its issuer's account would still admit a signup; deleted, it
322
- // answers a signup as a revoked one does, and an undelete can't revive it.
323
+ // audited as an `invite_delete` call is — the remover's actor and account.
324
+ // An invite records no authority, so one outliving its issuer's account
325
+ // would still admit a signup; deleted, it answers a signup as a revoked one
326
+ // does, and an undelete can't revive it.
323
327
  // A claimed invite is untouched.
324
328
  const void_unclaimed_invites = async (ctx, actors) => {
325
329
  const invite_ids = await query_invite_delete_unclaimed_by_creators(ctx, actors.map((a) => a.id));
326
330
  for (const invite_id of invite_ids) {
327
331
  await deps.audit.emit(ctx, {
328
332
  event_type: 'invite_delete',
333
+ actor_id: ctx.auth.actor.id,
329
334
  account_id: ctx.auth.account.id,
330
335
  ip: ctx.client_ip,
331
336
  metadata: { invite_id }