@fuzdev/fuz_app 0.165.0 → 0.167.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 (119) hide show
  1. package/dist/actions/CLAUDE.md +3 -3
  2. package/dist/actions/action_codegen.d.ts +10 -1
  3. package/dist/actions/action_codegen.d.ts.map +1 -1
  4. package/dist/actions/action_codegen.js +17 -8
  5. package/dist/actions/action_event_data.d.ts +1 -1
  6. package/dist/actions/action_rpc.d.ts.map +1 -1
  7. package/dist/actions/action_rpc.js +24 -15
  8. package/dist/actions/action_spec.d.ts +2 -2
  9. package/dist/actions/perform_action.d.ts +2 -1
  10. package/dist/actions/perform_action.d.ts.map +1 -1
  11. package/dist/auth/CLAUDE.md +10 -8
  12. package/dist/auth/account_action_specs.d.ts +8 -8
  13. package/dist/auth/account_route_schema.d.ts +197 -164
  14. package/dist/auth/account_route_schema.d.ts.map +1 -1
  15. package/dist/auth/account_route_schema.js +19 -17
  16. package/dist/auth/account_routes.d.ts.map +1 -1
  17. package/dist/auth/account_routes.js +285 -294
  18. package/dist/auth/admin_action_specs.d.ts +64 -64
  19. package/dist/auth/audit_log_route_schema.d.ts +16 -8
  20. package/dist/auth/audit_log_route_schema.d.ts.map +1 -1
  21. package/dist/auth/audit_log_route_schema.js +4 -3
  22. package/dist/auth/audit_log_routes.d.ts +1 -1
  23. package/dist/auth/audit_log_routes.d.ts.map +1 -1
  24. package/dist/auth/audit_log_routes.js +112 -111
  25. package/dist/auth/audit_log_schema.d.ts +22 -22
  26. package/dist/auth/bootstrap_route_schema.d.ts +46 -69
  27. package/dist/auth/bootstrap_route_schema.d.ts.map +1 -1
  28. package/dist/auth/bootstrap_route_schema.js +8 -7
  29. package/dist/auth/bootstrap_routes.d.ts.map +1 -1
  30. package/dist/auth/bootstrap_routes.js +100 -98
  31. package/dist/auth/cell_action_specs.d.ts +6092 -1092
  32. package/dist/auth/cell_action_specs.d.ts.map +1 -1
  33. package/dist/auth/cell_action_specs.js +96 -97
  34. package/dist/auth/cell_actions.d.ts.map +1 -1
  35. package/dist/auth/cell_actions.js +1 -0
  36. package/dist/auth/cell_audit_action_specs.d.ts +301 -69
  37. package/dist/auth/cell_audit_action_specs.d.ts.map +1 -1
  38. package/dist/auth/cell_audit_action_specs.js +21 -16
  39. package/dist/auth/cell_audit_actions.js +2 -2
  40. package/dist/auth/cell_audit_events.d.ts +2 -2
  41. package/dist/auth/cell_audit_events.d.ts.map +1 -1
  42. package/dist/auth/cell_audit_metadata.d.ts +93 -30
  43. package/dist/auth/cell_audit_metadata.d.ts.map +1 -1
  44. package/dist/auth/cell_audit_metadata.js +15 -14
  45. package/dist/auth/cell_authorize.d.ts +3 -3
  46. package/dist/auth/cell_authorize.js +3 -3
  47. package/dist/auth/cell_data_schema.d.ts +17 -9
  48. package/dist/auth/cell_data_schema.d.ts.map +1 -1
  49. package/dist/auth/cell_data_schema.js +9 -7
  50. package/dist/auth/cell_field_action_specs.d.ts +697 -130
  51. package/dist/auth/cell_field_action_specs.d.ts.map +1 -1
  52. package/dist/auth/cell_field_action_specs.js +25 -27
  53. package/dist/auth/cell_field_actions.d.ts.map +1 -1
  54. package/dist/auth/cell_field_actions.js +2 -0
  55. package/dist/auth/cell_field_audit_metadata.d.ts +49 -13
  56. package/dist/auth/cell_field_audit_metadata.d.ts.map +1 -1
  57. package/dist/auth/cell_field_audit_metadata.js +6 -6
  58. package/dist/auth/cell_grant_action_specs.d.ts +1044 -208
  59. package/dist/auth/cell_grant_action_specs.d.ts.map +1 -1
  60. package/dist/auth/cell_grant_action_specs.js +36 -33
  61. package/dist/auth/cell_grant_actions.js +2 -2
  62. package/dist/auth/cell_grant_audit_metadata.d.ts +188 -40
  63. package/dist/auth/cell_grant_audit_metadata.d.ts.map +1 -1
  64. package/dist/auth/cell_grant_audit_metadata.js +12 -11
  65. package/dist/auth/cell_item_action_specs.d.ts +949 -175
  66. package/dist/auth/cell_item_action_specs.d.ts.map +1 -1
  67. package/dist/auth/cell_item_action_specs.js +30 -34
  68. package/dist/auth/cell_item_actions.js +1 -1
  69. package/dist/auth/cell_item_audit_metadata.d.ts +68 -19
  70. package/dist/auth/cell_item_audit_metadata.d.ts.map +1 -1
  71. package/dist/auth/cell_item_audit_metadata.js +9 -9
  72. package/dist/auth/cell_relation_list_limit.d.ts +1 -2
  73. package/dist/auth/cell_relation_list_limit.d.ts.map +1 -1
  74. package/dist/auth/cell_relation_list_limit.js +3 -8
  75. package/dist/auth/role_grant_offer_action_specs.d.ts +4 -4
  76. package/dist/auth/signup_route_schema.d.ts +9 -4
  77. package/dist/auth/signup_route_schema.d.ts.map +1 -1
  78. package/dist/auth/signup_route_schema.js +5 -3
  79. package/dist/auth/signup_routes.d.ts.map +1 -1
  80. package/dist/auth/signup_routes.js +170 -170
  81. package/dist/db/cell_moderation.d.ts +6 -7
  82. package/dist/db/cell_moderation.d.ts.map +1 -1
  83. package/dist/db/cell_moderation.js +6 -7
  84. package/dist/http/CLAUDE.md +48 -3
  85. package/dist/http/common_routes.d.ts +1 -1
  86. package/dist/http/common_routes.d.ts.map +1 -1
  87. package/dist/http/common_routes.js +13 -11
  88. package/dist/http/db_routes.d.ts.map +1 -1
  89. package/dist/http/db_routes.js +25 -28
  90. package/dist/http/error_schemas.d.ts +5 -1
  91. package/dist/http/error_schemas.d.ts.map +1 -1
  92. package/dist/http/error_schemas.js +5 -1
  93. package/dist/http/route_result.d.ts +101 -0
  94. package/dist/http/route_result.d.ts.map +1 -0
  95. package/dist/http/route_result.js +56 -0
  96. package/dist/http/route_spec.d.ts +110 -6
  97. package/dist/http/route_spec.d.ts.map +1 -1
  98. package/dist/http/route_spec.js +82 -14
  99. package/dist/rate_limiter.d.ts +0 -9
  100. package/dist/rate_limiter.d.ts.map +1 -1
  101. package/dist/rate_limiter.js +0 -15
  102. package/dist/server/serve_fact_route.d.ts.map +1 -1
  103. package/dist/server/serve_fact_route.js +23 -21
  104. package/dist/server/surface_route.d.ts +1 -1
  105. package/dist/server/surface_route.d.ts.map +1 -1
  106. package/dist/server/surface_route.js +4 -2
  107. package/dist/testing/CLAUDE.md +5 -3
  108. package/dist/testing/adversarial_404.d.ts.map +1 -1
  109. package/dist/testing/adversarial_404.js +12 -7
  110. package/dist/testing/cross_backend/cell_crud.js +2 -2
  111. package/dist/testing/cross_backend/cell_relations.js +2 -2
  112. package/dist/testing/cross_backend/login_security.d.ts.map +1 -1
  113. package/dist/testing/cross_backend/login_security.js +3 -2
  114. package/dist/testing/cross_backend/rust_spine_consumer_surface.d.ts.map +1 -1
  115. package/dist/testing/cross_backend/rust_spine_consumer_surface.js +2 -6
  116. package/dist/testing/stubs.d.ts +14 -3
  117. package/dist/testing/stubs.d.ts.map +1 -1
  118. package/dist/testing/stubs.js +21 -6
  119. package/package.json +1 -1
