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,478 @@
1
+ // The server operator's side of hosted Shapewall: the kill switch per
2
+ // workspace, the operator log, abuse reports, and the legal links shown on the
3
+ // sign-up and share pages.
4
+ //
5
+ // The operator is whoever runs the server and holds its data folder; there is
6
+ // no operator account in the web console. Every operator action goes through
7
+ // `shapewall hosted admin ...` (src/hosted/ops-cli.js) on the server's own
8
+ // machine, against the same database (WAL, so the running server keeps
9
+ // going), and lands in the operator log. Actions that change a workspace also
10
+ // leave an entry in that workspace's audit log, so its admins see them.
11
+ //
12
+ // Suspended (`admin suspend`): the workspace is read-only for its members
13
+ // (every write in the console and on the board answers 403
14
+ // workspace_suspended, except "Download a copy"), its share links stop
15
+ // (tenancy.js treats them as off), its API tokens are refused, nobody new can
16
+ // join and `shapewall login` cannot make new tokens for it. Nothing is
17
+ // deleted: `admin resume` restores everything as it was.
18
+
19
+ import fs from 'node:fs'
20
+ import os from 'node:os'
21
+ import path from 'node:path'
22
+ import express from 'express'
23
+ import { ID_PATTERNS, newId, sha256 } from './crypto.js'
24
+ import { transaction } from './db.js'
25
+ import { deleteWorkspaceRows, planWorkspaceExport, writeWorkspaceExport } from './admin.js'
26
+ import { createProjectStore } from './projects.js'
27
+ import { ZIP_MAX_BYTES } from './zip.js'
28
+ import { accounts, allWorkspaces, audit, auditAccount, findShareById, tenant } from './tenancy.js'
29
+
30
+ export const REPORT_REASONS = {
31
+ copyright: 'Copyright or trademark',
32
+ personal_data: 'Personal data shown without consent',
33
+ malware_phishing: 'Malware, phishing or a scam',
34
+ illegal: 'Illegal content',
35
+ other: 'Something else that breaks the rules',
36
+ }
37
+ const REPORTS_OPEN_MAX = 1000
38
+ const REPORT_LIMIT = { limit: 5, windowMs: 60 * 60_000 }
39
+ // eslint-disable-next-line no-control-regex
40
+ const CONTROL_RE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g
41
+
42
+ export class OperatorError extends Error {}
43
+
44
+ // ---- The kill switch ----------------------------------------------------------
45
+
46
+ export function workspaceSuspended(t) {
47
+ const row = t.get('SELECT suspended_at, suspended_reason FROM workspaces WHERE workspace_id = :ws')
48
+ return row?.suspended_at ? { at: row.suspended_at, reason: row.suspended_reason || null } : null
49
+ }
50
+
51
+ // For inWorkspace (src/hosted/guards.js): the message when this request would
52
+ // write to a suspended workspace, or null. Reads pass, and so does the
53
+ // export, so a team can take its data out while the operator looks into it.
54
+ export function suspendedWrite(req, t) {
55
+ if (['GET', 'HEAD', 'OPTIONS'].includes(req.method)) return null
56
+ const pathname = String(req.originalUrl || '').split('?')[0]
57
+ if (req.method === 'POST' && /^\/api\/workspaces\/[^/]+\/export$/.test(pathname)) return null
58
+ const suspended = workspaceSuspended(t)
59
+ if (!suspended) return null
60
+ return `This workspace is suspended by the server operator${suspended.reason ? ` (${suspended.reason})` : ''}. It is read-only until they lift it; you can still download a copy.`
61
+ }
62
+
63
+ // ---- The operator log ---------------------------------------------------------
64
+
65
+ export function operatorName(explicit) {
66
+ if (explicit) return clean(explicit, 80)
67
+ let user = 'unknown'
68
+ try {
69
+ user = os.userInfo().username
70
+ } catch {
71
+ /* no passwd entry (containers) */
72
+ }
73
+ return `${user}@${os.hostname()}`
74
+ }
75
+
76
+ function clean(value, max) {
77
+ return String(value ?? '')
78
+ .replace(CONTROL_RE, '')
79
+ .replace(/\s+/g, ' ')
80
+ .trim()
81
+ .slice(0, max)
82
+ }
83
+
84
+ function logOperator(db, { operator, action, target = null, detail = {}, at }) {
85
+ accounts(db).run('INSERT INTO operator_log (operator, action, target, detail, at) VALUES (:operator, :action, :target, :detail, :at)', {
86
+ operator,
87
+ action,
88
+ target,
89
+ detail: JSON.stringify(detail),
90
+ at,
91
+ })
92
+ }
93
+
94
+ export function operatorLog(db, { limit = 50 } = {}) {
95
+ return accounts(db)
96
+ .all('SELECT id, operator, action, target, detail, at FROM operator_log ORDER BY id DESC LIMIT :limit', { limit })
97
+ .map((row) => ({ ...row, detail: JSON.parse(row.detail || '{}') }))
98
+ }
99
+
100
+ export function recordOperatorRead(db, { action, operator, now = new Date() }) {
101
+ if (!['list', 'reports', 'log'].includes(action)) throw new OperatorError('unknown operator read action')
102
+ logOperator(db, { operator, action, at: now.toISOString() })
103
+ }
104
+
105
+ // ---- Workspaces ---------------------------------------------------------------
106
+
107
+ function findWorkspace(db, workspaceId) {
108
+ if (!ID_PATTERNS.workspace.test(String(workspaceId || ''))) throw new OperatorError(`"${workspaceId}" is not a workspace id (ws_...)`)
109
+ const t = tenant(db, workspaceId)
110
+ const row = t.get(
111
+ 'SELECT id, name, created_at, suspended_at, suspended_reason, retention_versions FROM workspaces WHERE workspace_id = :ws',
112
+ )
113
+ if (!row) throw new OperatorError(`no workspace ${workspaceId}`)
114
+ return { t, row }
115
+ }
116
+
117
+ // Every workspace with what it uses: people, projects, boards, bytes on disk
118
+ // (committed boards), the last push, whether it is suspended, open reports.
119
+ export function listWorkspaces(db, { quotas = null } = {}) {
120
+ const open = new Map()
121
+ for (const row of accounts(db).all(
122
+ 'SELECT target_workspace AS ws, COUNT(*) AS n FROM reports WHERE closed_at IS NULL AND target_workspace IS NOT NULL GROUP BY target_workspace',
123
+ ))
124
+ open.set(row.ws, row.n)
125
+ return allWorkspaces(db).map((workspace) => {
126
+ const t = tenant(db, workspace.id)
127
+ const boards = t.get(
128
+ 'SELECT COUNT(*) AS n, COALESCE(SUM(bytes), 0) AS bytes, MAX(pushed_at) AS last_push FROM boards WHERE workspace_id = :ws',
129
+ )
130
+ const quota = quotas?.of(workspace.id)
131
+ return {
132
+ id: workspace.id,
133
+ name: workspace.name,
134
+ createdAt: workspace.created_at,
135
+ members: t.get('SELECT COUNT(*) AS n FROM memberships WHERE workspace_id = :ws').n,
136
+ owners: t
137
+ .all(
138
+ "SELECT u.handle FROM memberships m JOIN users u ON u.id = m.user_id WHERE m.workspace_id = :ws AND m.role = 'owner' ORDER BY m.created_at",
139
+ )
140
+ .map((row) => row.handle),
141
+ projects: t.get('SELECT COUNT(*) AS n FROM projects WHERE workspace_id = :ws').n,
142
+ boards: boards.n,
143
+ bytes: boards.bytes,
144
+ storageQuotaBytes: quota ? quota.storageBytes : null,
145
+ lastPushAt: boards.last_push || null,
146
+ shareLinksOn: Boolean(workspace.share_links),
147
+ suspendedAt: workspace.suspended_at || null,
148
+ suspendedReason: workspace.suspended_reason || null,
149
+ retentionVersions: workspace.retention_versions ?? null,
150
+ openReports: open.get(workspace.id) || 0,
151
+ }
152
+ })
153
+ }
154
+
155
+ export function suspendWorkspace(db, { workspaceId, reason, operator, now = new Date() }) {
156
+ const text = clean(reason, 200)
157
+ if (!text) throw new OperatorError('say why: --reason "..." (shown to the workspace admins)')
158
+ const { t, row } = findWorkspace(db, workspaceId)
159
+ if (row.suspended_at) throw new OperatorError(`${workspaceId} is already suspended (since ${row.suspended_at})`)
160
+ const at = now.toISOString()
161
+ transaction(db, () => {
162
+ t.run('UPDATE workspaces SET suspended_at = :at, suspended_reason = :reason WHERE workspace_id = :ws', { at, reason: text })
163
+ audit(t, { action: 'workspace_suspended', detail: { by: 'operator', reason: text }, at })
164
+ logOperator(db, { operator, action: 'suspend', target: workspaceId, detail: { name: row.name, reason: text }, at })
165
+ })
166
+ return { id: workspaceId, name: row.name, suspendedAt: at, reason: text }
167
+ }
168
+
169
+ export function resumeWorkspace(db, { workspaceId, note = '', operator, now = new Date() }) {
170
+ const { t, row } = findWorkspace(db, workspaceId)
171
+ if (!row.suspended_at) throw new OperatorError(`${workspaceId} is not suspended`)
172
+ const at = now.toISOString()
173
+ transaction(db, () => {
174
+ t.run('UPDATE workspaces SET suspended_at = NULL, suspended_reason = NULL WHERE workspace_id = :ws')
175
+ audit(t, { action: 'workspace_resumed', detail: { by: 'operator' }, at })
176
+ logOperator(db, {
177
+ operator,
178
+ action: 'resume',
179
+ target: workspaceId,
180
+ detail: { name: row.name, note: clean(note, 200), suspendedAt: row.suspended_at },
181
+ at,
182
+ })
183
+ })
184
+ return { id: workspaceId, name: row.name }
185
+ }
186
+
187
+ // Deleting needs the workspace's name typed back (`--confirm "<name>"`), as in
188
+ // the console. Rows and files go; the operator log and an account-level
189
+ // `workspace_deleted` entry stay.
190
+ export function deleteWorkspace(db, dataDir, { workspaceId, confirm, operator, now = new Date() }) {
191
+ const { t, row } = findWorkspace(db, workspaceId)
192
+ if (confirm !== row.name) throw new OperatorError(`type the workspace's name to confirm: --confirm "${row.name}"`)
193
+ const at = now.toISOString()
194
+ const projects = t.get('SELECT COUNT(*) AS n FROM projects WHERE workspace_id = :ws').n
195
+ let removeFiles
196
+ transaction(db, () => {
197
+ removeFiles = deleteWorkspaceRows(t, dataDir)
198
+ auditAccount(db, { action: 'workspace_deleted', target: workspaceId, detail: { by: 'operator', name: row.name }, at })
199
+ logOperator(db, { operator, action: 'delete', target: workspaceId, detail: { name: row.name, projects }, at })
200
+ })
201
+ removeFiles()
202
+ return { id: workspaceId, name: row.name, projects }
203
+ }
204
+
205
+ // The same zip as the console's "Download a copy", written to a file (0600).
206
+ export async function exportWorkspace(db, dataDir, { workspaceId, out, operator, now = () => new Date() }) {
207
+ const { t, row } = findWorkspace(db, workspaceId)
208
+ const iso = () => now().toISOString()
209
+ const projects = createProjectStore(t, { db, dataDir }).list()
210
+ const exported = planWorkspaceExport(t, { dataDir, projects, iso })
211
+ if (exported.total > ZIP_MAX_BYTES) throw new OperatorError('this workspace is over 3.5 GB, too large for one zip: use hosted backup')
212
+ const file = path.resolve(out)
213
+ if (fs.existsSync(file)) throw new OperatorError(`${file} exists; pick a new file name`)
214
+ fs.mkdirSync(path.dirname(file), { recursive: true })
215
+ const fd = fs.openSync(file, 'wx', 0o600)
216
+ try {
217
+ await writeWorkspaceExport(t, exported, {
218
+ write: (buffer) => fs.writeSync(fd, buffer) && null,
219
+ now: now(),
220
+ iso,
221
+ exportedBy: 'operator',
222
+ })
223
+ } catch (error) {
224
+ fs.closeSync(fd)
225
+ fs.rmSync(file, { force: true })
226
+ throw error
227
+ }
228
+ fs.closeSync(fd)
229
+ const at = iso()
230
+ transaction(db, () => {
231
+ audit(t, { action: 'workspace_exported', detail: { by: 'operator', projects: projects.length, files: exported.files }, at })
232
+ logOperator(db, { operator, action: 'export', target: workspaceId, detail: { name: row.name, file, files: exported.files }, at })
233
+ })
234
+ return { id: workspaceId, name: row.name, file, files: exported.files, bytes: fs.statSync(file).size }
235
+ }
236
+
237
+ // How many older versions per page and viewport the retention job keeps for
238
+ // one workspace (null: the server default). See src/hosted/retention.js.
239
+ export function setRetention(db, { workspaceId, keep, operator, now = new Date() }) {
240
+ const { t, row } = findWorkspace(db, workspaceId)
241
+ if (keep !== null && !(Number.isInteger(keep) && keep >= 0 && keep <= 100))
242
+ throw new OperatorError('--keep: a whole number from 0 to 100, or "default"')
243
+ const at = now.toISOString()
244
+ transaction(db, () => {
245
+ t.run('UPDATE workspaces SET retention_versions = :keep WHERE workspace_id = :ws', { keep })
246
+ logOperator(db, { operator, action: 'retention', target: workspaceId, detail: { name: row.name, keep }, at })
247
+ })
248
+ return { id: workspaceId, name: row.name, keep }
249
+ }
250
+
251
+ export function recordRetentionRun(db, { operator, result, dryRun, now = new Date() }) {
252
+ logOperator(db, {
253
+ operator,
254
+ action: dryRun ? 'retention_dry_run' : 'retention_run',
255
+ detail: { files: result.files, bytes: result.bytes, boards: result.boards },
256
+ at: now.toISOString(),
257
+ })
258
+ }
259
+
260
+ // ---- Abuse reports ------------------------------------------------------------
261
+
262
+ export function listReports(db, { all = false, limit = 100 } = {}) {
263
+ const sql = all
264
+ ? 'SELECT * FROM reports ORDER BY created_at DESC LIMIT :limit'
265
+ : 'SELECT * FROM reports WHERE closed_at IS NULL ORDER BY created_at DESC LIMIT :limit'
266
+ return accounts(db)
267
+ .all(sql, { limit })
268
+ .map((row) => ({
269
+ id: row.id,
270
+ url: row.url,
271
+ reason: row.reason,
272
+ details: row.details,
273
+ contact: row.contact,
274
+ workspace: row.target_workspace,
275
+ createdAt: row.created_at,
276
+ closedAt: row.closed_at,
277
+ closedNote: row.closed_note,
278
+ }))
279
+ }
280
+
281
+ export function closeReport(db, { id, note = '', operator, now = new Date() }) {
282
+ const q = accounts(db)
283
+ const row = /^rp_[a-f0-9]{24}$/.test(String(id || '')) ? q.get('SELECT * FROM reports WHERE id = :id', { id }) : null
284
+ if (!row) throw new OperatorError(`no report ${id}`)
285
+ if (row.closed_at) throw new OperatorError(`${id} is already closed`)
286
+ const at = now.toISOString()
287
+ const text = clean(note, 500)
288
+ transaction(db, () => {
289
+ q.run('UPDATE reports SET closed_at = :at, closed_note = :note WHERE id = :id', { at, note: text, id })
290
+ logOperator(db, { operator, action: 'report_closed', target: id, detail: { note: text, workspace: row.target_workspace }, at })
291
+ })
292
+ return { id, closedAt: at }
293
+ }
294
+
295
+ // The workspace a reported URL points into, when it is one of ours.
296
+ function reportTarget(db, value, origins) {
297
+ let url
298
+ try {
299
+ url = new URL(value)
300
+ } catch {
301
+ return null
302
+ }
303
+ if (!origins.includes(url.origin)) return null
304
+ const share = /^\/s\/(sh_[a-f0-9]{24})(?:\/|$)/.exec(url.pathname)
305
+ if (share) return findShareById(db, share[1])?.workspace_id || null
306
+ const ws = /^\/(?:w|uc)\/(ws_[a-f0-9]{24})(?:\/|$)/.exec(url.pathname)
307
+ return ws ? ws[1] : null
308
+ }
309
+
310
+ export function createReport(db, { url, reason, details, contact, ip, origins, now = new Date() }) {
311
+ const q = accounts(db)
312
+ let link
313
+ try {
314
+ const parsed = new URL(clean(url, 2000))
315
+ if (!['http:', 'https:'].includes(parsed.protocol)) throw new Error('scheme')
316
+ // Share/setup/invite fragments and signed query strings are secrets.
317
+ // The path identifies the resource; none of those credentials are needed.
318
+ link = `${parsed.origin}${parsed.pathname}`
319
+ } catch (error) {
320
+ throw new OperatorError('Give the full address of what you are reporting (https://...).', { cause: error })
321
+ }
322
+ if (!Object.hasOwn(REPORT_REASONS, reason)) throw new OperatorError('Pick a reason.')
323
+ if (q.get('SELECT COUNT(*) AS n FROM reports WHERE closed_at IS NULL').n >= REPORTS_OPEN_MAX)
324
+ throw new OperatorError('The report queue is full right now. Try again later or email the operator.')
325
+ const id = newId('rp')
326
+ q.run(
327
+ `INSERT INTO reports (id, url, reason, details, contact, target_workspace, ip_hash, created_at)
328
+ VALUES (:id, :url, :reason, :details, :contact, :target, :ip, :at)`,
329
+ {
330
+ id,
331
+ url: link,
332
+ reason,
333
+ details: String(details ?? '')
334
+ .replace(CONTROL_RE, '')
335
+ .trim()
336
+ .slice(0, 2000),
337
+ contact: clean(contact, 200) || null,
338
+ target: reportTarget(db, link, origins),
339
+ // Enough to spot a flood from one address; not the address itself.
340
+ ip: ip ? sha256(`report:${ip}`).slice(0, 16) : null,
341
+ at: now.toISOString(),
342
+ },
343
+ )
344
+ return { id }
345
+ }
346
+
347
+ // ---- Routes: /report and /api/legal ------------------------------------------
348
+
349
+ const escapeHtml = (value) =>
350
+ String(value ?? '').replace(/[&<>"']/g, (char) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[char])
351
+
352
+ const REPORT_CSP =
353
+ "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'; object-src 'none'"
354
+
355
+ function page(title, body) {
356
+ return `<!DOCTYPE html>
357
+ <html lang="en">
358
+ <head>
359
+ <meta charset="utf-8">
360
+ <meta name="viewport" content="width=device-width, initial-scale=1">
361
+ <meta name="robots" content="noindex, nofollow">
362
+ <title>${escapeHtml(title)}</title>
363
+ <style>
364
+ :root { color-scheme: light dark; font-family: system-ui, sans-serif; line-height: 1.5; }
365
+ body { max-width: 36rem; margin: 2rem auto; padding: 0 1rem; }
366
+ label { display: block; font-weight: 600; margin-top: 1rem; }
367
+ input, select, textarea { width: 100%; box-sizing: border-box; font: inherit; padding: 0.6rem; margin-top: 0.3rem; min-height: 44px; }
368
+ textarea { min-height: 8rem; }
369
+ button { margin-top: 1.25rem; font: inherit; padding: 0.7rem 1.2rem; min-height: 44px; cursor: pointer; }
370
+ .note { opacity: 0.8; font-size: 0.95em; }
371
+ .error { color: #b42318; font-weight: 600; }
372
+ </style>
373
+ </head>
374
+ <body>
375
+ ${body}
376
+ </body>
377
+ </html>`
378
+ }
379
+
380
+ function legalLinks(legal) {
381
+ const items = [
382
+ ['terms', 'Terms'],
383
+ ['privacy', 'Privacy'],
384
+ ['acceptableUse', 'Acceptable use'],
385
+ ['subprocessors', 'Subprocessors'],
386
+ ].filter(([key]) => legal[key])
387
+ return items.length
388
+ ? `<p class="note">${items.map(([key, label]) => `<a href="${escapeHtml(legal[key])}" rel="noopener">${label}</a>`).join(' · ')}</p>`
389
+ : ''
390
+ }
391
+
392
+ function reportForm(legal, { error = '', values = {} } = {}) {
393
+ const options = Object.entries(REPORT_REASONS)
394
+ .map(([key, label]) => `<option value="${key}"${values.reason === key ? ' selected' : ''}>${escapeHtml(label)}</option>`)
395
+ .join('')
396
+ return page(
397
+ 'Report abuse',
398
+ `<h1>Report abuse</h1>
399
+ <p>Tell the people who run this server about a board, share link or page that breaks the rules. Reports go to the server's operator, not to the workspace.</p>
400
+ ${legal.abuseEmail ? `<p class="note">Urgent, or a legal notice? Email <a href="mailto:${escapeHtml(legal.abuseEmail)}">${escapeHtml(legal.abuseEmail)}</a>.</p>` : ''}
401
+ ${error ? `<p class="error" role="alert">${escapeHtml(error)}</p>` : ''}
402
+ <form method="post" action="/report">
403
+ <label for="url">Address of the page</label>
404
+ <input id="url" name="url" type="url" required maxlength="2000" placeholder="https://..." value="${escapeHtml(values.url || '')}">
405
+ <label for="reason">What is wrong</label>
406
+ <select id="reason" name="reason" required><option value="">Choose one</option>${options}</select>
407
+ <label for="details">Details (optional)</label>
408
+ <textarea id="details" name="details" maxlength="2000">${escapeHtml(values.details || '')}</textarea>
409
+ <label for="contact">How to reach you (optional)</label>
410
+ <input id="contact" name="contact" maxlength="200" autocomplete="email" value="${escapeHtml(values.contact || '')}">
411
+ <p class="note">Only the operator sees your report. Leave a contact if you want an answer.</p>
412
+ <button type="submit">Send report</button>
413
+ </form>
414
+ ${legalLinks(legal)}`,
415
+ )
416
+ }
417
+
418
+ export function legalSettings(env = process.env) {
419
+ const pick = (name) => {
420
+ const value = String(env[name] || '').trim()
421
+ if (!value) return null
422
+ if (!/^https?:\/\/[^\s"<>]+$/i.test(value)) throw new Error(`${name}: an http(s) URL`)
423
+ return value
424
+ }
425
+ const abuse = String(env.SHAPEWALL_HOSTED_ABUSE_EMAIL || '').trim()
426
+ if (abuse && !/^[^\s@<>"]+@[^\s@<>"]+\.[^\s@<>"]+$/.test(abuse)) throw new Error('SHAPEWALL_HOSTED_ABUSE_EMAIL: an email address')
427
+ return {
428
+ terms: pick('SHAPEWALL_HOSTED_TERMS_URL'),
429
+ privacy: pick('SHAPEWALL_HOSTED_PRIVACY_URL'),
430
+ acceptableUse: pick('SHAPEWALL_HOSTED_AUP_URL'),
431
+ subprocessors: pick('SHAPEWALL_HOSTED_SUBPROCESSORS_URL'),
432
+ abuseEmail: abuse || null,
433
+ }
434
+ }
435
+
436
+ export function mountOps(app, { db, fail, clientIp, rateLimiter, now, legal = {}, publicOrigin, shareOrigin = null }) {
437
+ const links = {
438
+ terms: legal.terms || null,
439
+ privacy: legal.privacy || null,
440
+ acceptableUse: legal.acceptableUse || null,
441
+ subprocessors: legal.subprocessors || null,
442
+ abuseEmail: legal.abuseEmail || null,
443
+ }
444
+ const origins = [publicOrigin, shareOrigin].filter(Boolean)
445
+ app.get('/api/legal', (req, res) => {
446
+ res.setHeader('Cache-Control', 'public, max-age=300')
447
+ res.json({ ...links, report: '/report' })
448
+ })
449
+ const html = (res, status, body) => {
450
+ res.setHeader('Content-Security-Policy', REPORT_CSP)
451
+ res.setHeader('X-Frame-Options', 'DENY')
452
+ res.setHeader('Cache-Control', 'no-store')
453
+ res.status(status).type('html').send(body)
454
+ }
455
+ app.get('/report', (req, res) => html(res, 200, reportForm(links, { values: { url: String(req.query.url || '').slice(0, 2000) } })))
456
+ app.post('/report', express.urlencoded({ extended: false, limit: '16kb', parameterLimit: 10 }), (req, res) => {
457
+ const values = { url: req.body?.url, reason: req.body?.reason, details: req.body?.details, contact: req.body?.contact }
458
+ const hit = rateLimiter.hit('report', clientIp(req), REPORT_LIMIT)
459
+ if (!hit.ok) {
460
+ res.setHeader('Retry-After', String(hit.retryAfter))
461
+ return html(res, 429, reportForm(links, { error: 'That is a lot of reports from here. Try again in an hour.', values }))
462
+ }
463
+ try {
464
+ createReport(db, { ...values, ip: clientIp(req), origins, now: now() })
465
+ } catch (error) {
466
+ if (!(error instanceof OperatorError)) return fail(res, 500, 'server_error', 'Something went wrong on the server.')
467
+ return html(res, 400, reportForm(links, { error: error.message, values }))
468
+ }
469
+ html(
470
+ res,
471
+ 201,
472
+ page(
473
+ 'Report sent',
474
+ `<h1>Thanks, the report is in</h1><p>The operator of this server reviews every report. ${values.contact ? 'They may contact you about it.' : ''}</p>${legalLinks(links)}`,
475
+ ),
476
+ )
477
+ })
478
+ }