shapewall 0.0.0-stage → 0.2.2

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 (304) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/Dockerfile.agent +45 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +12 -0
  5. package/README.md +651 -2
  6. package/THIRD_PARTY.md +165 -0
  7. package/bin/shapewall.js +29 -0
  8. package/dist/agent/package-lock.json +8384 -0
  9. package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
  10. package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
  11. package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
  12. package/dist/client/assets/History-CEv6b-av.js +1 -0
  13. package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
  14. package/dist/client/assets/Management-DCfhcEUY.js +1 -0
  15. package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
  16. package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
  17. package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
  18. package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
  19. package/dist/client/assets/index-ByQRKVit.css +2 -0
  20. package/dist/client/assets/index-DweQ8Qtu.js +29 -0
  21. package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
  22. package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
  23. package/dist/client/assets/square-A8EnIYLR.js +1 -0
  24. package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
  25. package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
  26. package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
  27. package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
  28. package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
  29. package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
  30. package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
  31. package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
  32. package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
  33. package/dist/client/assets/ui-CLbws0rx.js +41 -0
  34. package/dist/client/build-info.json +1 -0
  35. package/dist/client/favicon.svg +7 -0
  36. package/dist/client/icons/apple-touch-icon.png +0 -0
  37. package/dist/client/icons/icon-192.png +0 -0
  38. package/dist/client/icons/icon-512.png +0 -0
  39. package/dist/client/icons/maskable-512.png +0 -0
  40. package/dist/client/index.html +25 -0
  41. package/dist/client/manifest.webmanifest +15 -0
  42. package/dist/client/sw.js +53 -0
  43. package/dist/client/theme-boot.js +27 -0
  44. package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
  45. package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
  46. package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
  47. package/dist/console/assets/console-C7uJLLOn.js +51 -0
  48. package/dist/console/assets/console-R8IBY3Am.css +2 -0
  49. package/dist/console/build-info.json +1 -0
  50. package/dist/console/console.html +21 -0
  51. package/dist/console/favicon.svg +7 -0
  52. package/dist/console/icons/apple-touch-icon.png +0 -0
  53. package/dist/console/icons/icon-192.png +0 -0
  54. package/dist/console/icons/icon-512.png +0 -0
  55. package/dist/console/icons/maskable-512.png +0 -0
  56. package/dist/console/manifest.webmanifest +15 -0
  57. package/dist/console/theme-boot.js +27 -0
  58. package/docs/agent-hardening.md +342 -0
  59. package/docs/agent-recipes.md +187 -0
  60. package/docs/agent-workflow.md +320 -0
  61. package/docs/agents.md +894 -0
  62. package/docs/architecture.md +242 -0
  63. package/docs/buzz.md +140 -0
  64. package/docs/capture-warnings.md +57 -0
  65. package/docs/config.md +489 -0
  66. package/docs/deployment.md +289 -0
  67. package/docs/design-system.md +162 -0
  68. package/docs/github.md +107 -0
  69. package/docs/hosted.md +764 -0
  70. package/docs/images/board-overview.png +0 -0
  71. package/docs/images/journeys.png +0 -0
  72. package/docs/images/live-comment.png +0 -0
  73. package/docs/images/phone-comment.png +0 -0
  74. package/docs/images/phone-overview.png +0 -0
  75. package/docs/images/variant.png +0 -0
  76. package/docs/interaction-audits.md +40 -0
  77. package/docs/journeys.md +137 -0
  78. package/docs/local-accounts.md +80 -0
  79. package/docs/notifications.md +237 -0
  80. package/docs/page-checks.md +69 -0
  81. package/docs/performance.md +174 -0
  82. package/docs/starter-agent.md +325 -0
  83. package/docs/updates.md +87 -0
  84. package/examples/audits/interaction.audit.json +17 -0
  85. package/package.json +132 -4
  86. package/src/accounts/avatar.js +121 -0
  87. package/src/accounts/local.js +464 -0
  88. package/src/agent/acp/command.js +204 -0
  89. package/src/agent/acp/detect.js +75 -0
  90. package/src/agent/acp/sandbox.js +160 -0
  91. package/src/agent/agent-health.js +75 -0
  92. package/src/agent/backends/acp.js +1297 -0
  93. package/src/agent/backends/index.js +61 -0
  94. package/src/agent/backends/openai.js +308 -0
  95. package/src/agent/composer.js +221 -0
  96. package/src/agent/config.js +338 -0
  97. package/src/agent/context.js +191 -0
  98. package/src/agent/control.js +122 -0
  99. package/src/agent/errors.js +17 -0
  100. package/src/agent/feedback-loop.js +137 -0
  101. package/src/agent/manage.js +94 -0
  102. package/src/agent/next-steps.js +51 -0
  103. package/src/agent/owner-mcp.js +38 -0
  104. package/src/agent/plain-text.js +52 -0
  105. package/src/agent/ratings-core.js +213 -0
  106. package/src/agent/ratings.js +100 -0
  107. package/src/agent/reply-text.js +67 -0
  108. package/src/agent/routes.js +1065 -0
  109. package/src/agent/run.js +326 -0
  110. package/src/agent/settings-routes.js +704 -0
  111. package/src/agent/showable.js +23 -0
  112. package/src/agent/starter/approvals.js +181 -0
  113. package/src/agent/starter/build-image.js +90 -0
  114. package/src/agent/starter/cli.js +163 -0
  115. package/src/agent/starter/disk.js +53 -0
  116. package/src/agent/starter/doctor.js +180 -0
  117. package/src/agent/starter/egress-proxy.js +351 -0
  118. package/src/agent/starter/gate.js +90 -0
  119. package/src/agent/starter/loop.js +114 -0
  120. package/src/agent/starter/model-relay.js +289 -0
  121. package/src/agent/starter/pause.js +14 -0
  122. package/src/agent/starter/probe.js +272 -0
  123. package/src/agent/starter/prompt.js +41 -0
  124. package/src/agent/starter/push-guard.js +146 -0
  125. package/src/agent/starter/rpc.js +164 -0
  126. package/src/agent/starter/runtime.js +287 -0
  127. package/src/agent/starter/sandbox-main.js +121 -0
  128. package/src/agent/starter/settings.js +290 -0
  129. package/src/agent/starter/source.js +69 -0
  130. package/src/agent/starter/supervisor.js +755 -0
  131. package/src/agent/starter/sweep.js +46 -0
  132. package/src/agent/starter/tools.js +524 -0
  133. package/src/agent/starter/watch.js +84 -0
  134. package/src/agent/starter/worktree.js +409 -0
  135. package/src/agent/text-diff.js +210 -0
  136. package/src/agent/thread-history.js +223 -0
  137. package/src/agent/threads.js +399 -0
  138. package/src/agent/tools.js +200 -0
  139. package/src/agent/turn-changes.js +424 -0
  140. package/src/agent/undo-state.js +26 -0
  141. package/src/agent/user-settings.js +308 -0
  142. package/src/auth/record.js +375 -0
  143. package/src/broken-links.js +164 -0
  144. package/src/buzz/client.js +75 -0
  145. package/src/buzz/config.js +151 -0
  146. package/src/buzz/environment.js +13 -0
  147. package/src/buzz/gateway.js +192 -0
  148. package/src/buzz/routes.js +61 -0
  149. package/src/buzz/signer-child.js +205 -0
  150. package/src/buzz/store.js +89 -0
  151. package/src/buzz/transport.js +60 -0
  152. package/src/buzz/verify.js +140 -0
  153. package/src/capture/actions.js +159 -0
  154. package/src/capture/capture.js +776 -0
  155. package/src/capture/dom-names.js +189 -0
  156. package/src/capture/element-map.js +509 -0
  157. package/src/capture/live.js +827 -0
  158. package/src/capture/media-scheme.js +317 -0
  159. package/src/capture/network-summary.js +86 -0
  160. package/src/capture/pacing.js +34 -0
  161. package/src/capture/requests.js +167 -0
  162. package/src/capture/sanitize.js +752 -0
  163. package/src/checks/accessibility.js +134 -0
  164. package/src/checks/budgets.js +58 -0
  165. package/src/checks/cli.js +55 -0
  166. package/src/checks/comparison.js +113 -0
  167. package/src/checks/environments.js +32 -0
  168. package/src/checks/flow-config.js +82 -0
  169. package/src/checks/flow-origin.js +53 -0
  170. package/src/checks/flows.js +398 -0
  171. package/src/checks/lighthouse-auth.js +145 -0
  172. package/src/checks/lighthouse-findings.js +130 -0
  173. package/src/checks/lighthouse.js +395 -0
  174. package/src/checks/robots.js +85 -0
  175. package/src/checks/routes.js +52 -0
  176. package/src/checks/run-control.js +259 -0
  177. package/src/checks/runner.js +724 -0
  178. package/src/checks/schema.js +133 -0
  179. package/src/checks/seo-analysis.js +443 -0
  180. package/src/checks/seo-probe.js +190 -0
  181. package/src/checks/seo-snapshot.js +99 -0
  182. package/src/checks/store.js +214 -0
  183. package/src/checks/transport.js +47 -0
  184. package/src/cli/args.js +319 -0
  185. package/src/cli/capture.js +79 -0
  186. package/src/cli/commands.js +92 -0
  187. package/src/cli/comments.js +84 -0
  188. package/src/cli/help.js +243 -0
  189. package/src/cli/init.js +214 -0
  190. package/src/cli/misc.js +64 -0
  191. package/src/cli/notify.js +87 -0
  192. package/src/cli/open.js +22 -0
  193. package/src/cli/serve.js +274 -0
  194. package/src/cli/variant.js +82 -0
  195. package/src/cli/watch.js +70 -0
  196. package/src/cli.js +130 -0
  197. package/src/comment-fields.js +76 -0
  198. package/src/comments-cli.js +208 -0
  199. package/src/config-reload.js +100 -0
  200. package/src/config.js +1061 -0
  201. package/src/devices-cli.js +21 -0
  202. package/src/diff.js +95 -0
  203. package/src/discover.js +284 -0
  204. package/src/doctor.js +295 -0
  205. package/src/event-log.js +268 -0
  206. package/src/events-cli.js +102 -0
  207. package/src/events.js +113 -0
  208. package/src/export.js +119 -0
  209. package/src/feedback.js +246 -0
  210. package/src/github.js +551 -0
  211. package/src/hosted/LICENSE.md +105 -0
  212. package/src/hosted/access-cli.js +78 -0
  213. package/src/hosted/admin.js +682 -0
  214. package/src/hosted/agent-approvals.js +163 -0
  215. package/src/hosted/agent-policy.js +189 -0
  216. package/src/hosted/app.js +510 -0
  217. package/src/hosted/auth-routes.js +540 -0
  218. package/src/hosted/auth.js +536 -0
  219. package/src/hosted/backup.js +483 -0
  220. package/src/hosted/board-context.js +35 -0
  221. package/src/hosted/board-routes.js +486 -0
  222. package/src/hosted/board-view.js +94 -0
  223. package/src/hosted/clean-worker.js +24 -0
  224. package/src/hosted/cleaner.js +89 -0
  225. package/src/hosted/comments.js +355 -0
  226. package/src/hosted/console-routes.js +401 -0
  227. package/src/hosted/cookies.js +18 -0
  228. package/src/hosted/crypto.js +80 -0
  229. package/src/hosted/db.js +496 -0
  230. package/src/hosted/device-routes.js +175 -0
  231. package/src/hosted/device.js +233 -0
  232. package/src/hosted/events.js +63 -0
  233. package/src/hosted/guards.js +172 -0
  234. package/src/hosted/guest-view.js +65 -0
  235. package/src/hosted/http.js +39 -0
  236. package/src/hosted/import.js +300 -0
  237. package/src/hosted/mail.js +73 -0
  238. package/src/hosted/mcp-http.js +71 -0
  239. package/src/hosted/membership.js +114 -0
  240. package/src/hosted/operator.js +478 -0
  241. package/src/hosted/ops-cli.js +345 -0
  242. package/src/hosted/pages.js +146 -0
  243. package/src/hosted/permissions.js +113 -0
  244. package/src/hosted/presence.js +372 -0
  245. package/src/hosted/projects.js +124 -0
  246. package/src/hosted/public.js +109 -0
  247. package/src/hosted/push.js +622 -0
  248. package/src/hosted/quotas.js +115 -0
  249. package/src/hosted/ratelimit.js +96 -0
  250. package/src/hosted/rebind.js +316 -0
  251. package/src/hosted/retention.js +128 -0
  252. package/src/hosted/server.js +260 -0
  253. package/src/hosted/setup-link.js +64 -0
  254. package/src/hosted/setup.js +323 -0
  255. package/src/hosted/shares.js +444 -0
  256. package/src/hosted/tailscale.js +204 -0
  257. package/src/hosted/tenancy.js +301 -0
  258. package/src/hosted/token-api.js +455 -0
  259. package/src/hosted/token-events.js +60 -0
  260. package/src/hosted/tokens.js +57 -0
  261. package/src/hosted/turnstile.js +49 -0
  262. package/src/hosted/usercontent.js +154 -0
  263. package/src/hosted/zip.js +155 -0
  264. package/src/invocation.js +58 -0
  265. package/src/journey-paths.js +282 -0
  266. package/src/journeys.js +189 -0
  267. package/src/links.js +106 -0
  268. package/src/local-projects.js +182 -0
  269. package/src/lock.js +193 -0
  270. package/src/mcp.js +417 -0
  271. package/src/mentions.js +151 -0
  272. package/src/net.js +15 -0
  273. package/src/notify/config.js +275 -0
  274. package/src/notify/index.js +301 -0
  275. package/src/notify/lease.js +73 -0
  276. package/src/notify/messages.js +212 -0
  277. package/src/notify/run.js +174 -0
  278. package/src/pixeldiff.js +49 -0
  279. package/src/png.js +143 -0
  280. package/src/reactions.js +111 -0
  281. package/src/remote/cli.js +133 -0
  282. package/src/remote/credentials.js +62 -0
  283. package/src/remote/ids.js +12 -0
  284. package/src/remote/login.js +155 -0
  285. package/src/remote/remote-mcp.js +237 -0
  286. package/src/remote/remote-watch.js +291 -0
  287. package/src/remote/remote.js +313 -0
  288. package/src/remote/share-state.js +78 -0
  289. package/src/remote/share.js +369 -0
  290. package/src/remote/terminal.js +13 -0
  291. package/src/serve-state.js +64 -0
  292. package/src/server/app.js +824 -0
  293. package/src/server/devices.js +186 -0
  294. package/src/server/local-console.js +28 -0
  295. package/src/server/local-user.js +59 -0
  296. package/src/server/pairing-routes.js +159 -0
  297. package/src/server/share-routes.js +89 -0
  298. package/src/server/tailscale.js +97 -0
  299. package/src/sitemap.js +431 -0
  300. package/src/store.js +983 -0
  301. package/src/tokens.js +11 -0
  302. package/src/update/routes.js +245 -0
  303. package/src/watch.js +403 -0
  304. package/src/which.js +58 -0
