@sssb/kit 0.0.0 → 0.3.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 (227) hide show
  1. package/LICENSE +21 -0
  2. package/dist/core/webhook-hmac.d.ts +69 -0
  3. package/dist/release.json +1 -0
  4. package/dist/testing/e2e.d.ts +15 -0
  5. package/dist/testing/e2e.js +195 -0
  6. package/package.json +212 -5
  7. package/src/adapters/README.md +6 -0
  8. package/src/adapters/dev-server.ts +21 -0
  9. package/src/adapters/fake/agreements.ts +81 -0
  10. package/src/adapters/fake/analytics.ts +93 -0
  11. package/src/adapters/fake/atomic-write.ts +161 -0
  12. package/src/adapters/fake/audit.ts +58 -0
  13. package/src/adapters/fake/auth.ts +313 -0
  14. package/src/adapters/fake/clock.ts +54 -0
  15. package/src/adapters/fake/data-dir.ts +27 -0
  16. package/src/adapters/fake/db.ts +88 -0
  17. package/src/adapters/fake/email.ts +46 -0
  18. package/src/adapters/fake/jobs.ts +248 -0
  19. package/src/adapters/fake/llm.ts +230 -0
  20. package/src/adapters/fake/pglite-dialect.ts +124 -0
  21. package/src/adapters/fake/service-auth.ts +145 -0
  22. package/src/adapters/fake/simulator-admin.ts +203 -0
  23. package/src/adapters/fake/simulator.ts +93 -0
  24. package/src/adapters/fake/sms.ts +43 -0
  25. package/src/adapters/fake/storage.ts +288 -0
  26. package/src/adapters/fake/webhooks.ts +98 -0
  27. package/src/adapters/index.ts +142 -0
  28. package/src/adapters/lazy-jobs.ts +25 -0
  29. package/src/adapters/proxy.ts +34 -0
  30. package/src/adapters/real/absent.ts +41 -0
  31. package/src/adapters/real/accept-invite-form.tsx +77 -0
  32. package/src/adapters/real/analytics.ts +28 -0
  33. package/src/adapters/real/auth.ts +216 -0
  34. package/src/adapters/real/clerk-proxy.ts +111 -0
  35. package/src/adapters/real/contract-data-api.ts +104 -0
  36. package/src/adapters/real/contract-postgres.ts +167 -0
  37. package/src/adapters/real/database-transport.ts +34 -0
  38. package/src/adapters/real/db-data-api.ts +61 -0
  39. package/src/adapters/real/db-port.ts +26 -0
  40. package/src/adapters/real/db.ts +69 -0
  41. package/src/adapters/real/email.ts +22 -0
  42. package/src/adapters/real/index.ts +139 -0
  43. package/src/adapters/real/jobs-in-process.ts +118 -0
  44. package/src/adapters/real/jobs.ts +33 -0
  45. package/src/adapters/real/llm.ts +108 -0
  46. package/src/adapters/real/migrate-handler.ts +66 -0
  47. package/src/adapters/real/optional-ports.ts +78 -0
  48. package/src/adapters/real/providers.tsx +12 -0
  49. package/src/adapters/real/rds-data-codec.ts +508 -0
  50. package/src/adapters/real/rds-data-dialect.ts +470 -0
  51. package/src/adapters/real/rds-data-emulator.ts +351 -0
  52. package/src/adapters/real/s3-storage.ts +233 -0
  53. package/src/adapters/real/sign-in-form.tsx +59 -0
  54. package/src/adapters/real/storage.ts +10 -0
  55. package/src/adapters/run-mode.ts +11 -0
  56. package/src/adapters/sentry/baseline.ts +90 -0
  57. package/src/adapters/sentry/client.ts +17 -0
  58. package/src/adapters/sentry/init.ts +17 -0
  59. package/src/authz/app-exemptions.ts +226 -0
  60. package/src/authz/authorize.ts +214 -0
  61. package/src/authz/route-policy.ts +153 -0
  62. package/src/authz/route-scan.ts +1516 -0
  63. package/src/components/agreements/acceptances-section.tsx +55 -0
  64. package/src/components/agreements/agreement-advisory-banner.tsx +33 -0
  65. package/src/components/agreements/agreement-body.tsx +34 -0
  66. package/src/components/agreements/agreement-gate.tsx +45 -0
  67. package/src/components/app-header.tsx +45 -0
  68. package/src/components/auth/accept-invite-screen.tsx +126 -0
  69. package/src/components/auth/auth-icons.tsx +68 -0
  70. package/src/components/auth/org-switcher.tsx +76 -0
  71. package/src/components/auth/people-picker.tsx +50 -0
  72. package/src/components/auth/preview-row.tsx +64 -0
  73. package/src/components/auth/sign-in-screen.tsx +24 -0
  74. package/src/components/auth/user-menu.tsx +64 -0
  75. package/src/components/demo-badge.tsx +20 -0
  76. package/src/components/demo-banner.tsx +14 -0
  77. package/src/components/gates/gate-interstitial.tsx +23 -0
  78. package/src/components/job-timeline.tsx +64 -0
  79. package/src/components/notifications/notification-bell.tsx +120 -0
  80. package/src/components/notifications/notification-prefs-section.tsx +112 -0
  81. package/src/components/org-screen.tsx +197 -0
  82. package/src/components/profile-screen.tsx +100 -0
  83. package/src/components/simulator/actor-runtime.ts +180 -0
  84. package/src/components/simulator/actor-shell.tsx +275 -0
  85. package/src/components/simulator/actors-app.tsx +119 -0
  86. package/src/components/simulator/errors-app.tsx +84 -0
  87. package/src/components/simulator/events-app.tsx +210 -0
  88. package/src/components/simulator/format-when.ts +20 -0
  89. package/src/components/simulator/hooks-app.tsx +319 -0
  90. package/src/components/simulator/inbound-app.tsx +210 -0
  91. package/src/components/simulator/jobs-app.tsx +471 -0
  92. package/src/components/simulator/mail-app.tsx +290 -0
  93. package/src/components/simulator/messages-app.tsx +64 -0
  94. package/src/components/simulator/people-app.tsx +140 -0
  95. package/src/components/simulator/people-dimensions.ts +16 -0
  96. package/src/components/simulator/simulator-host.tsx +335 -0
  97. package/src/components/simulator/simulator-panel.tsx +704 -0
  98. package/src/components/simulator/snapshots-app.tsx +398 -0
  99. package/src/components/simulator/tab-mount.ts +25 -0
  100. package/src/components/simulator/tours-app.tsx +105 -0
  101. package/src/components/webhook-endpoints-card.tsx +172 -0
  102. package/src/core/abilities.ts +464 -0
  103. package/src/core/agreements.ts +139 -0
  104. package/src/core/gates.ts +157 -0
  105. package/src/core/greeting.ts +11 -0
  106. package/src/core/inbound-email.ts +129 -0
  107. package/src/core/jobs.ts +51 -0
  108. package/src/core/keyset.ts +227 -0
  109. package/src/core/llm-models.ts +10 -0
  110. package/src/core/locale.ts +3 -0
  111. package/src/core/notifications.ts +182 -0
  112. package/src/core/presets.ts +456 -0
  113. package/src/core/roles.ts +47 -0
  114. package/src/core/same-origin-path.ts +20 -0
  115. package/src/core/schedules.ts +147 -0
  116. package/src/core/standard-schema.ts +56 -0
  117. package/src/core/state-machine.ts +43 -0
  118. package/src/core/webhook-events.ts +86 -0
  119. package/src/core/webhook-hmac.ts +248 -0
  120. package/src/core/webhook-signing.ts +27 -0
  121. package/src/cutover/rows.json +101 -0
  122. package/src/db/agreements.ts +176 -0
  123. package/src/db/app-roles.ts +197 -0
  124. package/src/db/audit.ts +356 -0
  125. package/src/db/inbound-email.ts +145 -0
  126. package/src/db/jobs.ts +300 -0
  127. package/src/db/jsonb.ts +18 -0
  128. package/src/db/keyset.ts +179 -0
  129. package/src/db/migrate.ts +73 -0
  130. package/src/db/migrations/0001_tenants.ts +25 -0
  131. package/src/db/migrations/0003_organizations.ts +27 -0
  132. package/src/db/migrations/0004_jobs.ts +64 -0
  133. package/src/db/migrations/0005_service_keys.ts +33 -0
  134. package/src/db/migrations/0008_audit_events.ts +51 -0
  135. package/src/db/migrations/0009_job_schedules.ts +52 -0
  136. package/src/db/migrations/0010_webhooks.ts +100 -0
  137. package/src/db/migrations/0011_notifications.ts +98 -0
  138. package/src/db/migrations/0012_inbound_email.ts +74 -0
  139. package/src/db/migrations/0013_agreements.ts +98 -0
  140. package/src/db/migrations/0014_audit_credential.ts +29 -0
  141. package/src/db/migrations/0015_app_user_grant.ts +67 -0
  142. package/src/db/migrations/0016_audit_detail.ts +26 -0
  143. package/src/db/migrations/index.ts +49 -0
  144. package/src/db/notifications.ts +185 -0
  145. package/src/db/org-lookup.ts +104 -0
  146. package/src/db/owner-pass.ts +252 -0
  147. package/src/db/rls-coverage.ts +259 -0
  148. package/src/db/rls-proof-runner.ts +94 -0
  149. package/src/db/rls-proofs.ts +1176 -0
  150. package/src/db/schedules.ts +187 -0
  151. package/src/db/schema.ts +235 -0
  152. package/src/db/seed.ts +176 -0
  153. package/src/db/service-keys.ts +50 -0
  154. package/src/db/tenant-lookup.ts +13 -0
  155. package/src/db/webhooks.ts +483 -0
  156. package/src/db/with-tenant.ts +26 -0
  157. package/src/demo-static/actor-drivers.ts +161 -0
  158. package/src/demo-static/assistant-replay.ts +61 -0
  159. package/src/demo-static/contracts.ts +282 -0
  160. package/src/demo-static/email-html.ts +58 -0
  161. package/src/demo-static/hash-route.ts +24 -0
  162. package/src/demo-static/rows.ts +105 -0
  163. package/src/demo-static/shell.tsx +440 -0
  164. package/src/demo-static/tour/contracts.ts +97 -0
  165. package/src/demo-static/tour/driver.ts +320 -0
  166. package/src/demo-static/tour/tour-overlay.tsx +226 -0
  167. package/src/demo-static/tour/use-tours.tsx +394 -0
  168. package/src/demo-static/world.ts +1480 -0
  169. package/src/email/send.ts +12 -0
  170. package/src/email/templates/digest-email.tsx +61 -0
  171. package/src/email/templates/invite-email.tsx +57 -0
  172. package/src/email/templates/notification-email.tsx +39 -0
  173. package/src/guide.md +132 -0
  174. package/src/i18n/messages/en.json +373 -0
  175. package/src/i18n/messages/es.json +373 -0
  176. package/src/i18n/messages.ts +34 -0
  177. package/src/i18n/namespaces.ts +32 -0
  178. package/src/i18n/request.ts +34 -0
  179. package/src/i18n/routing.ts +7 -0
  180. package/src/inbound-email/handlers.ts +67 -0
  181. package/src/inbound-email/intake.ts +180 -0
  182. package/src/jobs/digest-email.ts +112 -0
  183. package/src/jobs/run-in-process.ts +82 -0
  184. package/src/observability/scrub.ts +44 -0
  185. package/src/ports/README.md +8 -0
  186. package/src/ports/analytics.ts +8 -0
  187. package/src/ports/auth.ts +87 -0
  188. package/src/ports/db.ts +27 -0
  189. package/src/ports/email.ts +14 -0
  190. package/src/ports/errors.ts +46 -0
  191. package/src/ports/jobs.ts +69 -0
  192. package/src/ports/llm.ts +85 -0
  193. package/src/ports/storage.ts +132 -0
  194. package/src/seed/contracts.ts +129 -0
  195. package/src/server-lib/assistant.ts +45 -0
  196. package/src/server-lib/defer.ts +57 -0
  197. package/src/server-lib/demo-presets.ts +85 -0
  198. package/src/server-lib/invite.ts +92 -0
  199. package/src/server-lib/notify-deps.ts +15 -0
  200. package/src/server-lib/notify.ts +252 -0
  201. package/src/server-lib/preset-operations.ts +121 -0
  202. package/src/server-lib/scheduled-tick.ts +48 -0
  203. package/src/server-lib/simulator-api.ts +311 -0
  204. package/src/server-lib/sms.ts +43 -0
  205. package/src/server-lib/webhook-dispatch.ts +75 -0
  206. package/src/service-auth/credential.ts +20 -0
  207. package/src/service-auth/mailgun.ts +70 -0
  208. package/src/service-auth/verify.ts +80 -0
  209. package/src/service-auth/webhook.ts +69 -0
  210. package/src/testing/checks.json +103 -0
  211. package/src/testing/conformance/authz.ts +46 -0
  212. package/src/testing/conformance/db.ts +258 -0
  213. package/src/testing/conformance/demo-presets.ts +295 -0
  214. package/src/testing/conformance/i18n.ts +80 -0
  215. package/src/testing/conformance/register.ts +200 -0
  216. package/src/testing/conformance/seam-map.ts +205 -0
  217. package/src/testing/conformance/suite.ts +59 -0
  218. package/src/testing/conformance/types.ts +18 -0
  219. package/src/testing/contract.ts +200 -0
  220. package/src/testing/e2e.ts +15 -0
  221. package/src/testing/index.ts +6 -0
  222. package/src/testing/seam-specifiers.json +22 -0
  223. package/src/testing/tmp-dir-marker.d.mts +4 -0
  224. package/src/testing/tmp-dir-marker.mjs +5 -0
  225. package/src/testing/tmp-dir.ts +182 -0
  226. package/src/theme.ts +28 -0
  227. package/README.md +0 -3