@@ -4,9 +4,9 @@
4
4
  * Split from `bootstrap_routes.ts` so the route's declared shape (method,
5
5
  * path, auth, input/output/error schemas) is importable **without** the
6
6
  * hono-coupled handler (which sets a session cookie and reads the client
7
- * IP off the Hono context). `create_bootstrap_route_specs` spreads
8
- * `bootstrap_route_shape` and attaches the live handler; the test surface
9
- * builder (`create_test_app_surface_spec`) spreads it with a stub handler so
7
+ * IP off the Hono context). `create_bootstrap_route_specs` attaches the live
8
+ * handler to `bootstrap_route_shape` (`attach_route_handler`); the test surface
9
+ * builder (`create_test_app_surface_spec`) attaches a stub handler so
10
10
  * attack-surface generation reads the real shape without pulling the
11
11
  * in-process Hono app onto cross-process consumers. Single source of truth —
12
12
  * the shape can't drift between the live route and the surface.
@@ -17,6 +17,7 @@ import { meta } from '@fuzdev/fuz_schema/meta.ts';
17
17
  import { literal, max_length, min_length, object, pipe, string } from '@fuzdev/fuz_schema/schema.ts';
18
18
  import { Username, UsernameCanonical, Uuid } from '../primitive_schemas_fuz.js';
