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.
- package/CHANGELOG.md +139 -0
- package/Dockerfile.agent +45 -0
- package/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +651 -2
- package/THIRD_PARTY.md +165 -0
- package/bin/shapewall.js +29 -0
- package/dist/agent/package-lock.json +8384 -0
- package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
- package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
- package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
- package/dist/client/assets/History-CEv6b-av.js +1 -0
- package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
- package/dist/client/assets/Management-DCfhcEUY.js +1 -0
- package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
- package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
- package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
- package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
- package/dist/client/assets/index-ByQRKVit.css +2 -0
- package/dist/client/assets/index-DweQ8Qtu.js +29 -0
- package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
- package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
- package/dist/client/assets/square-A8EnIYLR.js +1 -0
- package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
- package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
- package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
- package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
- package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
- package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
- package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
- package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
- package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
- package/dist/client/assets/ui-CLbws0rx.js +41 -0
- package/dist/client/build-info.json +1 -0
- package/dist/client/favicon.svg +7 -0
- package/dist/client/icons/apple-touch-icon.png +0 -0
- package/dist/client/icons/icon-192.png +0 -0
- package/dist/client/icons/icon-512.png +0 -0
- package/dist/client/icons/maskable-512.png +0 -0
- package/dist/client/index.html +25 -0
- package/dist/client/manifest.webmanifest +15 -0
- package/dist/client/sw.js +53 -0
- package/dist/client/theme-boot.js +27 -0
- package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
- package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
- package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
- package/dist/console/assets/console-C7uJLLOn.js +51 -0
- package/dist/console/assets/console-R8IBY3Am.css +2 -0
- package/dist/console/build-info.json +1 -0
- package/dist/console/console.html +21 -0
- package/dist/console/favicon.svg +7 -0
- package/dist/console/icons/apple-touch-icon.png +0 -0
- package/dist/console/icons/icon-192.png +0 -0
- package/dist/console/icons/icon-512.png +0 -0
- package/dist/console/icons/maskable-512.png +0 -0
- package/dist/console/manifest.webmanifest +15 -0
- package/dist/console/theme-boot.js +27 -0
- package/docs/agent-hardening.md +342 -0
- package/docs/agent-recipes.md +187 -0
- package/docs/agent-workflow.md +320 -0
- package/docs/agents.md +894 -0
- package/docs/architecture.md +242 -0
- package/docs/buzz.md +140 -0
- package/docs/capture-warnings.md +57 -0
- package/docs/config.md +489 -0
- package/docs/deployment.md +289 -0
- package/docs/design-system.md +162 -0
- package/docs/github.md +107 -0
- package/docs/hosted.md +764 -0
- package/docs/images/board-overview.png +0 -0
- package/docs/images/journeys.png +0 -0
- package/docs/images/live-comment.png +0 -0
- package/docs/images/phone-comment.png +0 -0
- package/docs/images/phone-overview.png +0 -0
- package/docs/images/variant.png +0 -0
- package/docs/interaction-audits.md +40 -0
- package/docs/journeys.md +137 -0
- package/docs/local-accounts.md +80 -0
- package/docs/notifications.md +237 -0
- package/docs/page-checks.md +69 -0
- package/docs/performance.md +174 -0
- package/docs/starter-agent.md +325 -0
- package/docs/updates.md +87 -0
- package/examples/audits/interaction.audit.json +17 -0
- package/package.json +132 -4
- package/src/accounts/avatar.js +121 -0
- package/src/accounts/local.js +464 -0
- package/src/agent/acp/command.js +204 -0
- package/src/agent/acp/detect.js +75 -0
- package/src/agent/acp/sandbox.js +160 -0
- package/src/agent/agent-health.js +75 -0
- package/src/agent/backends/acp.js +1297 -0
- package/src/agent/backends/index.js +61 -0
- package/src/agent/backends/openai.js +308 -0
- package/src/agent/composer.js +221 -0
- package/src/agent/config.js +338 -0
- package/src/agent/context.js +191 -0
- package/src/agent/control.js +122 -0
- package/src/agent/errors.js +17 -0
- package/src/agent/feedback-loop.js +137 -0
- package/src/agent/manage.js +94 -0
- package/src/agent/next-steps.js +51 -0
- package/src/agent/owner-mcp.js +38 -0
- package/src/agent/plain-text.js +52 -0
- package/src/agent/ratings-core.js +213 -0
- package/src/agent/ratings.js +100 -0
- package/src/agent/reply-text.js +67 -0
- package/src/agent/routes.js +1065 -0
- package/src/agent/run.js +326 -0
- package/src/agent/settings-routes.js +704 -0
- package/src/agent/showable.js +23 -0
- package/src/agent/starter/approvals.js +181 -0
- package/src/agent/starter/build-image.js +90 -0
- package/src/agent/starter/cli.js +163 -0
- package/src/agent/starter/disk.js +53 -0
- package/src/agent/starter/doctor.js +180 -0
- package/src/agent/starter/egress-proxy.js +351 -0
- package/src/agent/starter/gate.js +90 -0
- package/src/agent/starter/loop.js +114 -0
- package/src/agent/starter/model-relay.js +289 -0
- package/src/agent/starter/pause.js +14 -0
- package/src/agent/starter/probe.js +272 -0
- package/src/agent/starter/prompt.js +41 -0
- package/src/agent/starter/push-guard.js +146 -0
- package/src/agent/starter/rpc.js +164 -0
- package/src/agent/starter/runtime.js +287 -0
- package/src/agent/starter/sandbox-main.js +121 -0
- package/src/agent/starter/settings.js +290 -0
- package/src/agent/starter/source.js +69 -0
- package/src/agent/starter/supervisor.js +755 -0
- package/src/agent/starter/sweep.js +46 -0
- package/src/agent/starter/tools.js +524 -0
- package/src/agent/starter/watch.js +84 -0
- package/src/agent/starter/worktree.js +409 -0
- package/src/agent/text-diff.js +210 -0
- package/src/agent/thread-history.js +223 -0
- package/src/agent/threads.js +399 -0
- package/src/agent/tools.js +200 -0
- package/src/agent/turn-changes.js +424 -0
- package/src/agent/undo-state.js +26 -0
- package/src/agent/user-settings.js +308 -0
- package/src/auth/record.js +375 -0
- package/src/broken-links.js +164 -0
- package/src/buzz/client.js +75 -0
- package/src/buzz/config.js +151 -0
- package/src/buzz/environment.js +13 -0
- package/src/buzz/gateway.js +192 -0
- package/src/buzz/routes.js +61 -0
- package/src/buzz/signer-child.js +205 -0
- package/src/buzz/store.js +89 -0
- package/src/buzz/transport.js +60 -0
- package/src/buzz/verify.js +140 -0
- package/src/capture/actions.js +159 -0
- package/src/capture/capture.js +776 -0
- package/src/capture/dom-names.js +189 -0
- package/src/capture/element-map.js +509 -0
- package/src/capture/live.js +827 -0
- package/src/capture/media-scheme.js +317 -0
- package/src/capture/network-summary.js +86 -0
- package/src/capture/pacing.js +34 -0
- package/src/capture/requests.js +167 -0
- package/src/capture/sanitize.js +752 -0
- package/src/checks/accessibility.js +134 -0
- package/src/checks/budgets.js +58 -0
- package/src/checks/cli.js +55 -0
- package/src/checks/comparison.js +113 -0
- package/src/checks/environments.js +32 -0
- package/src/checks/flow-config.js +82 -0
- package/src/checks/flow-origin.js +53 -0
- package/src/checks/flows.js +398 -0
- package/src/checks/lighthouse-auth.js +145 -0
- package/src/checks/lighthouse-findings.js +130 -0
- package/src/checks/lighthouse.js +395 -0
- package/src/checks/robots.js +85 -0
- package/src/checks/routes.js +52 -0
- package/src/checks/run-control.js +259 -0
- package/src/checks/runner.js +724 -0
- package/src/checks/schema.js +133 -0
- package/src/checks/seo-analysis.js +443 -0
- package/src/checks/seo-probe.js +190 -0
- package/src/checks/seo-snapshot.js +99 -0
- package/src/checks/store.js +214 -0
- package/src/checks/transport.js +47 -0
- package/src/cli/args.js +319 -0
- package/src/cli/capture.js +79 -0
- package/src/cli/commands.js +92 -0
- package/src/cli/comments.js +84 -0
- package/src/cli/help.js +243 -0
- package/src/cli/init.js +214 -0
- package/src/cli/misc.js +64 -0
- package/src/cli/notify.js +87 -0
- package/src/cli/open.js +22 -0
- package/src/cli/serve.js +274 -0
- package/src/cli/variant.js +82 -0
- package/src/cli/watch.js +70 -0
- package/src/cli.js +130 -0
- package/src/comment-fields.js +76 -0
- package/src/comments-cli.js +208 -0
- package/src/config-reload.js +100 -0
- package/src/config.js +1061 -0
- package/src/devices-cli.js +21 -0
- package/src/diff.js +95 -0
- package/src/discover.js +284 -0
- package/src/doctor.js +295 -0
- package/src/event-log.js +268 -0
- package/src/events-cli.js +102 -0
- package/src/events.js +113 -0
- package/src/export.js +119 -0
- package/src/feedback.js +246 -0
- package/src/github.js +551 -0
- package/src/hosted/LICENSE.md +105 -0
- package/src/hosted/access-cli.js +78 -0
- package/src/hosted/admin.js +682 -0
- package/src/hosted/agent-approvals.js +163 -0
- package/src/hosted/agent-policy.js +189 -0
- package/src/hosted/app.js +510 -0
- package/src/hosted/auth-routes.js +540 -0
- package/src/hosted/auth.js +536 -0
- package/src/hosted/backup.js +483 -0
- package/src/hosted/board-context.js +35 -0
- package/src/hosted/board-routes.js +486 -0
- package/src/hosted/board-view.js +94 -0
- package/src/hosted/clean-worker.js +24 -0
- package/src/hosted/cleaner.js +89 -0
- package/src/hosted/comments.js +355 -0
- package/src/hosted/console-routes.js +401 -0
- package/src/hosted/cookies.js +18 -0
- package/src/hosted/crypto.js +80 -0
- package/src/hosted/db.js +496 -0
- package/src/hosted/device-routes.js +175 -0
- package/src/hosted/device.js +233 -0
- package/src/hosted/events.js +63 -0
- package/src/hosted/guards.js +172 -0
- package/src/hosted/guest-view.js +65 -0
- package/src/hosted/http.js +39 -0
- package/src/hosted/import.js +300 -0
- package/src/hosted/mail.js +73 -0
- package/src/hosted/mcp-http.js +71 -0
- package/src/hosted/membership.js +114 -0
- package/src/hosted/operator.js +478 -0
- package/src/hosted/ops-cli.js +345 -0
- package/src/hosted/pages.js +146 -0
- package/src/hosted/permissions.js +113 -0
- package/src/hosted/presence.js +372 -0
- package/src/hosted/projects.js +124 -0
- package/src/hosted/public.js +109 -0
- package/src/hosted/push.js +622 -0
- package/src/hosted/quotas.js +115 -0
- package/src/hosted/ratelimit.js +96 -0
- package/src/hosted/rebind.js +316 -0
- package/src/hosted/retention.js +128 -0
- package/src/hosted/server.js +260 -0
- package/src/hosted/setup-link.js +64 -0
- package/src/hosted/setup.js +323 -0
- package/src/hosted/shares.js +444 -0
- package/src/hosted/tailscale.js +204 -0
- package/src/hosted/tenancy.js +301 -0
- package/src/hosted/token-api.js +455 -0
- package/src/hosted/token-events.js +60 -0
- package/src/hosted/tokens.js +57 -0
- package/src/hosted/turnstile.js +49 -0
- package/src/hosted/usercontent.js +154 -0
- package/src/hosted/zip.js +155 -0
- package/src/invocation.js +58 -0
- package/src/journey-paths.js +282 -0
- package/src/journeys.js +189 -0
- package/src/links.js +106 -0
- package/src/local-projects.js +182 -0
- package/src/lock.js +193 -0
- package/src/mcp.js +417 -0
- package/src/mentions.js +151 -0
- package/src/net.js +15 -0
- package/src/notify/config.js +275 -0
- package/src/notify/index.js +301 -0
- package/src/notify/lease.js +73 -0
- package/src/notify/messages.js +212 -0
- package/src/notify/run.js +174 -0
- package/src/pixeldiff.js +49 -0
- package/src/png.js +143 -0
- package/src/reactions.js +111 -0
- package/src/remote/cli.js +133 -0
- package/src/remote/credentials.js +62 -0
- package/src/remote/ids.js +12 -0
- package/src/remote/login.js +155 -0
- package/src/remote/remote-mcp.js +237 -0
- package/src/remote/remote-watch.js +291 -0
- package/src/remote/remote.js +313 -0
- package/src/remote/share-state.js +78 -0
- package/src/remote/share.js +369 -0
- package/src/remote/terminal.js +13 -0
- package/src/serve-state.js +64 -0
- package/src/server/app.js +824 -0
- package/src/server/devices.js +186 -0
- package/src/server/local-console.js +28 -0
- package/src/server/local-user.js +59 -0
- package/src/server/pairing-routes.js +159 -0
- package/src/server/share-routes.js +89 -0
- package/src/server/tailscale.js +97 -0
- package/src/sitemap.js +431 -0
- package/src/store.js +983 -0
- package/src/tokens.js +11 -0
- package/src/update/routes.js +245 -0
- package/src/watch.js +403 -0
- package/src/which.js +58 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
// The token API under /api/v1 (src/hosted/app.js), for the CLI, `watch`
|
|
2
|
+
// and MCP: Authorization: Bearer swp_... (cookies ignored). Pushes, board
|
|
3
|
+
// pages and files, comments, replies, agent statuses, the event stream
|
|
4
|
+
// (src/hosted/token-events.js), imports and agent approvals.
|
|
5
|
+
//
|
|
6
|
+
// mountTokenApi(deps) returns { v1, tokenAuth }; app.js mounts v1 (and MCP
|
|
7
|
+
// with tokenAuth) in its place in the route order.
|
|
8
|
+
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import express from 'express'
|
|
11
|
+
import { feedbackContext, feedbackItem, fileKind } from '../feedback.js'
|
|
12
|
+
import { listJourneys } from '../journeys.js'
|
|
13
|
+
import { joinUrl } from '../config.js'
|
|
14
|
+
import { MENTION_HANDLE_RE } from '../mentions.js'
|
|
15
|
+
import { captureFiles, listVersions, readManifest, resolveFile, variantsWithFiles } from '../store.js'
|
|
16
|
+
import { agentPolicy, expandScopes, trustedMember } from './agent-policy.js'
|
|
17
|
+
import { mountHostedApprovals } from './agent-approvals.js'
|
|
18
|
+
import { sendPng } from './board-context.js'
|
|
19
|
+
import { REPLY_AUTHOR, cleanAuthor, rowToComment } from './comments.js'
|
|
20
|
+
import { API_TOKEN_RE, sha256 } from './crypto.js'
|
|
21
|
+
import { transaction } from './db.js'
|
|
22
|
+
import { HttpError, fail } from './http.js'
|
|
23
|
+
import { mountImports } from './import.js'
|
|
24
|
+
import { createProjectStore } from './projects.js'
|
|
25
|
+
import { createPushStore } from './push.js'
|
|
26
|
+
import { LIMITS } from './ratelimit.js'
|
|
27
|
+
import { findApiToken, tenant } from './tenancy.js'
|
|
28
|
+
import { mountTokenEvents } from './token-events.js'
|
|
29
|
+
|
|
30
|
+
export function mountTokenApi({
|
|
31
|
+
db,
|
|
32
|
+
dataDir,
|
|
33
|
+
iso,
|
|
34
|
+
now,
|
|
35
|
+
publicOrigin,
|
|
36
|
+
rateLimiter,
|
|
37
|
+
quotas,
|
|
38
|
+
workspaceQuota,
|
|
39
|
+
cleaner,
|
|
40
|
+
q,
|
|
41
|
+
events,
|
|
42
|
+
streams,
|
|
43
|
+
usercontent,
|
|
44
|
+
handle,
|
|
45
|
+
auditOf,
|
|
46
|
+
clientIp,
|
|
47
|
+
policyFor,
|
|
48
|
+
boardContext,
|
|
49
|
+
dispatchView,
|
|
50
|
+
needFresh,
|
|
51
|
+
approvalApi,
|
|
52
|
+
}) {
|
|
53
|
+
// ---- Token API ----
|
|
54
|
+
const pushOptions = (t) => {
|
|
55
|
+
const quota = quotas.of(t.workspaceId)
|
|
56
|
+
return { db, dataDir, workspaceQuota: workspaceQuota ?? quota.storageBytes, boardsPerProject: quota.boardsPerProject }
|
|
57
|
+
}
|
|
58
|
+
const v1 = express.Router()
|
|
59
|
+
v1.use((req, res, next) => {
|
|
60
|
+
res.setHeader('Cache-Control', 'no-store')
|
|
61
|
+
next()
|
|
62
|
+
})
|
|
63
|
+
const tokenAuth = (req, res, next) => {
|
|
64
|
+
const header = String(req.headers.authorization || '')
|
|
65
|
+
const match = /^Bearer (\S+)$/.exec(header)
|
|
66
|
+
const token = match && API_TOKEN_RE.test(match[1]) ? match[1] : null
|
|
67
|
+
const row = token ? findApiToken(db, sha256(token), iso()) : null
|
|
68
|
+
if (!row) {
|
|
69
|
+
const result = rateLimiter.hit('tokenBadAuth', clientIp(req), LIMITS.tokenBadAuth)
|
|
70
|
+
if (!result.ok) {
|
|
71
|
+
res.setHeader('Retry-After', String(result.retryAfter))
|
|
72
|
+
return fail(res, 429, 'rate_limited', 'Too many requests with a bad token.')
|
|
73
|
+
}
|
|
74
|
+
res.setHeader('WWW-Authenticate', 'Bearer')
|
|
75
|
+
return fail(
|
|
76
|
+
res,
|
|
77
|
+
401,
|
|
78
|
+
'bad_token',
|
|
79
|
+
'This token was revoked or has expired, its maker is no longer an admin of the workspace, or it is wrong. Send a live project token: Authorization: Bearer swp_...',
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
const hits = rateLimiter.hit('token', row.id, LIMITS.token)
|
|
83
|
+
if (!hits.ok) {
|
|
84
|
+
res.setHeader('Retry-After', String(hits.retryAfter))
|
|
85
|
+
return fail(res, 429, 'rate_limited', 'Too many requests for this token.')
|
|
86
|
+
}
|
|
87
|
+
req.token = { id: row.id, name: row.name, scopes: expandScopes(JSON.parse(row.scopes)) }
|
|
88
|
+
req.tokenHash = sha256(token)
|
|
89
|
+
req.authTokenHash = row.token_hash
|
|
90
|
+
req.t = tenant(db, row.workspace_id)
|
|
91
|
+
req.project = createProjectStore(req.t, { db, dataDir }).get(row.project_id)
|
|
92
|
+
if (!req.project) return fail(res, 401, 'bad_token', 'This token has no project.')
|
|
93
|
+
if (!row.last_used_at || now().getTime() - Date.parse(row.last_used_at) > 60_000) {
|
|
94
|
+
req.t.run('UPDATE api_tokens SET last_used_at = :at WHERE workspace_id = :ws AND id = :id', { at: iso(), id: row.id })
|
|
95
|
+
}
|
|
96
|
+
req.workspaceName = row.workspace_name
|
|
97
|
+
next()
|
|
98
|
+
}
|
|
99
|
+
v1.use(tokenAuth)
|
|
100
|
+
const scope = (name) => (req, res, next) =>
|
|
101
|
+
req.token.scopes.includes(name) ? next() : fail(res, 403, 'missing_scope', `This token does not have the "${name}" scope.`)
|
|
102
|
+
const v1Board = (fn) =>
|
|
103
|
+
handle(async (req, res) => {
|
|
104
|
+
req.ctx = boardContext(req.t, req.project, req.params.bid)
|
|
105
|
+
await fn(req, res, req.ctx)
|
|
106
|
+
})
|
|
107
|
+
v1.use('/boards/:bid/import', express.json({ limit: '2mb' }))
|
|
108
|
+
mountImports(v1, { db, boardContext, handle, now })
|
|
109
|
+
// Guest comments stay away from agents unless asked for, on every route
|
|
110
|
+
// and not only in lists: reading one, replying, statuses, resolving and
|
|
111
|
+
// reopening need the same opt-in (?guests=1) as listing them.
|
|
112
|
+
const v1Comment = (fn) =>
|
|
113
|
+
handle(async (req, res) => {
|
|
114
|
+
req.ctx = boardContext(req.t, req.project, req.params.bid)
|
|
115
|
+
const row = req.ctx.comments.get(req.params.cid)
|
|
116
|
+
if (
|
|
117
|
+
req.token.scopes.includes('claim') &&
|
|
118
|
+
!policyFor(req.t).state(req.project.id, req.ctx.board.id, { ...row, status: 'open' }).dispatchable
|
|
119
|
+
)
|
|
120
|
+
throw new HttpError(404, 'unknown_comment', 'No dispatched comment at that id.')
|
|
121
|
+
if (row.author_share_id && req.query.guests !== '1') {
|
|
122
|
+
throw new HttpError(
|
|
123
|
+
403,
|
|
124
|
+
'guest_comment',
|
|
125
|
+
'This comment is from a share link guest (untrusted). Ask for guest comments to act on it (--include-guests, includeGuests or ?guests=1).',
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
await fn(req, res, req.ctx)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
// What a claim token reads of a dispatched comment's replies: a trusted
|
|
132
|
+
// member vouched for the comment, not for what others added after, so only
|
|
133
|
+
// replies by members the agent policy trusts and the token's own replies.
|
|
134
|
+
// Guests, other tokens, untrusted members and imported replies never reach it.
|
|
135
|
+
function agentReplies(req, ctx, replies = []) {
|
|
136
|
+
const policy = agentPolicy(req.t, req.project.id, ctx.board.id)
|
|
137
|
+
const handleFor = (user) => q.get('SELECT handle FROM users WHERE id = :id', { id: user })?.handle || null
|
|
138
|
+
return replies.filter((reply) => {
|
|
139
|
+
const by = reply[REPLY_AUTHOR] || {}
|
|
140
|
+
if (reply.agent) return Boolean(by.token) && by.token === req.token.id
|
|
141
|
+
if (reply.guest || reply.imported) return false
|
|
142
|
+
const user = by.user || (reply.handle ? q.get('SELECT id FROM users WHERE handle = :handle', { handle: reply.handle })?.id : null)
|
|
143
|
+
return trustedMember({ ...req.t, handleFor }, policy, user)
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// The replies a token reads. A claim token: agentReplies above. Any other
|
|
148
|
+
// token: everything but share link guests' replies, which (like guest
|
|
149
|
+
// comments) come only when asked for (?guests=1), on every route.
|
|
150
|
+
function tokenReplies(req, ctx, replies = []) {
|
|
151
|
+
if (req.token.scopes.includes('claim')) return agentReplies(req, ctx, replies)
|
|
152
|
+
return req.query.guests === '1' ? replies : replies.filter((reply) => !reply.guest)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Feedback items with download URLs instead of server paths.
|
|
156
|
+
function remoteItem(req, ctx, context, comment) {
|
|
157
|
+
const item = {
|
|
158
|
+
...feedbackItem(context, { ...comment, replies: tokenReplies(req, ctx, comment.replies) }),
|
|
159
|
+
dispatchable: Boolean(comment.dispatchable),
|
|
160
|
+
needsRunApproval: Boolean(comment.needsRunApproval),
|
|
161
|
+
}
|
|
162
|
+
// The capture a comment was made on can be an archived version (versions/).
|
|
163
|
+
const url = (file) =>
|
|
164
|
+
file ? `${publicOrigin}/api/v1/boards/${ctx.board.id}/files/${fileKind(item, file)}/${path.basename(file)}` : null
|
|
165
|
+
const files = item.files
|
|
166
|
+
item.files = {
|
|
167
|
+
screenshot: url(files.screenshot),
|
|
168
|
+
liveHtml: url(files.liveHtml),
|
|
169
|
+
version: files.version,
|
|
170
|
+
latestScreenshot: url(files.latestScreenshot),
|
|
171
|
+
latestLiveHtml: url(files.latestLiveHtml),
|
|
172
|
+
latestVersion: files.latestVersion,
|
|
173
|
+
}
|
|
174
|
+
return item
|
|
175
|
+
}
|
|
176
|
+
// Guest comments (from share links) are untrusted: a token sees them only
|
|
177
|
+
// when it asks (`guests=1`), so an agent never picks one up by default.
|
|
178
|
+
function remoteReport(req, ctx, { status = 'open', pageId = null, mention = null, guests = false } = {}) {
|
|
179
|
+
const context = feedbackContext(ctx.config, ctx.board)
|
|
180
|
+
let guestsHidden = 0
|
|
181
|
+
const comments = ctx.comments
|
|
182
|
+
.list({ status: status === 'all' ? null : status, mention })
|
|
183
|
+
.map((comment) => dispatchView(req.t, req.project.id, ctx.board.id, comment))
|
|
184
|
+
.filter((comment) => !req.token.scopes.includes('claim') || comment.dispatchable)
|
|
185
|
+
.filter((comment) => !pageId || comment.pageId === pageId || context.variants.get(comment.pageId)?.pageId === pageId)
|
|
186
|
+
.filter((comment) => {
|
|
187
|
+
if (guests || !comment.authorGuest) return true
|
|
188
|
+
guestsHidden += 1
|
|
189
|
+
return false
|
|
190
|
+
})
|
|
191
|
+
.sort((a, b) => a.id - b.id)
|
|
192
|
+
.map((comment) => remoteItem(req, ctx, context, comment))
|
|
193
|
+
return {
|
|
194
|
+
board: {
|
|
195
|
+
id: ctx.board.id,
|
|
196
|
+
title: ctx.board.title,
|
|
197
|
+
baseUrl: ctx.board.baseUrl,
|
|
198
|
+
project: req.project.id,
|
|
199
|
+
url: `${publicOrigin}/w/${req.t.workspaceId}/p/${req.project.id}/`,
|
|
200
|
+
},
|
|
201
|
+
status,
|
|
202
|
+
guestsHidden,
|
|
203
|
+
count: comments.length,
|
|
204
|
+
comments,
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// A token writes as itself: its name (set by an admin) with an agent flag.
|
|
208
|
+
// Any `author` in the request is ignored, so a token cannot pose as a person.
|
|
209
|
+
const tokenAuthor = (req) => ({ name: cleanAuthor(req.token.name, 'agent'), agent: true })
|
|
210
|
+
|
|
211
|
+
mountTokenEvents(v1, { db, iso, events, streams, scope })
|
|
212
|
+
|
|
213
|
+
v1.get(
|
|
214
|
+
'/project',
|
|
215
|
+
handle(async (req, res) => {
|
|
216
|
+
const boards = createProjectStore(req.t, { db, dataDir }).boards(req.project.id)
|
|
217
|
+
res.json({
|
|
218
|
+
workspace: { id: req.t.workspaceId, name: req.workspaceName },
|
|
219
|
+
project: {
|
|
220
|
+
id: req.project.id,
|
|
221
|
+
name: req.project.name,
|
|
222
|
+
slug: req.project.slug,
|
|
223
|
+
url: `${publicOrigin}/w/${req.t.workspaceId}/p/${req.project.id}/`,
|
|
224
|
+
},
|
|
225
|
+
token: { name: req.token.name, scopes: req.token.scopes },
|
|
226
|
+
boards: boards.map((row) => ({ id: row.board_id, pushedAt: row.pushed_at })),
|
|
227
|
+
})
|
|
228
|
+
}),
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
const pushJson = express.json({ limit: '4mb' })
|
|
232
|
+
// Files a token is uploading right now: the CLI sends one at a time, so a
|
|
233
|
+
// handful in flight is plenty and one token cannot fill the cleaner queue.
|
|
234
|
+
const uploading = new Map()
|
|
235
|
+
const UPLOADS_PER_TOKEN = 4
|
|
236
|
+
const oneUpload = (req, res, next) => {
|
|
237
|
+
const id = req.token.id
|
|
238
|
+
const count = uploading.get(id) || 0
|
|
239
|
+
if (count >= UPLOADS_PER_TOKEN) return fail(res, 429, 'rate_limited', 'Too many uploads at once for this token.')
|
|
240
|
+
uploading.set(id, count + 1)
|
|
241
|
+
res.once('close', () => {
|
|
242
|
+
const left = (uploading.get(id) || 1) - 1
|
|
243
|
+
if (left > 0) uploading.set(id, left)
|
|
244
|
+
else uploading.delete(id)
|
|
245
|
+
})
|
|
246
|
+
next()
|
|
247
|
+
}
|
|
248
|
+
v1.post(
|
|
249
|
+
'/pushes',
|
|
250
|
+
scope('push'),
|
|
251
|
+
(req, res, next) => {
|
|
252
|
+
const result = rateLimiter.hit('push', req.token.id, { ...LIMITS.push, limit: quotas.of(req.t.workspaceId).pushesPerHour })
|
|
253
|
+
if (result.ok) return next()
|
|
254
|
+
res.setHeader('Retry-After', String(result.retryAfter))
|
|
255
|
+
fail(res, 429, 'rate_limited', 'Too many requests. Wait a moment and try again.')
|
|
256
|
+
},
|
|
257
|
+
pushJson,
|
|
258
|
+
handle(async (req, res) => {
|
|
259
|
+
const pushes = createPushStore(req.t, pushOptions(req.t))
|
|
260
|
+
res.status(201).json(pushes.start({ projectId: req.project.id, tokenId: req.token.id, raw: req.body, now: now() }))
|
|
261
|
+
}),
|
|
262
|
+
)
|
|
263
|
+
v1.put(
|
|
264
|
+
'/pushes/:pushId/files/:area/:name',
|
|
265
|
+
scope('push'),
|
|
266
|
+
oneUpload,
|
|
267
|
+
express.raw({ type: () => true, limit: '11mb' }),
|
|
268
|
+
handle(async (req, res) => {
|
|
269
|
+
const pushes = createPushStore(req.t, pushOptions(req.t))
|
|
270
|
+
res.json(
|
|
271
|
+
await pushes.receive({
|
|
272
|
+
pushId: req.params.pushId,
|
|
273
|
+
projectId: req.project.id,
|
|
274
|
+
area: req.params.area,
|
|
275
|
+
name: req.params.name,
|
|
276
|
+
body: req.body,
|
|
277
|
+
cleaner,
|
|
278
|
+
now,
|
|
279
|
+
}),
|
|
280
|
+
)
|
|
281
|
+
}),
|
|
282
|
+
)
|
|
283
|
+
v1.post(
|
|
284
|
+
'/pushes/:pushId/commit',
|
|
285
|
+
scope('push'),
|
|
286
|
+
pushJson,
|
|
287
|
+
handle(async (req, res) => {
|
|
288
|
+
const pushes = createPushStore(req.t, pushOptions(req.t))
|
|
289
|
+
const result = pushes.commit({
|
|
290
|
+
pushId: req.params.pushId,
|
|
291
|
+
projectId: req.project.id,
|
|
292
|
+
manifest: req.body?.manifest,
|
|
293
|
+
variants: req.body?.variants,
|
|
294
|
+
checks: req.body?.checks,
|
|
295
|
+
now: now(),
|
|
296
|
+
})
|
|
297
|
+
auditOf(req, 'push_committed', `${req.project.id}/${result.board}`, { files: result.files })
|
|
298
|
+
res.json({ ...result, url: `${publicOrigin}/w/${req.t.workspaceId}/p/${req.project.id}/?board=${result.board}` })
|
|
299
|
+
}),
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
v1.get(
|
|
303
|
+
'/boards/:bid/pages',
|
|
304
|
+
scope('read'),
|
|
305
|
+
v1Board((req, res, ctx) => {
|
|
306
|
+
const files = captureFiles(ctx.config, ctx.board)
|
|
307
|
+
const manifest = readManifest(ctx.config, ctx.board.id)
|
|
308
|
+
res.json({
|
|
309
|
+
board: { id: ctx.board.id, title: ctx.board.title, baseUrl: ctx.board.baseUrl },
|
|
310
|
+
capturedAt: manifest?.capturedAt || null,
|
|
311
|
+
pushedAt: ctx.row.pushed_at,
|
|
312
|
+
pages: ctx.board.pages.map((page) => ({
|
|
313
|
+
id: page.id,
|
|
314
|
+
path: page.path,
|
|
315
|
+
title: page.title,
|
|
316
|
+
group: page.group,
|
|
317
|
+
note: page.note || null,
|
|
318
|
+
url: joinUrl(ctx.board.publicUrl || ctx.board.baseUrl, page.path),
|
|
319
|
+
viewports: page.viewports,
|
|
320
|
+
captured: Object.keys(files[page.id] || {}),
|
|
321
|
+
})),
|
|
322
|
+
journeys: listJourneys(ctx.config, ctx.board),
|
|
323
|
+
variants: variantsWithFiles(ctx.config, ctx.board).map(({ files: _files, ...variant }) => variant),
|
|
324
|
+
versions: Object.keys(listVersions(ctx.config, ctx.board.id)).length,
|
|
325
|
+
})
|
|
326
|
+
}),
|
|
327
|
+
)
|
|
328
|
+
v1.get(
|
|
329
|
+
'/boards/:bid/comments',
|
|
330
|
+
scope('read'),
|
|
331
|
+
v1Board((req, res, ctx) => {
|
|
332
|
+
const status = ['open', 'resolved', 'all'].includes(req.query.status) ? req.query.status : 'open'
|
|
333
|
+
const mention =
|
|
334
|
+
typeof req.query.mention === 'string' && MENTION_HANDLE_RE.test(req.query.mention.replace(/^@/, '').toLowerCase())
|
|
335
|
+
? req.query.mention.replace(/^@/, '').toLowerCase()
|
|
336
|
+
: null
|
|
337
|
+
const pageId =
|
|
338
|
+
typeof req.query.page === 'string' && ctx.board.pages.some((page) => page.id === req.query.page) ? req.query.page : null
|
|
339
|
+
if (req.query.page && !pageId) return fail(res, 404, 'unknown_page', 'That page is not on this board.')
|
|
340
|
+
res.json(remoteReport(req, ctx, { status, pageId, mention, guests: req.query.guests === '1' }))
|
|
341
|
+
}),
|
|
342
|
+
)
|
|
343
|
+
v1.get(
|
|
344
|
+
'/boards/:bid/comments/:cid',
|
|
345
|
+
scope('read'),
|
|
346
|
+
v1Comment((req, res, ctx) => {
|
|
347
|
+
const row = ctx.comments.get(req.params.cid)
|
|
348
|
+
res.json({
|
|
349
|
+
board: ctx.board.id,
|
|
350
|
+
comment: remoteItem(
|
|
351
|
+
req,
|
|
352
|
+
ctx,
|
|
353
|
+
feedbackContext(ctx.config, ctx.board),
|
|
354
|
+
dispatchView(req.t, req.project.id, ctx.board.id, rowToComment(row)),
|
|
355
|
+
),
|
|
356
|
+
})
|
|
357
|
+
}),
|
|
358
|
+
)
|
|
359
|
+
v1.get(
|
|
360
|
+
'/boards/:bid/files/:kind/:file',
|
|
361
|
+
scope('read'),
|
|
362
|
+
v1Board((req, res, ctx) => {
|
|
363
|
+
const { kind, file: name } = req.params
|
|
364
|
+
if (!['captures', 'variants', 'versions'].includes(kind)) return fail(res, 404, 'not_found', 'No such file.')
|
|
365
|
+
const file = resolveFile(ctx.config, ctx.board.id, kind, name)
|
|
366
|
+
if (!file) return fail(res, 404, 'not_found', 'No such file.')
|
|
367
|
+
if (name.endsWith('.png')) return sendPng(res, file, false)
|
|
368
|
+
if (name.endsWith('.elements.json')) return res.type('json').sendFile(file, { dotfiles: 'allow' })
|
|
369
|
+
res.setHeader('Content-Security-Policy', usercontent.csp)
|
|
370
|
+
res.setHeader('Content-Disposition', 'attachment')
|
|
371
|
+
res.type('html').sendFile(file, { dotfiles: 'allow' })
|
|
372
|
+
}),
|
|
373
|
+
)
|
|
374
|
+
const v1Json = express.json({ limit: '64kb' })
|
|
375
|
+
const v1Item = (req, ctx, comment) => remoteItem(req, ctx, feedbackContext(ctx.config, ctx.board), comment)
|
|
376
|
+
v1.post(
|
|
377
|
+
'/boards/:bid/comments/:cid/replies',
|
|
378
|
+
scope('reply'),
|
|
379
|
+
v1Json,
|
|
380
|
+
v1Comment((req, res, ctx) => {
|
|
381
|
+
const { comment, reply } = transaction(db, () =>
|
|
382
|
+
ctx.comments.reply(req.params.cid, { body: req.body?.body }, { ...tokenAuthor(req), tokenId: req.token.id }, now()),
|
|
383
|
+
)
|
|
384
|
+
res.status(201).json({ comment: v1Item(req, ctx, comment), reply })
|
|
385
|
+
}),
|
|
386
|
+
)
|
|
387
|
+
v1.post(
|
|
388
|
+
'/boards/:bid/comments/:cid/agent-status',
|
|
389
|
+
scope('status'),
|
|
390
|
+
v1Json,
|
|
391
|
+
v1Comment((req, res, ctx) => {
|
|
392
|
+
const { agentStatus = null, onlyFrom = null } = req.body || {}
|
|
393
|
+
const result = transaction(db, () => {
|
|
394
|
+
const row = ctx.comments.get(req.params.cid)
|
|
395
|
+
const taking = ['queued', 'working'].includes(agentStatus) && !['queued', 'working'].includes(row.agent_status)
|
|
396
|
+
if (taking && !req.token.scopes.includes('claim')) throw new HttpError(403, 'missing_scope', 'Taking work needs the claim scope.')
|
|
397
|
+
// Only open comments are claimed (and counted against the budget).
|
|
398
|
+
if (taking && row.status !== 'open') throw new HttpError(409, 'not_open', 'Only an open comment can be taken.')
|
|
399
|
+
if (taking && row.status === 'open' && (!onlyFrom || onlyFrom.includes(row.agent_status || null)))
|
|
400
|
+
policyFor(req.t).claim(req.project.id, ctx.board.id, row, req.token.id)
|
|
401
|
+
if (['queued', 'working'].includes(row.agent_status)) {
|
|
402
|
+
const claim = req.t.get(
|
|
403
|
+
'SELECT token_id FROM agent_claims WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND comment_number = :number AND ended_at IS NULL ORDER BY created_at DESC LIMIT 1',
|
|
404
|
+
{ project: req.project.id, board: ctx.board.id, number: row.number },
|
|
405
|
+
)
|
|
406
|
+
if (claim && claim.token_id !== req.token.id) throw new HttpError(403, 'claim_owner', 'Another agent holds this claim.')
|
|
407
|
+
}
|
|
408
|
+
const result = ctx.comments.setAgentStatus(req.params.cid, agentStatus, { onlyFrom }, now())
|
|
409
|
+
if (result.changed && !['queued', 'working'].includes(agentStatus))
|
|
410
|
+
req.t.run(
|
|
411
|
+
'UPDATE agent_claims SET ended_at = :at WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND comment_number = :number AND ended_at IS NULL',
|
|
412
|
+
{ project: req.project.id, board: ctx.board.id, number: row.number, at: iso() },
|
|
413
|
+
)
|
|
414
|
+
return result
|
|
415
|
+
})
|
|
416
|
+
res.json({ comment: v1Item(req, ctx, result.comment), changed: result.changed })
|
|
417
|
+
}),
|
|
418
|
+
)
|
|
419
|
+
v1.post(
|
|
420
|
+
'/boards/:bid/comments/:cid/resolve',
|
|
421
|
+
scope('resolve'),
|
|
422
|
+
v1Json,
|
|
423
|
+
v1Comment((req, res, ctx) => {
|
|
424
|
+
const { note, fixRef, agentStatus } = req.body || {}
|
|
425
|
+
const comment = transaction(db, () =>
|
|
426
|
+
ctx.comments.update(
|
|
427
|
+
req.params.cid,
|
|
428
|
+
{
|
|
429
|
+
status: 'resolved',
|
|
430
|
+
resolution: typeof note === 'string' ? note : undefined,
|
|
431
|
+
...(fixRef !== undefined ? { fixRef } : {}),
|
|
432
|
+
...(agentStatus !== undefined ? { agentStatus } : {}),
|
|
433
|
+
},
|
|
434
|
+
now(),
|
|
435
|
+
),
|
|
436
|
+
)
|
|
437
|
+
auditOf(req, 'comment_resolved', `${ctx.board.id}#${comment.id}`, { project: req.project.id })
|
|
438
|
+
res.json({ comment: v1Item(req, ctx, comment) })
|
|
439
|
+
}),
|
|
440
|
+
)
|
|
441
|
+
v1.post(
|
|
442
|
+
'/boards/:bid/comments/:cid/reopen',
|
|
443
|
+
scope('resolve'),
|
|
444
|
+
v1Json,
|
|
445
|
+
v1Comment((req, res, ctx) => {
|
|
446
|
+
const comment = transaction(db, () => ctx.comments.update(req.params.cid, { status: 'open' }, now()))
|
|
447
|
+
res.json({ comment: v1Item(req, ctx, comment) })
|
|
448
|
+
}),
|
|
449
|
+
)
|
|
450
|
+
v1.use(v1Json)
|
|
451
|
+
mountHostedApprovals({ v1, boardApi: approvalApi, handle, scope, db, transaction, policyFor, boardContext, needFresh, auditOf, now })
|
|
452
|
+
v1.use((req, res) => fail(res, 404, 'not_found', 'No such API route.'))
|
|
453
|
+
|
|
454
|
+
return { v1, tokenAuth }
|
|
455
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// GET /api/v1/events (src/hosted/token-api.js): a token's server-sent
|
|
2
|
+
// event stream of change events for its project. Each stream is a check in
|
|
3
|
+
// `streams`, run after every write (src/hosted/events.js) and on the
|
|
4
|
+
// keepalive, that ends it once the token stops working.
|
|
5
|
+
|
|
6
|
+
import { fail } from './http.js'
|
|
7
|
+
import { findApiToken } from './tenancy.js'
|
|
8
|
+
|
|
9
|
+
const STREAMS_PER_WORKSPACE = 100
|
|
10
|
+
|
|
11
|
+
export function mountTokenEvents(v1, { db, iso, events, streams, scope }) {
|
|
12
|
+
v1.get('/events', scope('read'), (req, res) => {
|
|
13
|
+
if (streams.size >= 1000 || [...streams].filter((check) => check.tokenId === req.token.id).length >= 5)
|
|
14
|
+
return fail(res, 429, 'stream_budget', 'Too many event streams for this token.')
|
|
15
|
+
// One workspace cannot hold every stream on the server.
|
|
16
|
+
if ([...streams].filter((check) => check.workspaceId === req.t.workspaceId).length >= STREAMS_PER_WORKSPACE)
|
|
17
|
+
return fail(res, 429, 'stream_budget', 'Too many event streams for this workspace.')
|
|
18
|
+
res.setHeader('Content-Type', 'text/event-stream')
|
|
19
|
+
res.setHeader('X-Accel-Buffering', 'no')
|
|
20
|
+
res.flushHeaders()
|
|
21
|
+
let closed = false
|
|
22
|
+
let unsubscribe = () => {}
|
|
23
|
+
let timer
|
|
24
|
+
const close = () => {
|
|
25
|
+
if (closed) return
|
|
26
|
+
closed = true
|
|
27
|
+
clearInterval(timer)
|
|
28
|
+
unsubscribe()
|
|
29
|
+
streams.delete(check)
|
|
30
|
+
res.end()
|
|
31
|
+
}
|
|
32
|
+
const check = (force = false) => {
|
|
33
|
+
if (force || !findApiToken(db, req.tokenHash, iso())) close()
|
|
34
|
+
}
|
|
35
|
+
check.tokenId = req.token.id
|
|
36
|
+
check.workspaceId = req.t.workspaceId
|
|
37
|
+
streams.add(check)
|
|
38
|
+
unsubscribe = events.subscribe((event) => {
|
|
39
|
+
if (event.type === 'close') return close()
|
|
40
|
+
// Other projects' events never reach this stream, so they skip the token
|
|
41
|
+
// lookup; revocation is still caught here, by the keepalive and after
|
|
42
|
+
// every mutating request.
|
|
43
|
+
if (closed || event.workspace !== req.t.workspaceId || event.project !== req.project.id) return
|
|
44
|
+
check()
|
|
45
|
+
if (!closed) {
|
|
46
|
+
if (
|
|
47
|
+
!res.write(`id: ${event.sequence}\nevent: change\ndata: ${JSON.stringify({ type: event.type, board: event.board || null })}\n\n`)
|
|
48
|
+
)
|
|
49
|
+
close()
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
timer = setInterval(() => {
|
|
53
|
+
check()
|
|
54
|
+
if (!closed) res.write(': keepalive\n\n')
|
|
55
|
+
}, 15000)
|
|
56
|
+
timer.unref()
|
|
57
|
+
res.write(': connected\n\n')
|
|
58
|
+
res.once('close', close)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Project API tokens (src/hosted/app.js): the scopes a token can have and
|
|
2
|
+
// making one, from the console or an approved `shapewall login`.
|
|
3
|
+
|
|
4
|
+
import { listTokens } from './admin.js'
|
|
5
|
+
import { expandScopes } from './agent-policy.js'
|
|
6
|
+
import { newApiToken, newId } from './crypto.js'
|
|
7
|
+
import { HttpError, cleanName } from './http.js'
|
|
8
|
+
|
|
9
|
+
export const TOKEN_SCOPES = ['read', 'reply', 'status', 'claim', 'resolve', 'push', 'import']
|
|
10
|
+
const DAY = 86_400_000
|
|
11
|
+
|
|
12
|
+
export function createApiTokens({ iso, auditOf }) {
|
|
13
|
+
// A new project token for req.t / req.project, made by req.user. Returns
|
|
14
|
+
// the token (shown once) and its listing. `via` marks how it was made
|
|
15
|
+
// ("login" for `shapewall login`) in the audit log.
|
|
16
|
+
function createApiToken(req, body, { via = null } = {}) {
|
|
17
|
+
const name = cleanName(body.name, 60)
|
|
18
|
+
if (!name) throw new HttpError(400, 'invalid_name', 'Token name: 1 to 60 characters.')
|
|
19
|
+
const scopes = Array.isArray(body.scopes) ? expandScopes(body.scopes) : ['read']
|
|
20
|
+
if (!scopes.length || scopes.some((scope) => !TOKEN_SCOPES.includes(scope))) {
|
|
21
|
+
throw new HttpError(400, 'invalid_scopes', `Scopes: ${TOKEN_SCOPES.join(', ')}.`)
|
|
22
|
+
}
|
|
23
|
+
if (scopes.includes('import') && via !== 'import')
|
|
24
|
+
throw new HttpError(403, 'import_login_required', 'Import is a one-time grant from a moving-project device login.')
|
|
25
|
+
const days = body.expiresInDays
|
|
26
|
+
if (days !== undefined && days !== null && (!Number.isInteger(days) || days < 1 || days > 365)) {
|
|
27
|
+
throw new HttpError(400, 'invalid_expiry', 'expiresInDays: 1 to 365, or leave it out.')
|
|
28
|
+
}
|
|
29
|
+
const count = req.t.get(
|
|
30
|
+
'SELECT COUNT(*) AS n FROM api_tokens WHERE workspace_id = :ws AND project_id = :project AND revoked_at IS NULL',
|
|
31
|
+
{ project: req.project.id },
|
|
32
|
+
).n
|
|
33
|
+
if (count >= 50) throw new HttpError(409, 'too_many_tokens', 'Revoke an old token first (50 per project).')
|
|
34
|
+
const { token, hash, prefix } = newApiToken()
|
|
35
|
+
const id = newId('tk')
|
|
36
|
+
req.t.run(
|
|
37
|
+
`INSERT INTO api_tokens (workspace_id, id, project_id, name, token_hash, prefix, scopes, created_by, created_at, expires_at)
|
|
38
|
+
VALUES (:ws, :id, :project, :name, :hash, :prefix, :scopes, :user, :at, :expires)`,
|
|
39
|
+
{
|
|
40
|
+
id,
|
|
41
|
+
project: req.project.id,
|
|
42
|
+
name,
|
|
43
|
+
hash,
|
|
44
|
+
prefix,
|
|
45
|
+
scopes: JSON.stringify(scopes.sort()),
|
|
46
|
+
user: req.user.id,
|
|
47
|
+
at: iso(),
|
|
48
|
+
expires: days ? iso(days * DAY) : null,
|
|
49
|
+
},
|
|
50
|
+
)
|
|
51
|
+
auditOf(req, 'token_created', id, { project: req.project.id, scopes, ...(days ? { days } : {}), ...(via ? { via } : {}) })
|
|
52
|
+
const info = listTokens(req.t, { projectId: req.project.id, now: iso() }).find((item) => item.id === id)
|
|
53
|
+
return { token, info }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return createApiToken
|
|
57
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Cloudflare Turnstile hooks: a bot check on open sign-up and before a share
|
|
2
|
+
// link guest can comment. Off unless the operator sets both keys
|
|
3
|
+
// (SHAPEWALL_TURNSTILE_SITE_KEY and SHAPEWALL_TURNSTILE_SECRET); a server in
|
|
4
|
+
// invite mode on a tailnet needs none of it.
|
|
5
|
+
//
|
|
6
|
+
// The only URL this calls is Cloudflare's fixed siteverify endpoint, never
|
|
7
|
+
// one a user gives, so it adds no SSRF surface. It fails closed: a timeout or
|
|
8
|
+
// an error from Cloudflare refuses the sign-up or the guest, it never lets
|
|
9
|
+
// one through.
|
|
10
|
+
|
|
11
|
+
export const SITEVERIFY_URL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'
|
|
12
|
+
const TOKEN_MAX = 2048
|
|
13
|
+
|
|
14
|
+
export function createTurnstile({ siteKey, secret, fetchImpl = globalThis.fetch, timeoutMs = 5000, log = () => {} }) {
|
|
15
|
+
if (!siteKey || !secret) return null
|
|
16
|
+
// hostnames: the hosts this server's pages are on; a token solved on any
|
|
17
|
+
// other site (Cloudflare reports where) is refused.
|
|
18
|
+
async function verify(token, ip, { hostnames = null } = {}) {
|
|
19
|
+
if (typeof token !== 'string' || !token || token.length > TOKEN_MAX) return false
|
|
20
|
+
const body = new URLSearchParams({ secret, response: token })
|
|
21
|
+
if (ip && ip !== 'unknown') body.set('remoteip', ip)
|
|
22
|
+
try {
|
|
23
|
+
const response = await fetchImpl(SITEVERIFY_URL, {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
body,
|
|
26
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
27
|
+
redirect: 'error',
|
|
28
|
+
})
|
|
29
|
+
if (!response.ok) return false
|
|
30
|
+
const data = await response.json()
|
|
31
|
+
if (data?.success !== true) return false
|
|
32
|
+
if (hostnames && !(typeof data.hostname === 'string' && hostnames.includes(data.hostname.toLowerCase()))) {
|
|
33
|
+
log('turnstile: refused a token solved on another host')
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
return true
|
|
37
|
+
} catch (error) {
|
|
38
|
+
log(`turnstile: ${error.message}`)
|
|
39
|
+
return false
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { siteKey: String(siteKey), verify }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const turnstileFromEnv = (env = process.env, options = {}) =>
|
|
46
|
+
createTurnstile({ siteKey: env.SHAPEWALL_TURNSTILE_SITE_KEY, secret: env.SHAPEWALL_TURNSTILE_SECRET, ...options })
|
|
47
|
+
|
|
48
|
+
// Extra CSP sources the console needs to show the widget.
|
|
49
|
+
export const TURNSTILE_ORIGIN = 'https://challenges.cloudflare.com'
|