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,401 @@
|
|
|
1
|
+
// The account console API under /api (src/hosted/app.js): invite links,
|
|
2
|
+
// workspaces, members, invites, projects, project tokens and agent policy.
|
|
3
|
+
// `shapewall login` approvals are mounted in their place in the route order
|
|
4
|
+
// (src/hosted/device-routes.js).
|
|
5
|
+
|
|
6
|
+
import { listTokens } from './admin.js'
|
|
7
|
+
import { AuthError } from './auth.js'
|
|
8
|
+
import { agentPolicy, cleanPolicy } from './agent-policy.js'
|
|
9
|
+
import { newId, randomToken, sha256 } from './crypto.js'
|
|
10
|
+
import { transaction } from './db.js'
|
|
11
|
+
import { mountDeviceApprovals } from './device-routes.js'
|
|
12
|
+
import { HttpError, fail, cleanInviteNote, cleanWorkspaceName } from './http.js'
|
|
13
|
+
import { cleanEmail } from './mail.js'
|
|
14
|
+
import {
|
|
15
|
+
NEEDS,
|
|
16
|
+
canInvite,
|
|
17
|
+
canManage,
|
|
18
|
+
canRemoveMember,
|
|
19
|
+
inviteNeedsFresh,
|
|
20
|
+
leavesNoOwner,
|
|
21
|
+
removalNeedsFresh,
|
|
22
|
+
roleChangeNeedsFresh,
|
|
23
|
+
} from './permissions.js'
|
|
24
|
+
import { projectPublic } from './public.js'
|
|
25
|
+
import { LIMITS } from './ratelimit.js'
|
|
26
|
+
import { SHARE_DAYS } from './shares.js'
|
|
27
|
+
import { ROLES, findInviteByHash, inviteUsable, roleAtLeast, roleIn, tenant, workspacesForUser } from './tenancy.js'
|
|
28
|
+
|
|
29
|
+
export const INVITE_DAYS = [1, 3, 7, 14, 30]
|
|
30
|
+
export const INVITE_DAYS_DEFAULT = 7
|
|
31
|
+
export const INVITES_PENDING_MAX = 100
|
|
32
|
+
const DAY = 86_400_000
|
|
33
|
+
|
|
34
|
+
// Invite links: preview needs no session, accepting one does.
|
|
35
|
+
export function mountInviteLinks(consoleApi, deps) {
|
|
36
|
+
const { db, iso, limit, handle, sessionOf, needSession, clientIp, acceptInvite } = deps
|
|
37
|
+
|
|
38
|
+
// Invite preview needs no session (the link is the secret). Invite and
|
|
39
|
+
// setup links carry their token in the URL fragment, which browsers never
|
|
40
|
+
// send, so it stays out of access logs; the console posts it here.
|
|
41
|
+
consoleApi.post(
|
|
42
|
+
'/invites/preview',
|
|
43
|
+
limit('invite'),
|
|
44
|
+
handle(async (req, res) => {
|
|
45
|
+
const invite = typeof req.body?.token === 'string' ? findInviteByHash(db, sha256(req.body.token)) : null
|
|
46
|
+
// Someone who is already in the workspace (often: they used this very
|
|
47
|
+
// link and opened it again) is told so, with the way in. Only members
|
|
48
|
+
// learn anything about a used or expired invite.
|
|
49
|
+
const current = invite ? sessionOf(req) : null
|
|
50
|
+
const memberRole = current ? roleIn(db, current.user.id, invite.workspace_id) : null
|
|
51
|
+
if (memberRole) {
|
|
52
|
+
return res.json({
|
|
53
|
+
alreadyMember: true,
|
|
54
|
+
workspace: { id: invite.workspace_id, name: invite.workspace_name },
|
|
55
|
+
role: memberRole,
|
|
56
|
+
usable: inviteUsable(invite, iso()),
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
if (!inviteUsable(invite, iso())) return fail(res, 404, 'invalid_invite', 'This invite link is used, expired or revoked.')
|
|
60
|
+
res.json({ workspace: { name: invite.workspace_name }, role: invite.role, expiresAt: invite.expires_at })
|
|
61
|
+
}),
|
|
62
|
+
)
|
|
63
|
+
consoleApi.post(
|
|
64
|
+
'/invites/accept',
|
|
65
|
+
limit('invite'),
|
|
66
|
+
needSession,
|
|
67
|
+
handle(async (req, res) => {
|
|
68
|
+
const invite = typeof req.body?.token === 'string' ? findInviteByHash(db, sha256(req.body.token)) : null
|
|
69
|
+
if (!invite) throw new AuthError(403, 'invalid_invite', 'This invite link is used, expired or revoked.')
|
|
70
|
+
acceptInvite(tenant(db, invite.workspace_id), invite, req.user.id, clientIp(req))
|
|
71
|
+
res.json({ workspaceId: invite.workspace_id })
|
|
72
|
+
}),
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Everything after the console API's needSession.
|
|
77
|
+
export function mountConsoleRoutes(consoleApi, deps) {
|
|
78
|
+
const {
|
|
79
|
+
db,
|
|
80
|
+
auth,
|
|
81
|
+
iso,
|
|
82
|
+
now,
|
|
83
|
+
publicOrigin,
|
|
84
|
+
rateLimiter,
|
|
85
|
+
mailer,
|
|
86
|
+
inviteEmailOn,
|
|
87
|
+
clientIp,
|
|
88
|
+
handle,
|
|
89
|
+
inWorkspace,
|
|
90
|
+
inProject,
|
|
91
|
+
needFresh,
|
|
92
|
+
freshOrThrow,
|
|
93
|
+
auditOf,
|
|
94
|
+
createWorkspace,
|
|
95
|
+
revokeGrantsOf,
|
|
96
|
+
ownerCount,
|
|
97
|
+
createApiToken,
|
|
98
|
+
} = deps
|
|
99
|
+
|
|
100
|
+
consoleApi.get(
|
|
101
|
+
'/workspaces',
|
|
102
|
+
handle(async (req, res) => {
|
|
103
|
+
res.json({ workspaces: workspacesForUser(db, req.user.id).map((item) => ({ id: item.id, name: item.name, role: item.role })) })
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
consoleApi.post(
|
|
107
|
+
'/workspaces',
|
|
108
|
+
handle(async (req, res) => {
|
|
109
|
+
const id = createWorkspace(req.user, req.body?.name, clientIp(req))
|
|
110
|
+
res.status(201).json({ workspace: { id } })
|
|
111
|
+
}),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
consoleApi.get(
|
|
115
|
+
'/workspaces/:ws',
|
|
116
|
+
inWorkspace(NEEDS.workspace),
|
|
117
|
+
handle(async (req, res) => {
|
|
118
|
+
const t = req.t
|
|
119
|
+
const workspace = t.get(
|
|
120
|
+
'SELECT id, name, created_at, share_links, public_projects, suspended_at, suspended_reason FROM workspaces WHERE workspace_id = :ws',
|
|
121
|
+
)
|
|
122
|
+
const members = t
|
|
123
|
+
.all(
|
|
124
|
+
`SELECT m.user_id, m.role, m.created_at, m.last_active_at, u.handle, u.display_name FROM memberships m JOIN users u ON u.id = m.user_id
|
|
125
|
+
WHERE m.workspace_id = :ws ORDER BY m.created_at`,
|
|
126
|
+
)
|
|
127
|
+
.map((row) => ({
|
|
128
|
+
userId: row.user_id,
|
|
129
|
+
handle: row.handle,
|
|
130
|
+
displayName: row.display_name,
|
|
131
|
+
role: row.role,
|
|
132
|
+
joinedAt: row.created_at,
|
|
133
|
+
lastActiveAt: row.last_active_at || null,
|
|
134
|
+
}))
|
|
135
|
+
const projects = req.projects.list().map((project) => ({
|
|
136
|
+
...publicProject(project),
|
|
137
|
+
boards: req.projects.boards(project.id).map((row) => ({ id: row.board_id, pushedAt: row.pushed_at, pushes: row.push_count })),
|
|
138
|
+
}))
|
|
139
|
+
const invites = roleAtLeast(req.role, NEEDS.invites)
|
|
140
|
+
? t
|
|
141
|
+
.all(
|
|
142
|
+
'SELECT id, role, created_at, expires_at, note FROM invites WHERE workspace_id = :ws AND used_at IS NULL AND revoked_at IS NULL AND expires_at > :now ORDER BY created_at DESC',
|
|
143
|
+
{ now: iso() },
|
|
144
|
+
)
|
|
145
|
+
.map((row) => ({ id: row.id, role: row.role, createdAt: row.created_at, expiresAt: row.expires_at, note: row.note || null }))
|
|
146
|
+
: []
|
|
147
|
+
res.json({
|
|
148
|
+
workspace: {
|
|
149
|
+
id: workspace.id,
|
|
150
|
+
name: workspace.name,
|
|
151
|
+
createdAt: workspace.created_at,
|
|
152
|
+
settings: { shareLinks: Boolean(workspace.share_links), publicProjects: Boolean(workspace.public_projects) },
|
|
153
|
+
// Set by the server operator (shapewall hosted admin suspend).
|
|
154
|
+
suspended: workspace.suspended_at ? { at: workspace.suspended_at, reason: workspace.suspended_reason || null } : null,
|
|
155
|
+
},
|
|
156
|
+
role: req.role,
|
|
157
|
+
me: { userId: req.user.id, fresh: auth.isFresh(req.auth.session) },
|
|
158
|
+
members,
|
|
159
|
+
projects,
|
|
160
|
+
invites,
|
|
161
|
+
// What the console can offer: invite emails only when the server can send them.
|
|
162
|
+
features: { inviteEmail: inviteEmailOn, inviteDays: INVITE_DAYS, shareDays: SHARE_DAYS, publicProjects: true, guestSessions: true },
|
|
163
|
+
})
|
|
164
|
+
}),
|
|
165
|
+
)
|
|
166
|
+
consoleApi.patch(
|
|
167
|
+
'/workspaces/:ws',
|
|
168
|
+
inWorkspace(NEEDS.rename),
|
|
169
|
+
handle(async (req, res) => {
|
|
170
|
+
const name = cleanWorkspaceName(req.body?.name)
|
|
171
|
+
if (!name) throw new HttpError(400, 'invalid_name', 'Workspace name: 1 to 80 characters.')
|
|
172
|
+
const before = req.t.get('SELECT name FROM workspaces WHERE workspace_id = :ws').name
|
|
173
|
+
req.t.run('UPDATE workspaces SET name = :name WHERE workspace_id = :ws', { name })
|
|
174
|
+
if (before !== name) auditOf(req, 'workspace_renamed', null, { from: before, to: name })
|
|
175
|
+
res.json({ ok: true })
|
|
176
|
+
}),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
consoleApi.patch(
|
|
180
|
+
'/workspaces/:ws/members/:uid',
|
|
181
|
+
inWorkspace(NEEDS.changeRole),
|
|
182
|
+
handle(async (req, res) => {
|
|
183
|
+
const role = req.body?.role
|
|
184
|
+
if (!ROLES.includes(role)) throw new HttpError(400, 'invalid_role', `Role: ${ROLES.join(', ')}.`)
|
|
185
|
+
// Granting admin or owner needs a recent passkey check.
|
|
186
|
+
if (inviteNeedsFresh(role)) freshOrThrow(req)
|
|
187
|
+
let revoked = { invites: 0, shares: 0, tokens: 0 }
|
|
188
|
+
transaction(db, () => {
|
|
189
|
+
const target = req.t.get('SELECT role FROM memberships WHERE workspace_id = :ws AND user_id = :user', {
|
|
190
|
+
user: String(req.params.uid),
|
|
191
|
+
})
|
|
192
|
+
if (!target) throw new HttpError(404, 'unknown_member', 'No such member.')
|
|
193
|
+
if (!canManage(req.role, target.role, role)) throw new HttpError(403, 'forbidden', 'Only an owner can change owners and admins.')
|
|
194
|
+
if (leavesNoOwner({ targetRole: target.role, newRole: role, owners: ownerCount(req.t) }))
|
|
195
|
+
throw new HttpError(409, 'last_owner', 'A workspace needs an owner.')
|
|
196
|
+
// Taking power away from an owner or admin needs a recent passkey check too.
|
|
197
|
+
if (roleChangeNeedsFresh(target.role, role)) freshOrThrow(req)
|
|
198
|
+
req.t.run('UPDATE memberships SET role = :role WHERE workspace_id = :ws AND user_id = :user', {
|
|
199
|
+
role,
|
|
200
|
+
user: String(req.params.uid),
|
|
201
|
+
})
|
|
202
|
+
auditOf(req, 'member_role_changed', String(req.params.uid), { from: target.role, to: role })
|
|
203
|
+
// Below admin also revokes the API tokens they made (they can no
|
|
204
|
+
// longer make or manage tokens; grantsToRevoke).
|
|
205
|
+
if (target.role !== role) revoked = revokeGrantsOf(req, String(req.params.uid), role)
|
|
206
|
+
})
|
|
207
|
+
res.json({ ok: true, revoked })
|
|
208
|
+
}),
|
|
209
|
+
)
|
|
210
|
+
consoleApi.delete(
|
|
211
|
+
'/workspaces/:ws/members/:uid',
|
|
212
|
+
inWorkspace(NEEDS.leave),
|
|
213
|
+
handle(async (req, res) => {
|
|
214
|
+
const userId = String(req.params.uid)
|
|
215
|
+
let revoked = { invites: 0, shares: 0, tokens: 0 }
|
|
216
|
+
transaction(db, () => {
|
|
217
|
+
const target = req.t.get('SELECT role FROM memberships WHERE workspace_id = :ws AND user_id = :user', { user: userId })
|
|
218
|
+
if (!target) throw new HttpError(404, 'unknown_member', 'No such member.')
|
|
219
|
+
const self = userId === req.user.id
|
|
220
|
+
if (!canRemoveMember(req.role, target.role, { self })) throw new HttpError(403, 'forbidden', 'You cannot remove this member.')
|
|
221
|
+
if (leavesNoOwner({ targetRole: target.role, owners: ownerCount(req.t) }))
|
|
222
|
+
throw new HttpError(409, 'last_owner', 'A workspace needs an owner.')
|
|
223
|
+
if (removalNeedsFresh(target.role, { self })) freshOrThrow(req)
|
|
224
|
+
req.t.run('DELETE FROM memberships WHERE workspace_id = :ws AND user_id = :user', { user: userId })
|
|
225
|
+
auditOf(req, self ? 'member_left' : 'member_removed', userId, { role: target.role })
|
|
226
|
+
// Their API tokens are revoked too, unless an admin asks to keep them
|
|
227
|
+
// ({ revokeTokens: false }). Kept or not, they stop working: a token
|
|
228
|
+
// works only while its maker is an admin here (tokenCreatorOk), so
|
|
229
|
+
// a kept one is inactive until they come back as an admin.
|
|
230
|
+
const keepTokens = req.body?.revokeTokens === false && roleAtLeast(req.role, NEEDS.tokens)
|
|
231
|
+
revoked = revokeGrantsOf(req, userId, null, { revokeTokens: !keepTokens })
|
|
232
|
+
})
|
|
233
|
+
res.json({ ok: true, revoked })
|
|
234
|
+
}),
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
consoleApi.post(
|
|
238
|
+
'/workspaces/:ws/invites',
|
|
239
|
+
inWorkspace(NEEDS.invites),
|
|
240
|
+
handle(async (req, res) => {
|
|
241
|
+
const role = req.body?.role || 'member'
|
|
242
|
+
if (!ROLES.includes(role)) throw new HttpError(400, 'invalid_role', `Role: ${ROLES.join(', ')}.`)
|
|
243
|
+
if (!canInvite(req.role, role)) throw new HttpError(403, 'forbidden', 'Only an owner can invite an owner.')
|
|
244
|
+
if (inviteNeedsFresh(role)) freshOrThrow(req)
|
|
245
|
+
const days = req.body?.expiresInDays ?? INVITE_DAYS_DEFAULT
|
|
246
|
+
if (!INVITE_DAYS.includes(days)) throw new HttpError(400, 'invalid_expiry', `expiresInDays: one of ${INVITE_DAYS.join(', ')}.`)
|
|
247
|
+
let email = null
|
|
248
|
+
if (req.body?.email !== undefined && req.body?.email !== null && req.body?.email !== '') {
|
|
249
|
+
if (!inviteEmailOn) throw new HttpError(409, 'email_off', 'This server does not send invite emails. Copy the link instead.')
|
|
250
|
+
email = cleanEmail(req.body.email)
|
|
251
|
+
if (!email) throw new HttpError(400, 'invalid_email', 'That email address does not look right.')
|
|
252
|
+
// Per workspace an hour, per person a day, and the whole server a day,
|
|
253
|
+
// so the operator's sending domain cannot be used to spam or phish.
|
|
254
|
+
for (const [bucket, key, message] of [
|
|
255
|
+
['inviteEmail', req.t.workspaceId, 'Too many invite emails from this workspace.'],
|
|
256
|
+
['inviteEmailUser', req.user.id, 'You sent the most invite emails for today.'],
|
|
257
|
+
['inviteEmailServer', 'all', 'This server sent the most invite emails for today.'],
|
|
258
|
+
]) {
|
|
259
|
+
const sent = rateLimiter.hit(bucket, key, LIMITS[bucket])
|
|
260
|
+
if (!sent.ok) throw new HttpError(429, 'rate_limited', `${message} Copy the link instead.`)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const pending = req.t.get(
|
|
264
|
+
'SELECT COUNT(*) AS n FROM invites WHERE workspace_id = :ws AND used_at IS NULL AND revoked_at IS NULL AND expires_at > :now',
|
|
265
|
+
{ now: iso() },
|
|
266
|
+
).n
|
|
267
|
+
if (pending >= INVITES_PENDING_MAX)
|
|
268
|
+
throw new HttpError(409, 'too_many_invites', `Revoke some open invites first (${INVITES_PENDING_MAX} at once).`)
|
|
269
|
+
// A note for the admins ("for Sam, design"): shown in the pending list,
|
|
270
|
+
// never to the person invited.
|
|
271
|
+
const note = cleanInviteNote(req.body?.note)
|
|
272
|
+
if (note === false) throw new HttpError(400, 'invalid_note', 'Note: up to 80 characters.')
|
|
273
|
+
const token = randomToken(32)
|
|
274
|
+
const id = newId('iv')
|
|
275
|
+
const expiresAt = iso(days * DAY)
|
|
276
|
+
req.t.run(
|
|
277
|
+
'INSERT INTO invites (workspace_id, id, token_hash, role, created_by, created_at, expires_at, email, note) VALUES (:ws, :id, :hash, :role, :user, :at, :expires, :email, :note)',
|
|
278
|
+
{ id, hash: sha256(token), role, user: req.user.id, at: iso(), expires: expiresAt, email, note },
|
|
279
|
+
)
|
|
280
|
+
auditOf(req, 'invite_created', id, { role, days, ...(email ? { emailed: true } : {}), ...(note ? { note } : {}) })
|
|
281
|
+
const url = `${publicOrigin}/invite#${token}`
|
|
282
|
+
let emailed = null
|
|
283
|
+
if (email) {
|
|
284
|
+
const workspace = req.t.get('SELECT name FROM workspaces WHERE workspace_id = :ws')
|
|
285
|
+
const result = await mailer.sendInvite({
|
|
286
|
+
to: email,
|
|
287
|
+
workspaceName: workspace.name,
|
|
288
|
+
role,
|
|
289
|
+
url,
|
|
290
|
+
inviterName: req.user.display_name,
|
|
291
|
+
expiresAt,
|
|
292
|
+
})
|
|
293
|
+
emailed = { ok: result.ok, error: result.ok ? null : result.error }
|
|
294
|
+
if (result.ok) req.t.run('UPDATE invites SET emailed_at = :at WHERE workspace_id = :ws AND id = :id', { at: iso(), id })
|
|
295
|
+
}
|
|
296
|
+
res.status(201).json({ invite: { id, role, expiresAt, email, note }, url, emailed })
|
|
297
|
+
}),
|
|
298
|
+
)
|
|
299
|
+
consoleApi.delete(
|
|
300
|
+
'/workspaces/:ws/invites/:id',
|
|
301
|
+
inWorkspace(NEEDS.invites),
|
|
302
|
+
handle(async (req, res) => {
|
|
303
|
+
const result = req.t.run(
|
|
304
|
+
'UPDATE invites SET revoked_at = :at WHERE workspace_id = :ws AND id = :id AND revoked_at IS NULL AND used_at IS NULL',
|
|
305
|
+
{ at: iso(), id: String(req.params.id) },
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
if (!result.changes) throw new HttpError(404, 'unknown_invite', 'No such invite.')
|
|
309
|
+
auditOf(req, 'invite_revoked', String(req.params.id))
|
|
310
|
+
res.json({ ok: true })
|
|
311
|
+
}),
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
function publicProject(project) {
|
|
315
|
+
return {
|
|
316
|
+
id: project.id,
|
|
317
|
+
name: project.name,
|
|
318
|
+
slug: project.slug,
|
|
319
|
+
createdAt: project.created_at,
|
|
320
|
+
boardUrl: `/w/${project.workspace_id}/p/${project.id}/`,
|
|
321
|
+
public: projectPublic(project),
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
consoleApi.post(
|
|
326
|
+
'/workspaces/:ws/projects',
|
|
327
|
+
inWorkspace(NEEDS.projects),
|
|
328
|
+
handle(async (req, res) => {
|
|
329
|
+
const project = req.projects.create({ name: req.body?.name, slug: req.body?.slug, userId: req.user.id, now: now() })
|
|
330
|
+
auditOf(req, 'project_created', project.id)
|
|
331
|
+
res.status(201).json({ project: publicProject(project) })
|
|
332
|
+
}),
|
|
333
|
+
)
|
|
334
|
+
consoleApi.delete(
|
|
335
|
+
'/workspaces/:ws/projects/:pid',
|
|
336
|
+
inWorkspace(NEEDS.projects),
|
|
337
|
+
needFresh,
|
|
338
|
+
inProject,
|
|
339
|
+
handle(async (req, res) => {
|
|
340
|
+
req.projects.remove(req.project.id)
|
|
341
|
+
auditOf(req, 'project_deleted', req.project.id, { name: req.project.name })
|
|
342
|
+
res.json({ ok: true })
|
|
343
|
+
}),
|
|
344
|
+
)
|
|
345
|
+
|
|
346
|
+
consoleApi.get(
|
|
347
|
+
'/workspaces/:ws/projects/:pid/tokens',
|
|
348
|
+
inWorkspace(NEEDS.tokens),
|
|
349
|
+
inProject,
|
|
350
|
+
handle(async (req, res) => {
|
|
351
|
+
res.json({ tokens: listTokens(req.t, { projectId: req.project.id, now: iso() }) })
|
|
352
|
+
}),
|
|
353
|
+
)
|
|
354
|
+
consoleApi.post(
|
|
355
|
+
'/workspaces/:ws/projects/:pid/tokens',
|
|
356
|
+
inWorkspace(NEEDS.tokens),
|
|
357
|
+
needFresh,
|
|
358
|
+
inProject,
|
|
359
|
+
handle(async (req, res) => {
|
|
360
|
+
res.status(201).json(createApiToken(req, req.body || {}))
|
|
361
|
+
}),
|
|
362
|
+
)
|
|
363
|
+
|
|
364
|
+
consoleApi.get(
|
|
365
|
+
'/workspaces/:ws/projects/:pid/agent',
|
|
366
|
+
inWorkspace('admin'),
|
|
367
|
+
inProject,
|
|
368
|
+
handle((req, res) => res.json({ policy: agentPolicy(req.t, req.project.id) })),
|
|
369
|
+
)
|
|
370
|
+
consoleApi.put(
|
|
371
|
+
'/workspaces/:ws/projects/:pid/agent',
|
|
372
|
+
inWorkspace('admin'),
|
|
373
|
+
needFresh,
|
|
374
|
+
inProject,
|
|
375
|
+
handle((req, res) => {
|
|
376
|
+
const policy = cleanPolicy(req.body)
|
|
377
|
+
req.t.run('UPDATE projects SET agent_policy = :policy WHERE workspace_id = :ws AND id = :project', {
|
|
378
|
+
project: req.project.id,
|
|
379
|
+
policy: JSON.stringify(policy),
|
|
380
|
+
})
|
|
381
|
+
auditOf(req, 'agent_policy_changed', req.project.id)
|
|
382
|
+
res.json({ policy })
|
|
383
|
+
}),
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
mountDeviceApprovals(consoleApi, deps)
|
|
387
|
+
consoleApi.delete(
|
|
388
|
+
'/workspaces/:ws/projects/:pid/tokens/:tid',
|
|
389
|
+
inWorkspace(NEEDS.tokens),
|
|
390
|
+
inProject,
|
|
391
|
+
handle(async (req, res) => {
|
|
392
|
+
const result = req.t.run(
|
|
393
|
+
'UPDATE api_tokens SET revoked_at = :at WHERE workspace_id = :ws AND project_id = :project AND id = :id AND revoked_at IS NULL',
|
|
394
|
+
{ at: iso(), project: req.project.id, id: String(req.params.tid) },
|
|
395
|
+
)
|
|
396
|
+
if (!result.changes) throw new HttpError(404, 'unknown_token', 'No such token.')
|
|
397
|
+
auditOf(req, 'token_revoked', String(req.params.tid))
|
|
398
|
+
res.json({ ok: true })
|
|
399
|
+
}),
|
|
400
|
+
)
|
|
401
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Cookie names and parsing shared by the hosted app, share links and presence.
|
|
2
|
+
|
|
3
|
+
// The account session (app.js) and a share link guest's session (shares.js).
|
|
4
|
+
// On https the __Host- and __Secure- prefixes pin them to this origin.
|
|
5
|
+
export const sessionCookieName = (secure) => (secure ? '__Host-sw_session' : 'sw_session')
|
|
6
|
+
export const guestCookieName = (secure) => (secure ? '__Secure-sw_guest' : 'sw_guest')
|
|
7
|
+
|
|
8
|
+
// A Cookie header as { name: value }; the first of a repeated name wins.
|
|
9
|
+
export function parseCookies(header) {
|
|
10
|
+
const out = {}
|
|
11
|
+
for (const part of String(header || '').split(';')) {
|
|
12
|
+
const index = part.indexOf('=')
|
|
13
|
+
if (index < 0) continue
|
|
14
|
+
const name = part.slice(0, index).trim()
|
|
15
|
+
if (name && !(name in out)) out[name] = part.slice(index + 1).trim()
|
|
16
|
+
}
|
|
17
|
+
return out
|
|
18
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Random tokens, hashes and signed URLs for the hosted server. Every secret
|
|
2
|
+
// that reaches a client (session, invite, API token, recovery code) is stored
|
|
3
|
+
// only as its SHA-256; secrets are 32 random bytes (recovery codes 16), so a
|
|
4
|
+
// plain hash is enough (nothing to brute-force, unlike a password).
|
|
5
|
+
|
|
6
|
+
import { createHash, createHmac, randomBytes, scrypt, timingSafeEqual } from 'node:crypto'
|
|
7
|
+
import { promisify } from 'node:util'
|
|
8
|
+
|
|
9
|
+
const scryptAsync = promisify(scrypt)
|
|
10
|
+
const SCRYPT = { N: 16384, r: 8, p: 1 }
|
|
11
|
+
|
|
12
|
+
export const sha256 = (value) => createHash('sha256').update(String(value)).digest('hex')
|
|
13
|
+
export const randomToken = (bytes = 32) => randomBytes(bytes).toString('base64url')
|
|
14
|
+
export const newId = (prefix) => `${prefix}_${randomBytes(12).toString('hex')}`
|
|
15
|
+
export { ID_PATTERNS } from '../remote/ids.js'
|
|
16
|
+
|
|
17
|
+
// API tokens: swp_ + 43 base64url characters (32 bytes).
|
|
18
|
+
export const API_TOKEN_RE = /^swp_[A-Za-z0-9_-]{43}$/
|
|
19
|
+
export function newApiToken() {
|
|
20
|
+
const token = `swp_${randomToken(32)}`
|
|
21
|
+
return { token, hash: sha256(token), prefix: token.slice(0, 12) }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Recovery codes: 16 random bytes (128 bits) as 4 groups of base32-ish
|
|
25
|
+
// characters, easy to type; compared case-insensitively without dashes.
|
|
26
|
+
const CODE_ALPHABET = 'abcdefghjkmnpqrstuvwxyz23456789'
|
|
27
|
+
// 26 characters of a 31-letter alphabet is about 128.8 bits; bytes are
|
|
28
|
+
// drawn by rejection so every letter is equally likely.
|
|
29
|
+
export function newRecoveryCode() {
|
|
30
|
+
const limit = 256 - (256 % CODE_ALPHABET.length)
|
|
31
|
+
let out = ''
|
|
32
|
+
while (out.length < 26) {
|
|
33
|
+
for (const byte of randomBytes(32)) {
|
|
34
|
+
if (byte < limit && out.length < 26) out += CODE_ALPHABET[byte % CODE_ALPHABET.length]
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return out.match(/.{1,6}/g).join('-')
|
|
38
|
+
}
|
|
39
|
+
export const normalizeRecoveryCode = (value) =>
|
|
40
|
+
String(value || '')
|
|
41
|
+
.toLowerCase()
|
|
42
|
+
.replace(/[^a-z0-9]/g, '')
|
|
43
|
+
|
|
44
|
+
// Ids that only the hosted server makes (the clients in src/remote never see them).
|
|
45
|
+
export const SHARE_ID_RE = /^sh_[a-f0-9]{24}$/
|
|
46
|
+
export const SESSION_ID_RE = /^se_[a-f0-9]{24}$/
|
|
47
|
+
|
|
48
|
+
// Share link passcodes are short and chosen by people, so unlike the random
|
|
49
|
+
// secrets above they get a salted, slow hash (scrypt). The link's own 32-byte
|
|
50
|
+
// token is still required; the passcode is a second thing to know.
|
|
51
|
+
// Off the event loop (scrypt runs in the libuv pool).
|
|
52
|
+
export async function hashPasscode(passcode) {
|
|
53
|
+
const salt = randomBytes(16)
|
|
54
|
+
const hash = await scryptAsync(String(passcode), salt, 32, SCRYPT)
|
|
55
|
+
return ['scrypt', salt.toString('base64url'), hash.toString('base64url')].join('$')
|
|
56
|
+
}
|
|
57
|
+
export async function checkPasscode(passcode, stored) {
|
|
58
|
+
const [kind, salt, hash] = String(stored || '').split('$')
|
|
59
|
+
if (kind !== 'scrypt' || !salt || !hash) return false
|
|
60
|
+
const actual = await scryptAsync(String(passcode), Buffer.from(salt, 'base64url'), 32, SCRYPT)
|
|
61
|
+
return safeEqual(actual.toString('base64url'), hash)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function safeEqual(a, b) {
|
|
65
|
+
const left = Buffer.from(String(a))
|
|
66
|
+
const right = Buffer.from(String(b))
|
|
67
|
+
return left.length === right.length && timingSafeEqual(left, right)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Signed, short-lived URLs for captured files (the usercontent path).
|
|
71
|
+
export function signPath(secret, value) {
|
|
72
|
+
return createHmac('sha256', secret).update(value).digest('base64url')
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// A usercontent URL's signature: over its parts as one JSON array, so no
|
|
76
|
+
// part can run into the next (ids and file names have no slash, but the
|
|
77
|
+
// share part is optional).
|
|
78
|
+
export function signUsercontent(secret, parts) {
|
|
79
|
+
return signPath(secret, JSON.stringify(parts.map((part) => String(part ?? ''))))
|
|
80
|
+
}
|