19
19
  import { Password } from './password.js';
20
+ import { define_route_shape } from '../http/route_spec.js';
20
21
  import { REST_ALREADY_BOOTSTRAPPED, REST_INVALID_TOKEN, REST_TOKEN_FILE_MISSING } from '../http/error_schemas.js';
21
22
  /**
22
23
  * The longest `token` `POST /bootstrap` takes, in UTF-16 code units: the token
@@ -52,11 +53,11 @@ export const BootstrapOutput = object({
52
53
  });
53
54
  /**
54
55
  * The `POST /bootstrap` route shape minus its handler — pure hono-free data.
55
- * `create_bootstrap_route_specs` spreads this and attaches the live handler;
56
- * surface generation spreads it with a stub handler (handlers are never run
56
+ * `create_bootstrap_route_specs` attaches the live handler to it
57
+ * (`attach_route_handler`); surface generation attaches a stub (handlers are never run
57
58
  * during surface assembly, only the shape is read).
58
59
  */
59
- export const bootstrap_route_shape = {
60
+ export const bootstrap_route_shape = define_route_shape({
60
61
  method: 'POST',
61
62
  path: '/bootstrap',
62
63
  auth: { account: 'none', actor: 'none' },
@@ -67,4 +68,4 @@ export const bootstrap_route_shape = {
67
68
  output: BootstrapOutput,
68
69
  rate_limit: 'ip',
69
70
  errors: [REST_INVALID_TOKEN, REST_ALREADY_BOOTSTRAPPED, REST_TOKEN_FILE_MISSING]
70
- };
71
+ });
@@ -1 +1 @@
1
- {"version":3,"file":"bootstrap_routes.d.ts","sourceRoot":"../src/lib/","sources":["../../src/lib/auth/bootstrap_routes.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,EAGN,KAAK,uBAAuB,EAC5B,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAmB,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAGxE,OAAO,EAGN,KAAK,WAAW,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAGlD;;G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MG;AACH,eAAO,MAAM,4BAA4B,GACxC,MAAM,gBAAgB,EACtB,SAAS,qBAAqB,KAC5B,KAAK,CAAC,SAAS,CA6HjB,CAAC"}
@@ -10,11 +10,12 @@ import { to_error_message } from '@fuzdev/fuz_util/error.ts';
10
10
  import { create_session_and_set_cookie } from './session_middleware.js';
11
11
  import { bootstrap_account, read_bootstrap_token } from './bootstrap_account.js';
12
12
  import { bootstrap_route_shape } from './bootstrap_route_schema.js';
13
- import { get_route_input } from '../http/route_spec.js';
13
+ import { attach_route_handler, get_route_input } from '../http/route_spec.js';
14
+ import { route_fail, route_ok } from '../http/route_result.js';
14
15
  import { get_client_ip } from '../http/client_ip.js';
15
16
  import { rate_limit_ip_key } from '../http/ip_canonical.js';
16
- import { rate_limit_exceeded_response, reserve_optional } from '../rate_limiter.js';
17
- import { ERROR_ALREADY_BOOTSTRAPPED, ERROR_TOKEN_FILE_MISSING } from '../http/error_schemas.js';
17
+ import { reserve_optional } from '../rate_limiter.js';
18
+ import { ERROR_ALREADY_BOOTSTRAPPED, ERROR_TOKEN_FILE_MISSING, REST_ALREADY_BOOTSTRAPPED, REST_INVALID_TOKEN, REST_RATE_LIMIT_EXCEEDED, REST_TOKEN_FILE_MISSING } from '../http/error_schemas.js';
18
19
  /**
19
20
  * Check bootstrap availability at startup.
20
21
  *
@@ -68,107 +69,108 @@ export const create_bootstrap_route_specs = (deps, options) => {
68
69
  const { session_options, bootstrap_status, on_bootstrap, bootstrap_ip_rate_limiter } = options;
69
70
  const { token_path } = bootstrap_status;
70
71
  return [
71
- {
72
- ...bootstrap_route_shape,
73
- handler: async (c, route) => {
74
- // Short-circuit if bootstrap already completed or surface-only mounted.
75
- // In 'surface_only' mode `bootstrap_status.token_path === null` and
76
- // `available === false`; in 'live' mode after success `available` flips
77
- // to `false`. Either way the wire shape is 403 ALREADY_BOOTSTRAPPED.
78
- if (!bootstrap_status.available || token_path === null) {
79
- return c.json({ error: ERROR_ALREADY_BOOTSTRAPPED }, 403);
80
- }
81
- // Per-IP rate limit (before any token/DB work). Reserved, not
82
- // checked: the attempt counts against the cap until it settles, so
83
- // a concurrent burst of token guesses can't all pass before any of
84
- // it records. The `finally` releases it unless a failure recorded it.
85
- // keyed by the address's rate-limit key (an IPv6 client's /64)
86
- const ip_key = bootstrap_ip_rate_limiter ? rate_limit_ip_key(get_client_ip(c)) : null;
87
- const ip_reserved = await reserve_optional(bootstrap_ip_rate_limiter, ip_key, {
88
- signal: c.req.raw.signal
89
- });
90
- if (!ip_reserved.ok) {
91
- return rate_limit_exceeded_response(c, ip_reserved.retry_after);
92
- }
93
- try {
94
- const input = get_route_input(c);
95
- // `transaction: false` makes `route.db` the pool. `bootstrap_account`
96
- // manages its own transaction internally.
97
- const result = await bootstrap_account({
98
- db: route.db,
99
- token_path,
100
- read_secure_file: deps.read_secure_file,
101
- delete_file: deps.delete_file,
102
- password: deps.password,
103
- log: deps.log
104
- }, input.token, input);
105
- if (!result.ok) {
106
- // An unreadable token file closes the window. Bootstrap cannot
107
- // succeed without one, and `check_bootstrap_status` already reads
108
- // an unreadable file as unavailable at startup — so leaving the
109
- // flag set means the two disagree, and every later request takes
110
- // this leg and writes another audit row. The limiter would bound
111
- // that channel; closing it ends it, and stops `/status`
112
- // advertising a window that can't be walked through. Twin of the
113
- // Rust spine's `bootstrap_handler`, which does the same.
114
- if (result.error === ERROR_TOKEN_FILE_MISSING)
115
- bootstrap_status.available = false;
116
- ip_reserved.reservation.record();
117
- await deps.audit.emit(route, {
118
- event_type: 'bootstrap',
119
- outcome: 'failure',
120
- ip: get_client_ip(c),
121
- metadata: { error: result.error }
122
- });
123
- return c.json({ error: result.error }, result.status);
124
- }
125
- // Successful bootstrap — update state immediately. Nothing is
126
- // reset: bootstrap has no account-grain bucket to forgive (it
127
- // predates any account), and the IP bucket is never refunded on
128
- // success (see `RateLimiter.reset`). So this is the one auth
129
- // surface where a success clears nothing at all. Bootstrap is
130
- // one-shot, so at most `max_attempts - 1` residual entries
131
- // survive — and they sit on this route's own limiter, not
132
- // login's, so a fumbled token can't spend the operator's login
133
- // budget on a deployment where their account is the only one.
134
- bootstrap_status.available = false;
135
- await create_session_and_set_cookie({
136
- keyring,
137
- deps: { db: route.db },
138
- c,
139
- account_id: result.account.id,
140
- session_options
141
- });
142
- if (on_bootstrap) {
143
- try {
144
- await on_bootstrap(result, c);
145
- }
146
- catch (err) {
147
- deps.log.error(`on_bootstrap callback failed: ${to_error_message(err)}`);
148
- }
149
- }
72
+ attach_route_handler(bootstrap_route_shape, async (c, route) => {
73
+ // Short-circuit if bootstrap already completed or surface-only mounted.
74
+ // In 'surface_only' mode `bootstrap_status.token_path === null` and
75
+ // `available === false`; in 'live' mode after success `available` flips
76
+ // to `false`. Either way the wire shape is 403 ALREADY_BOOTSTRAPPED.
77
+ if (!bootstrap_status.available || token_path === null) {
78
+ return route_fail(REST_ALREADY_BOOTSTRAPPED);
79
+ }
80
+ // Per-IP rate limit (before any token/DB work). Reserved, not
81
+ // checked: the attempt counts against the cap until it settles, so
82
+ // a concurrent burst of token guesses can't all pass before any of
83
+ // it records. The `finally` releases it unless a failure recorded it.
84
+ // keyed by the address's rate-limit key (an IPv6 client's /64)
85
+ const ip_key = bootstrap_ip_rate_limiter ? rate_limit_ip_key(get_client_ip(c)) : null;
86
+ const ip_reserved = await reserve_optional(bootstrap_ip_rate_limiter, ip_key, {
87
+ signal: c.req.raw.signal
88
+ });
89
+ if (!ip_reserved.ok) {
90
+ return route_fail(REST_RATE_LIMIT_EXCEEDED, { retry_after: ip_reserved.retry_after });
91
+ }
92
+ try {
93
+ const input = get_route_input(c);
94
+ // `transaction: false` makes `route.db` the pool. `bootstrap_account`
95
+ // manages its own transaction internally.
96
+ const result = await bootstrap_account({
97
+ db: route.db,
98
+ token_path,
99
+ read_secure_file: deps.read_secure_file,
100
+ delete_file: deps.delete_file,
101
+ password: deps.password,
102
+ log: deps.log
103
+ }, input.token, input);
104
+ if (!result.ok) {
105
+ // An unreadable token file closes the window. Bootstrap cannot
106
+ // succeed without one, and `check_bootstrap_status` already reads
107
+ // an unreadable file as unavailable at startup — so leaving the
108
+ // flag set means the two disagree, and every later request takes
109
+ // this leg and writes another audit row. The limiter would bound
110
+ // that channel; closing it ends it, and stops `/status`
111
+ // advertising a window that can't be walked through. Twin of the
112
+ // Rust spine's `bootstrap_handler`, which does the same.
113
+ if (result.error === ERROR_TOKEN_FILE_MISSING)
114
+ bootstrap_status.available = false;
115
+ ip_reserved.reservation.record();
150
116
  await deps.audit.emit(route, {
151
117
  event_type: 'bootstrap',
152
- actor_id: result.actor.id,
153
- account_id: result.account.id,
154
- ip: get_client_ip(c)
118
+ outcome: 'failure',
119
+ ip: get_client_ip(c),
120
+ metadata: { error: result.error }
155
121
  });
156
- // CRITICAL: If token file deletion failed, throw to force operator attention.
157
- // All success work (session, on_bootstrap, audit) has completed above.
158
- // The error response alerts the operator to delete the token file manually.
159
- if (!result.token_file_deleted) {
160
- throw new Error(`Bootstrap succeeded but token file was not deleted at ${token_path}. Delete it manually and log in.`);
122
+ return route_fail(result.error === ERROR_ALREADY_BOOTSTRAPPED
123
+ ? REST_ALREADY_BOOTSTRAPPED
124
+ : result.error === ERROR_TOKEN_FILE_MISSING
125
+ ? REST_TOKEN_FILE_MISSING
126
+ : REST_INVALID_TOKEN);
127
+ }
128
+ // Successful bootstrap — update state immediately. Nothing is
129
+ // reset: bootstrap has no account-grain bucket to forgive (it
130
+ // predates any account), and the IP bucket is never refunded on
131
+ // success (see `RateLimiter.reset`). So this is the one auth
132
+ // surface where a success clears nothing at all. Bootstrap is
133
+ // one-shot, so at most `max_attempts - 1` residual entries
134
+ // survive — and they sit on this route's own limiter, not
135
+ // login's, so a fumbled token can't spend the operator's login
136
+ // budget on a deployment where their account is the only one.
137
+ bootstrap_status.available = false;
138
+ await create_session_and_set_cookie({
139
+ keyring,
140
+ deps: { db: route.db },
141
+ c,
142
+ account_id: result.account.id,
143
+ session_options
144
+ });
145
+ if (on_bootstrap) {
146
+ try {
147
+ await on_bootstrap(result, c);
148
+ }
149
+ catch (err) {
150
+ deps.log.error(`on_bootstrap callback failed: ${to_error_message(err)}`);
161
151
  }
162
- return c.json({
163
- ok: true,
164
- account: { id: result.account.id, username: result.account.username },
165
- actor: { id: result.actor.id }
166
- });
167
152
  }
168
- finally {
169
- ip_reserved.reservation.release();
153
+ await deps.audit.emit(route, {
154
+ event_type: 'bootstrap',
155
+ actor_id: result.actor.id,
156
+ account_id: result.account.id,
157
+ ip: get_client_ip(c)
158
+ });
159
+ // CRITICAL: If token file deletion failed, throw to force operator attention.
160
+ // All success work (session, on_bootstrap, audit) has completed above.
161
+ // The error response alerts the operator to delete the token file manually.
162
+ if (!result.token_file_deleted) {
163
+ throw new Error(`Bootstrap succeeded but token file was not deleted at ${token_path}. Delete it manually and log in.`);
170
164
  }
165
+ return route_ok({
166
+ ok: true,
167
+ account: { id: result.account.id, username: result.account.username },
168
+ actor: { id: result.actor.id }
169
+ });
170
+ }
171
+ finally {
172
+ ip_reserved.reservation.release();
171
173
  }
172
- }
174
+ })
173
175
  ];
174
176
  };