@@ -0,0 +1,510 @@
1
+ // The hosted Shapewall server (v0, push-first): accounts with passkeys,
2
+ // workspaces with roles, projects, `shapewall push`, the board for viewing
3
+ // and commenting, and a token API for the CLI, `watch` and MCP.
4
+ //
5
+ // Surfaces and how each is authenticated:
6
+ // /auth/* passkey ceremonies, recovery, session (cookie)
7
+ // /api/* the account console API (session cookie)
8
+ // /w/:ws/p/:project/ the board client and its API (session cookie)
9
+ // /uc/... live HTML through short-lived signed URLs (no cookie needed)
10
+ // /api/v1/* the token API (Authorization: Bearer swp_...; cookies ignored)
11
+ //
12
+ // Cookie-authenticated writes need JSON and an Origin (or Sec-Fetch-Site)
13
+ // that matches this server, on top of SameSite=Lax cookies. Workspace and
14
+ // project ids that a user cannot see answer 404, never 403, so they cannot
15
+ // be probed. Every tenant query goes through tenant() (src/hosted/tenancy.js).
16
+ //
17
+ // This file is the composition root: it builds the shared pieces and mounts
18
+ // the surfaces in order (Express matches in registration order). The routes
19
+ // live in auth-routes.js, console-routes.js, device-routes.js,
20
+ // board-routes.js, token-api.js (+ token-events.js) and pages.js; the
21
+ // middleware in guards.js.
22
+
23
+ import path from 'node:path'
24
+ import { fileURLToPath } from 'node:url'
25
+ import express from 'express'
26
+ import { CLIENT_DIR } from '../server/app.js'
27
+ import { resolveFile } from '../store.js'
28
+ import { mountAvatarRoutes } from '../accounts/avatar.js'
29
+ import { createAuth } from './auth.js'
30
+ import { listTokens, mountAdmin } from './admin.js'
31
+ import { createDeviceLogins } from './device.js'
32
+ import { cleanEmail } from './mail.js'
33
+ import { mountShareAdmin, mountSharePublic } from './shares.js'
34
+ import { createCommentStore } from './comments.js'
35
+ import { createUsercontent } from './usercontent.js'
36
+ import { mountRebind, prepareOrigins, previousHostGate } from './rebind.js'
37
+ import { mountPublicAdmin } from './public.js'
38
+ import { mintSetupLink } from './setup-link.js'
39
+ import { createAgentPolicy, dispatchHash } from './agent-policy.js'
40
+ import { createPresenceHub, presenceAuthorizer } from './presence.js'
41
+ import { changeEventType, createEventBus, publishMutations } from './events.js'
42
+ import { mountMcpHttp } from './mcp-http.js'
43
+ import { createProjectStore } from './projects.js'
44
+ import { createCleaner } from './cleaner.js'
45
+ import { sweepStaging } from './push.js'
46
+ import { createRateLimiter } from './ratelimit.js'
47
+ import { createQuotas } from './quotas.js'
48
+ import { sessionCookieName } from './cookies.js'
49
+ import { mountOps } from './operator.js'
50
+ import { accounts, tenant } from './tenancy.js'
51
+ import { HttpError, fail } from './http.js'
52
+ import { publicBoardView } from './board-view.js'
53
+ import { createGuards } from './guards.js'
54
+ import { createMembership } from './membership.js'
55
+ import { mountAuthRoutes } from './auth-routes.js'
56
+ import { TOKEN_SCOPES, createApiTokens } from './tokens.js'
57
+ import { INVITE_DAYS, mountConsoleRoutes, mountInviteLinks } from './console-routes.js'
58
+ import { mountDeviceApi } from './device-routes.js'
59
+ import { createBoardContext } from './board-context.js'
60
+ import { mountBoardRoutes } from './board-routes.js'
61
+ import { mountTokenApi } from './token-api.js'
62
+ import { mountPages } from './pages.js'
63
+
64
+ export { changeEventType, publicBoardView, TOKEN_SCOPES, INVITE_DAYS }
65
+
66
+ const HERE = path.dirname(fileURLToPath(import.meta.url))
67
+ // The console is a React app built by Vite (client/src/console, `npm run
68
+ // build`) into dist/console, served under /_console/.
69
+ export const CONSOLE_DIR = path.join(HERE, '..', '..', 'dist', 'console')
70
+
71
+ // The board shell (same as the local server); src/hosted/pages.js has the console's.
72
+ const BOARD_CSP =
73
+ "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self'; frame-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'none'; object-src 'none'"
74
+
75
+ // createHostedApp({ db, dataDir, origin }) returns the Express app.
76
+ // - origin: the public origin (https://app.example.com); the passkey RP ID is its host
77
+ // - signup: 'invite' (default: an invite or the setup code) or 'open'
78
+ // - trustProxy: true behind Caddy on the same host (client IPs from X-Forwarded-For)
79
+ // - secret: key for signed URLs (32+ bytes)
80
+ // - usercontentOrigin: serve live HTML only from this other origin (src/hosted/usercontent.js)
81
+ export function createHostedApp({
82
+ db,
83
+ dataDir,
84
+ origin,
85
+ rpName = 'Shapewall',
86
+ signup = 'invite',
87
+ trustProxy = false,
88
+ secret,
89
+ clientDir = CLIENT_DIR,
90
+ consoleDir = CONSOLE_DIR,
91
+ // Invite emails ({ configured, sendInvite }, src/hosted/mail.js); none by default.
92
+ mailer = null,
93
+ now = () => new Date(),
94
+ log = () => {},
95
+ rateLimiter = createRateLimiter(),
96
+ // Pending `shapewall login`s (src/hosted/device.js), in SQLite.
97
+ deviceLogins = undefined,
98
+ cleaner = createCleaner(),
99
+ // Storage for every workspace in bytes (tests); otherwise from quotas.
100
+ workspaceQuota = undefined,
101
+ // Per-workspace quotas set by the operator (src/hosted/quotas.js).
102
+ quotas = createQuotas(),
103
+ // Where share links point (the Tailscale Funnel listener, src/hosted/tailscale.js); the origin by default.
104
+ shareOrigin = null,
105
+ // Links on the sign-up and share pages (operator's terms and privacy pages) and the abuse contact.
106
+ legal = {},
107
+ // Secondary Funnel listener: no canonical-origin/passkey bootstrap (W6),
108
+ // setup-link minting or storage maintenance. Wrapped by shareOnly().
109
+ guestOnly = false,
110
+ eventBus = null,
111
+ presenceHub = null,
112
+ sweepEveryMs = 10 * 60_000,
113
+ usercontentOrigin = null,
114
+ // The origin this server is moving from (src/hosted/rebind.js), or null.
115
+ previousOrigin = null,
116
+ // Bot checks on open sign-up and comment-link guests ({ siteKey, verify },
117
+ // src/hosted/turnstile.js); none by default.
118
+ turnstile = null,
119
+ // Where "Report this board" on a public project goes (an email address).
120
+ reportEmail = null,
121
+ }) {
122
+ const base = new URL(origin)
123
+ if (!['http:', 'https:'].includes(base.protocol) || base.pathname !== '/' || base.search)
124
+ throw new Error('origin: an http(s) origin without a path')
125
+ const publicOrigin = base.origin
126
+ const secure = base.protocol === 'https:'
127
+ if (!secret || Buffer.byteLength(secret) < 32) throw new Error('secret: at least 32 bytes')
128
+ if (reportEmail !== null && !cleanEmail(reportEmail)) throw new Error('report email: an email address')
129
+ const auth = createAuth({ db, origin: publicOrigin, rpId: base.hostname, rpName, now })
130
+ deviceLogins ||= createDeviceLogins({ db, now })
131
+ const ownsEvents = !eventBus
132
+ const events = eventBus || createEventBus()
133
+ const streams = new Set()
134
+ const policyFor = (t) =>
135
+ createAgentPolicy(t, { now, handleFor: (user) => q.get('SELECT handle FROM users WHERE id = :id', { id: user })?.handle || null })
136
+ const dispatchView = (t, project, board, comment) => {
137
+ const row = createCommentStore(t, project, board).get(comment.id)
138
+ // dispatchHash: what "Send to agent" must send back, so a vouch covers
139
+ // the text its sender saw (src/hosted/board-routes.js).
140
+ return { ...comment, ...policyFor(t).state(project, board, row), dispatchHash: dispatchHash(row) }
141
+ }
142
+ const q = accounts(db)
143
+ const iso = (offset = 0) => new Date(now().getTime() + offset).toISOString()
144
+ // One process can answer for two origins while it moves: the new one
145
+ // (primary) and the old one (previous; sign-in and the move only). Each has
146
+ // its own passkey checks; `related` checks old passkeys on the new origin
147
+ // (WebAuthn Related Origin Requests). The site of a request is its Host.
148
+ const previous = guestOnly ? null : prepareOrigins({ db, origin: publicOrigin, rpId: base.hostname, previousOrigin, now, log })
149
+ const sites = {
150
+ primary: { origin: publicOrigin, host: base.host, auth },
151
+ previous: previous
152
+ ? { ...previous, auth: createAuth({ db, origin: previous.origin, rpId: previous.rpId, rpName, now, tag: 'previous:' }) }
153
+ : null,
154
+ related: previous ? createAuth({ db, origin: publicOrigin, rpId: previous.rpId, rpName, now, tag: 'related:' }) : null,
155
+ nowIso: () => iso(),
156
+ siteOf(req) {
157
+ if (!this.previous) return this.primary
158
+ return String(req.headers.host || '').toLowerCase() === this.previous.host ? this.previous : this.primary
159
+ },
160
+ }
161
+ const siteOf = (req) => req.site || sites.siteOf(req)
162
+ // Where this server's pages (and so its bot check widget) are shown.
163
+ const turnstileHosts = [publicOrigin, previous?.origin, shareOrigin].filter(Boolean).map((value) => new URL(value).hostname.toLowerCase())
164
+ // The passkey checks a sign-in or re-auth uses: the request's own origin,
165
+ // or on the new origin an old passkey when the console asks (`related`).
166
+ const ceremonyAuth = (req) =>
167
+ req.body?.related === true && sites.related && sites.previous.move.endsAt > iso() && siteOf(req) === sites.primary
168
+ ? sites.related
169
+ : siteOf(req).auth
170
+ const SESSION_COOKIE = sessionCookieName(secure)
171
+ const CEREMONY_COOKIE = secure ? '__Host-sw_ceremony' : 'sw_ceremony'
172
+
173
+ const app = express()
174
+ app.disable('x-powered-by')
175
+ app.use(publishMutations({ events, streams }))
176
+ const boardContext = createBoardContext({ db, dataDir })
177
+ // Live HTML (/uc/), on its own origin when one is configured. Created here
178
+ // because its gate runs before every route.
179
+ const usercontent = createUsercontent({
180
+ db,
181
+ secret,
182
+ appOrigin: publicOrigin,
183
+ usercontentOrigin,
184
+ now,
185
+ locate: (ws, pid, boardId, kind, file) => {
186
+ const t = tenant(db, ws)
187
+ const project = createProjectStore(t, { db, dataDir }).get(pid)
188
+ if (!project) return null
189
+ const ctx = boardContext(t, project, boardId)
190
+ return resolveFile(ctx.config, ctx.board.id, kind, file)
191
+ },
192
+ })
193
+ const BOARD_PAGE_CSP = usercontent.boardCsp(BOARD_CSP)
194
+ // true: a proxy on this host. A string ('uniquelocal'): the proxy is on a
195
+ // private network (Docker Compose), in Express's trust proxy syntax.
196
+ app.set('trust proxy', trustProxy ? (typeof trustProxy === 'string' ? trustProxy : 'loopback') : false)
197
+
198
+ const {
199
+ clientIp,
200
+ setCookie,
201
+ limit,
202
+ sessionOf,
203
+ guardWrites,
204
+ needSession,
205
+ needFresh,
206
+ needFreshOrRecovery,
207
+ freshOrThrow,
208
+ inWorkspace,
209
+ inProject,
210
+ handle,
211
+ auditOf,
212
+ } = createGuards({ db, dataDir, auth, quotas, secure, trustProxy, rateLimiter, siteOf, SESSION_COOKIE, iso, now, log })
213
+
214
+ // ---- Headers for everything ----
215
+ app.use((req, res, next) => {
216
+ res.setHeader('X-Content-Type-Options', 'nosniff')
217
+ res.setHeader('Referrer-Policy', 'no-referrer')
218
+ res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()')
219
+ res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
220
+ res.setHeader('X-Robots-Tag', 'noindex, nofollow')
221
+ if (secure) res.setHeader('Strict-Transport-Security', 'max-age=31536000')
222
+ next()
223
+ })
224
+ app.use(usercontent.gate)
225
+ // While moving: which origin a request is for; the old one serves only the move.
226
+ app.use(previousHostGate(sites))
227
+
228
+ const { publicUser, createWorkspace, acceptInvite, revokeGrantsOf, ownerCount } = createMembership({ db, quotas, iso, auditOf })
229
+
230
+ // ---- Health ----
231
+ app.get('/api/health', (req, res) => res.json({ ok: true }))
232
+ // Abuse reports and the legal links (src/hosted/operator.js).
233
+ mountOps(app, { db, fail, clientIp, rateLimiter, now, legal, publicOrigin, shareOrigin })
234
+
235
+ // ---- Auth ----
236
+ const authRouter = express.Router()
237
+ authRouter.use((req, res, next) => {
238
+ res.setHeader('Cache-Control', 'no-store')
239
+ next()
240
+ })
241
+ authRouter.use(guardWrites)
242
+ mountAvatarRoutes(authRouter, { db, needSession, handle, now })
243
+ authRouter.use(express.json({ limit: '64kb' }))
244
+
245
+ const { startSession, ceremonyCookie } = mountAuthRoutes(authRouter, {
246
+ db,
247
+ dataDir,
248
+ auth,
249
+ q,
250
+ iso,
251
+ rateLimiter,
252
+ signup,
253
+ turnstile,
254
+ turnstileHosts,
255
+ sites,
256
+ siteOf,
257
+ ceremonyAuth,
258
+ SESSION_COOKIE,
259
+ CEREMONY_COOKIE,
260
+ clientIp,
261
+ setCookie,
262
+ limit,
263
+ handle,
264
+ sessionOf,
265
+ needSession,
266
+ needFresh,
267
+ needFreshOrRecovery,
268
+ auditOf,
269
+ publicUser,
270
+ createWorkspace,
271
+ acceptInvite,
272
+ revokeGrantsOf,
273
+ ownerCount,
274
+ })
275
+ app.use('/auth', authRouter)
276
+ app.use('/auth', (req, res) => fail(res, 404, 'not_found', 'No such route.'))
277
+
278
+ // Invite emails go out from the operator's domain, so they are off on a
279
+ // server with open sign-up (anyone could make an account and send them).
280
+ const inviteEmailOn = Boolean(mailer?.configured) && signup !== 'open'
281
+
282
+ // Shared with src/hosted/admin.js (members, invites, access, tokens, the
283
+ // audit log, settings, deleting a workspace) and src/hosted/shares.js
284
+ // (share links and their guests).
285
+ const deps = {
286
+ db,
287
+ dataDir,
288
+ auth,
289
+ mailer,
290
+ publicOrigin,
291
+ secure,
292
+ secret,
293
+ rateLimiter,
294
+ iso,
295
+ now,
296
+ clientIp,
297
+ limit,
298
+ handle,
299
+ fail,
300
+ inWorkspace,
301
+ inProject,
302
+ needFresh,
303
+ freshOrThrow,
304
+ auditOf,
305
+ HttpError,
306
+ listTokens,
307
+ setCookie,
308
+ log,
309
+ quotas,
310
+ shareOrigin,
311
+ sites,
312
+ turnstile,
313
+ turnstileHosts,
314
+ needSession,
315
+ startSession,
316
+ ceremonyCookie,
317
+ CEREMONY_COOKIE,
318
+ publicUser,
319
+ }
320
+ // Moving to a new origin: the related-origins file and the rebind routes.
321
+ mountRebind(app, authRouter, deps)
322
+
323
+ // ---- The console API (session) ----
324
+ const consoleApi = express.Router()
325
+ consoleApi.use((req, res, next) => {
326
+ res.setHeader('Cache-Control', 'no-store')
327
+ next()
328
+ })
329
+ consoleApi.use(guardWrites)
330
+ consoleApi.use(express.json({ limit: '64kb' }))
331
+
332
+ const createApiToken = createApiTokens({ iso, auditOf })
333
+ const consoleDeps = {
334
+ db,
335
+ dataDir,
336
+ auth,
337
+ iso,
338
+ now,
339
+ publicOrigin,
340
+ rateLimiter,
341
+ mailer,
342
+ quotas,
343
+ deviceLogins,
344
+ inviteEmailOn,
345
+ clientIp,
346
+ limit,
347
+ handle,
348
+ sessionOf,
349
+ needSession,
350
+ inWorkspace,
351
+ inProject,
352
+ needFresh,
353
+ freshOrThrow,
354
+ auditOf,
355
+ createWorkspace,
356
+ acceptInvite,
357
+ revokeGrantsOf,
358
+ ownerCount,
359
+ createApiToken,
360
+ }
361
+ mountInviteLinks(consoleApi, consoleDeps)
362
+ // Share link preview and opening: no session, the link is the secret.
363
+ const openShare = mountSharePublic(consoleApi, deps)
364
+ // The path-scoped cookie is sent here, so reopening ends the prior visit.
365
+ app.post('/s/:sid/open', guardWrites, express.json({ limit: '64kb' }), ...openShare)
366
+
367
+ consoleApi.use(needSession)
368
+ consoleApi.use(limit('session', (req) => req.user.id))
369
+ mountConsoleRoutes(consoleApi, consoleDeps)
370
+ mountAdmin(consoleApi, deps)
371
+ mountShareAdmin(consoleApi, deps)
372
+ mountPublicAdmin(consoleApi, deps)
373
+
374
+ const { approvalApi } = mountBoardRoutes(app, {
375
+ db,
376
+ dataDir,
377
+ iso,
378
+ now,
379
+ rateLimiter,
380
+ reportEmail,
381
+ usercontent,
382
+ guardWrites,
383
+ needSession,
384
+ limit,
385
+ inWorkspace,
386
+ inProject,
387
+ handle,
388
+ freshOrThrow,
389
+ auditOf,
390
+ clientIp,
391
+ policyFor,
392
+ dispatchView,
393
+ boardContext,
394
+ deps,
395
+ })
396
+
397
+ mountDeviceApi(app, consoleDeps)
398
+
399
+ const { v1, tokenAuth } = mountTokenApi({
400
+ db,
401
+ dataDir,
402
+ iso,
403
+ now,
404
+ publicOrigin,
405
+ rateLimiter,
406
+ quotas,
407
+ workspaceQuota,
408
+ cleaner,
409
+ q,
410
+ events,
411
+ streams,
412
+ usercontent,
413
+ handle,
414
+ auditOf,
415
+ clientIp,
416
+ policyFor,
417
+ boardContext,
418
+ dispatchView,
419
+ needFresh,
420
+ approvalApi,
421
+ })
422
+ // The token API first: it never falls through to the cookie-based console API.
423
+ mountMcpHttp(app, { tokenAuth, origin: publicOrigin, dataDir, db })
424
+ app.use('/api/v1', v1)
425
+ app.use('/api', consoleApi)
426
+
427
+ mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile, sessionOf, BOARD_PAGE_CSP, deps })
428
+
429
+ app.use((req, res) => {
430
+ if (req.path.startsWith('/api/') || req.path.startsWith('/auth/')) return fail(res, 404, 'not_found', 'No such route.')
431
+ res.status(404).type('text').send('Not found')
432
+ })
433
+ // eslint-disable-next-line no-unused-vars
434
+ app.use((error, req, res, next) => {
435
+ const status = Number(error.status || error.statusCode) || 500
436
+ if (status >= 500) log(`error: ${error.name || 'Error'} (request failed; request values omitted)`)
437
+ const api = /^\/(api|auth|w\/[^/]+\/p\/[^/]+\/api|s\/[^/]+\/api)\//.test(req.path)
438
+ if (api) {
439
+ return fail(
440
+ res,
441
+ status,
442
+ error.type === 'entity.parse.failed'
443
+ ? 'invalid_json'
444
+ : error.type === 'entity.too.large'
445
+ ? 'too_large'
446
+ : status === 404
447
+ ? 'not_found'
448
+ : 'error',
449
+ status >= 500 ? 'Something went wrong on the server.' : status === 413 ? 'The request is too large.' : 'Bad request.',
450
+ )
451
+ }
452
+ res
453
+ .status(status)
454
+ .type('text')
455
+ .send(status === 404 ? 'Not found' : 'Error')
456
+ })
457
+
458
+ // The one-time link that lets the first person sign up on an invite-only
459
+ // server. Returns the secret URL for the operator file, or null when users exist.
460
+ app.locals.setupLink = () => {
461
+ if (guestOnly || auth.userCount() > 0) return null
462
+ return mintSetupLink(db, publicOrigin, now())
463
+ }
464
+ app.locals.deviceLogins = deviceLogins
465
+ app.locals.sites = sites
466
+
467
+ // Staging sweep at start and on a timer (unref'd: it never keeps the
468
+ // process alive). app.locals.close() stops it and the cleaner's workers.
469
+ const sweep = () => {
470
+ try {
471
+ sweepStaging({ db, dataDir, now: now() })
472
+ } catch (error) {
473
+ log(`staging sweep: ${error.message}`)
474
+ }
475
+ }
476
+ if (!guestOnly) sweep()
477
+ const sweepTimer = !guestOnly && sweepEveryMs ? setInterval(sweep, sweepEveryMs) : null
478
+ sweepTimer?.unref()
479
+ const ownsPresence = !presenceHub
480
+ const hub = presenceHub || createPresenceHub({ events })
481
+ // A WebSocket upgrade never passes through Express, so its address is
482
+ // worked out here the way req.ip is: X-Forwarded-For hops right to left
483
+ // while the proxy before them is trusted.
484
+ const upgradeIp = (req) => {
485
+ const hops = [
486
+ req.socket.remoteAddress,
487
+ ...String(req.headers['x-forwarded-for'] || '')
488
+ .split(',')
489
+ .map((part) => part.trim())
490
+ .reverse(),
491
+ ]
492
+ const trusted = app.get('trust proxy fn')
493
+ let i = 0
494
+ while (i < hops.length - 1 && hops[i + 1] && trusted(hops[i], i)) i += 1
495
+ return hops[i] || 'unknown'
496
+ }
497
+ const presence = hub.listener(presenceAuthorizer({ db, dataDir, origin: publicOrigin, auth, now, guestOnly }), { clientIp: upgradeIp })
498
+ app.locals.presence = { attach: presence.attach, close: presence.close, hub }
499
+ app.locals.events = events
500
+ app.locals.sweep = sweep
501
+ app.locals.close = async () => {
502
+ presence.close()
503
+ if (ownsPresence) hub.close()
504
+ if (ownsEvents) events.close()
505
+ for (const check of [...streams]) check(true)
506
+ if (sweepTimer) clearInterval(sweepTimer)
507
+ await cleaner.close()
508
+ }
509
+ return app
510
+ }