@@ -0,0 +1,153 @@
1
+ import type { Exemption, Gate, ScanPolicy } from './route-scan'
2
+
3
+ /**
4
+ * THE FRAMEWORK'S AUTHORIZATION POLICY: the exemptions and gates the authorization scan applies to every route
5
+ * tree it walks. Every mutating route handler (POST/PUT/PATCH/DELETE) must reach `authorize(...)` on its own path
6
+ * (./route-scan.ts judges that), or its route must sit in the justified exemption map below AND the handler must
7
+ * pass that exemption's gate. GET-only files never have to authorize.
8
+ *
9
+ * It lives in source, not in a test, because two scans apply it: the kit's own (`authorized-mutations.test.ts`, over
10
+ * the framework's reference tree `packages/kit/test-fixture/api`, which is also what keeps every entry here from
11
+ * going stale) and the conformance suite's `authz-mutations` check (`@sssb/kit/testing`), over each app's own
12
+ * `src/app`. An app cannot edit this map; it names exemptions of its own in `<app>/authz-exemptions.mjs`
13
+ * (./app-exemptions.ts), which may not collide with an entry here.
14
+ *
15
+ * Exemption keys are paths relative to a root's `exemptDir` (an app's `src/app/api`, or the fixture's `api/`), each
16
+ * with the reason it needs no per-actor authorize(...) call. Blanket by prefix (whole dirs) or exact by file. Keep
17
+ * this list honest: a new mutating route that isn't genuinely one of these must call authorize(...), not be added
18
+ * here. Every entry is backed by a file in the fixture tree, which is what keeps the staleness checks meaningful.
19
+ *
20
+ * EVERY exemption names the gate its reason rests on as `mustMatch`, and the scan checks it on EVERY exempted
21
+ * mutating HANDLER, per verb, so the claim is ENFORCED, not prose: a guarded POST does not carry an open DELETE
22
+ * beside it. `mustMatch` is REQUIRED (`isGate` below, which the kit's test applies to every entry) because a bare
23
+ * exemption is matched by RELATIVE PATH IN EVERY ROOT, including an adopter's app. Without a gate,
24
+ * `profile/route.ts` would exempt any file at that path from authorize(...): an adopter's PUT that updated any user
25
+ * by id would ship un-authorized and the gate would stay green. With one, that handler has to reach
26
+ * `auth.updateProfile(` — a port call that takes no user id and so cannot express "update someone else" — to inherit
27
+ * the exemption.
28
+ *
29
+ * A gate is one of three kinds (./route-scan.ts, `Gate`), and the kind is the entry's statement of what "guarded"
30
+ * means for its reason:
31
+ *
32
+ * - `wrapped-by`: the guard WRAPS the handler, so the handler must BE the call (`export const POST =
33
+ * withWebhookSecret(…)`). Reaching the guard somewhere inside is not enough.
34
+ * - `calls`: the reason rests on a port call the handler makes, so the handler must reach that call.
35
+ * - `opens-with-simulated-404`: the handler's FIRST statement is `if (!isSimulated) return …`.
36
+ *
37
+ * What a gate does NOT promise: it proves the exempted route is the KIND of route the reason describes, not that
38
+ * the reason's full claim holds. `llm.complete(` proves a handler is the model-calling route; it cannot prove the
39
+ * route persists nothing. The gates are strongest where the reason is mechanical (a simulated-mode 404, a signature
40
+ * wrapper, a self-only port call) and weakest where it is about intent. See ADR-0012 for the trade-off in full.
41
+ */
42
+ const SIMULATED_404: Gate = { kind: 'opens-with-simulated-404' }
43
+
44
+ // No `service/` entry, deliberately. A service caller used to be exempt here as "org-scoped by
45
+ // construction"; it is now a MACHINE actor (@sssb/kit/core/abilities `MachineActor`) and its mutations call
46
+ // authorize(...) like any other route, so the per-action check replaced the blanket one (ADR-0012
47
+ // addendum, 2026-11-16). The fixture's service/jobs/route.ts is that machine route. What the old entry
48
+ // ALSO guaranteed (that a service route verifies its caller) is kept, below.
49
+ export const EXEMPT_PREFIXES: Readonly<Record<string, Exemption>> = {
50
+ 'webhooks/': {
51
+ reason:
52
+ 'authenticated by a machine caller, not a user session — a shared webhook secret ' +
53
+ '(withWebhookSecret) or the Mailgun inbound signature (withMailgunSignature) — so no ' +
54
+ 'per-actor ability applies',
55
+ mustMatch: { kind: 'wrapped-by', callees: ['withWebhookSecret', 'withMailgunSignature'] },
56
+ },
57
+ 'simulator/': {
58
+ reason: 'simulated-world surface, simulated-mode-gated (404 in real) — never a product mutation path',
59
+ mustMatch: SIMULATED_404,
60
+ },
61
+ }
62
+
63
+ export const EXEMPT_FILES: Readonly<Record<string, Exemption>> = {
64
+ 'profile/route.ts': {
65
+ reason: 'self-service: the subject is the caller (User.update is self-only), no cross-actor authorization',
66
+ // The port call that MAKES it self-only: updateProfile takes no user id, so a route that
67
+ // updates another user by id cannot be written through it and cannot inherit this exemption.
68
+ mustMatch: { kind: 'calls', callees: ['auth.updateProfile'] },
69
+ },
70
+ 'assistant/route.ts': {
71
+ reason:
72
+ 'stateless LLM call — creates no persisted, org-scoped resource to authorize. Any read tool it ' +
73
+ "exposes stays inside the caller's OWN active-org scope (filtered inside withTenant by the " +
74
+ 'handler-resolved orgId, the same scope as that app’s own list route) — no per-actor product mutation',
75
+ mustMatch: { kind: 'calls', callees: ['llm.complete', 'llm.stream', 'llm.runToolLoop'] },
76
+ },
77
+ 'analytics/page-view/route.ts': {
78
+ reason: 'telemetry beacon — records a page view, mutates no product resource',
79
+ mustMatch: { kind: 'calls', callees: ['analytics.capture'] },
80
+ },
81
+ 'auth/org/route.ts': {
82
+ reason: 'setActiveOrg enforces membership at the auth port (ForbiddenError for non-members)',
83
+ mustMatch: { kind: 'calls', callees: ['auth.setActiveOrg'] },
84
+ },
85
+ 'auth/signout/route.ts': {
86
+ reason: 'session lifecycle (sign-out) — no product resource, runs post-authz',
87
+ mustMatch: { kind: 'calls', callees: ['auth.signOut'] },
88
+ },
89
+ 'auth/dev-signin/route.ts': {
90
+ reason: 'session lifecycle (dev sign-in) — pre-authz by definition, simulated-mode only',
91
+ mustMatch: SIMULATED_404,
92
+ },
93
+ 'auth/accept-invite/route.ts': {
94
+ reason: 'invite acceptance is authorized by possession of the invite token, before any membership exists',
95
+ mustMatch: { kind: 'calls', callees: ['acceptInvite'] },
96
+ },
97
+ 'storage-upload/route.ts': {
98
+ reason:
99
+ 'simulated-mode-only (404 in real) multipart upload endpoint — the local twin of an S3 presigned POST, ' +
100
+ 'authenticated by the server-signed HMAC upload-target fields (like the webhook secret), NOT a user ' +
101
+ 'session; the upload TARGET was already authorized by the app route that minted it',
102
+ mustMatch: SIMULATED_404,
103
+ },
104
+ }
105
+
106
+ /**
107
+ * Gates a mutating handler under a prefix must pass IN ADDITION to reaching authorize(...). Authorizing an
108
+ * actor means something only if the actor came from a verified credential: `authorize(` alone is satisfied
109
+ * just as well by a `{ kind: 'machine', credential: { kind: 'service-key' } … }` literal built from the
110
+ * request. So a service handler must also BE the wrapper call that verifies its caller and BUILDS that
111
+ * actor. Matched by relative path in every root, like the exemptions, and backed by a fixture route.
112
+ */
113
+ export const REQUIRED_ALONGSIDE: Readonly<Record<string, Exemption>> = {
114
+ 'service/': {
115
+ reason:
116
+ 'a service route verifies its caller with withServiceCaller, which builds the machine actor ' +
117
+ '(ctx.actor) the route should authorize. The gate proves the wrapper is the handler, not that the ' +
118
+ 'actor authorized is the one it built (ADR-0012: a gate proves the KIND of route)',
119
+ mustMatch: { kind: 'wrapped-by', callees: ['withServiceCaller'] },
120
+ },
121
+ }
122
+
123
+ /**
124
+ * What the failure says to a route whose caller is not a session, because that is where an author is
125
+ * most likely to look for an exemption, and since 2026-11-16 there is none to find: `service/` routes
126
+ * used to be exempt, and an adopter's old one fails here on taking the update.
127
+ */
128
+ const NON_SESSION_HINT =
129
+ 'A caller that is not a session authorizes its actor with the three-argument form, ' +
130
+ 'authorize(actor, action, subject): a service key via abilityActorFromService (withServiceCaller passes it as ' +
131
+ 'ctx.actor), a machine the app verifies via machineActor, or a person holding a non-session credential (a ' +
132
+ 'personal API token) via abilityActorFromCredential. The `service/` exemption was retired (ADR-0012 addendum, 2026-11-16).'
133
+
134
+ /** The framework's policy, as the scan takes it. An app's own exemptions are added by `policyWithApp` (./app-exemptions.ts). */
135
+ export const FRAMEWORK_POLICY: ScanPolicy = {
136
+ exemptPrefixes: EXEMPT_PREFIXES,
137
+ exemptFiles: EXEMPT_FILES,
138
+ requiredAlongside: REQUIRED_ALONGSIDE,
139
+ hint: NON_SESSION_HINT,
140
+ }
141
+
142
+ /** A runtime check of the `Gate` shape: an exemption map is data, and a type is not present when a scan runs. */
143
+ export function isGate(gate: unknown): gate is Gate {
144
+ if (typeof gate !== 'object' || gate === null || !('kind' in gate)) return false
145
+ if (gate.kind === 'opens-with-simulated-404') return true
146
+ if (gate.kind !== 'wrapped-by' && gate.kind !== 'calls') return false
147
+ return (
148
+ 'callees' in gate &&
149
+ Array.isArray(gate.callees) &&
150
+ gate.callees.length > 0 &&
151
+ gate.callees.every((callee) => typeof callee === 'string' && callee.length > 0)
152
+ )
153
+ }