shapewall 0.3.3 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/CHANGELOG.md +501 -0
  2. package/README.md +87 -4
  3. package/THIRD_PARTY.md +15 -0
  4. package/dist/agent/package-lock.json +594 -2
  5. package/dist/client/assets/BoardChat-WS8mAIW6.js +27 -0
  6. package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-B-Mlr6wF.js} +1 -1
  7. package/dist/client/assets/ChecksPanel-Dx1ZLBR6.js +1 -0
  8. package/dist/client/assets/History-sXhmpcvs.js +1 -0
  9. package/dist/client/assets/LocalSettings-CtGzz9F3.js +1 -0
  10. package/dist/client/assets/{Management-NVFDtSZB.js → Management-CGHe8Wt9.js} +1 -1
  11. package/dist/client/assets/QuickConnect-DEk84E4A.js +1 -0
  12. package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-CGGx-AYK.js} +2 -2
  13. package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-_bVp8hnC.js} +1 -1
  14. package/dist/client/assets/{circle-x-B6KfScOE.js → circle-x-BWoHA1QY.js} +1 -1
  15. package/dist/client/assets/index-Bm-Xd3YN.js +37 -0
  16. package/dist/client/assets/index-C7Ostk3K.css +2 -0
  17. package/dist/client/assets/play-BzQ3tRTv.js +1 -0
  18. package/dist/client/assets/{refresh-cw-vkgvCCfR.js → refresh-cw-BYBMyDKT.js} +1 -1
  19. package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
  20. package/dist/client/assets/ui-CAY6myRA.js +41 -0
  21. package/dist/client/build-info.json +1 -1
  22. package/dist/client/index.html +3 -3
  23. package/dist/client/sw.js +2 -2
  24. package/dist/console/assets/LocalAccountsApp-CWRguRRL.js +1 -0
  25. package/dist/console/assets/LocalConsole--I2SaIpr.js +2 -0
  26. package/dist/console/assets/LocalUsage-BmpCZ36k.js +1 -0
  27. package/dist/console/assets/app-update-dialog-B8M9plaT.js +1 -0
  28. package/dist/console/assets/app-version-muKrQqbL.js +1 -0
  29. package/dist/console/assets/console-CrhTsPbz.css +2 -0
  30. package/dist/console/assets/console-DxapQ9oS.js +11 -0
  31. package/dist/console/assets/local-agents-CoqD4dSN.js +6 -0
  32. package/dist/console/assets/minus-DDQ0ydcM.js +1 -0
  33. package/dist/console/assets/tailnet-C5Flwl_d.js +2 -0
  34. package/dist/console/assets/theme-DCd0qh4l.js +1 -0
  35. package/dist/console/assets/ui-Bry1mnCY.js +41 -0
  36. package/dist/console/assets/usage-CIPWnHaS.css +1 -0
  37. package/dist/console/assets/usage-CqVjMb7m.js +5 -0
  38. package/dist/console/assets/usage-D1G0GgE3.js +56 -0
  39. package/dist/console/build-info.json +1 -1
  40. package/dist/console/console.html +8 -2
  41. package/docs/agent-hardening.md +206 -0
  42. package/docs/agent-recipes.md +23 -0
  43. package/docs/agent-workflow.md +5 -0
  44. package/docs/agents.md +49 -6
  45. package/docs/architecture.md +10 -4
  46. package/docs/deployment.md +39 -17
  47. package/docs/design-system.md +56 -7
  48. package/docs/hosted.md +1800 -46
  49. package/docs/images/board-overview.png +0 -0
  50. package/docs/images/journeys.png +0 -0
  51. package/docs/images/live-comment.png +0 -0
  52. package/docs/images/phone-comment.png +0 -0
  53. package/docs/images/phone-overview.png +0 -0
  54. package/docs/images/variant.png +0 -0
  55. package/docs/where-to-run.md +119 -3
  56. package/package.json +6 -1
  57. package/src/agent/backends/acp.js +745 -44
  58. package/src/agent/branch-mode.js +17 -3
  59. package/src/agent/card-hash.js +65 -0
  60. package/src/agent/context.js +41 -0
  61. package/src/agent/exec-paths.js +105 -0
  62. package/src/agent/routes.js +23 -2
  63. package/src/agent/run.js +11 -1
  64. package/src/agent/starter/cli.js +6 -1
  65. package/src/agent/turn-changes.js +70 -8
  66. package/src/agent/usage-log.js +251 -0
  67. package/src/cli/args.js +37 -0
  68. package/src/cli/commands.js +20 -5
  69. package/src/cli/help.js +73 -3
  70. package/src/cli/init.js +2 -1
  71. package/src/cli/serve.js +20 -5
  72. package/src/cli.js +8 -0
  73. package/src/hosted/access-cli.js +99 -1
  74. package/src/hosted/activity-core.js +232 -0
  75. package/src/hosted/activity.js +375 -0
  76. package/src/hosted/admin.js +87 -13
  77. package/src/hosted/agent-approvals.js +29 -4
  78. package/src/hosted/agent-link.js +1032 -0
  79. package/src/hosted/agent-policy.js +10 -3
  80. package/src/hosted/agent-presence.js +63 -22
  81. package/src/hosted/agent-routes.js +135 -0
  82. package/src/hosted/agents.js +1206 -0
  83. package/src/hosted/app.js +178 -12
  84. package/src/hosted/auth-routes.js +156 -15
  85. package/src/hosted/auth.js +123 -20
  86. package/src/hosted/board-routes.js +129 -5
  87. package/src/hosted/chat-frames.js +246 -0
  88. package/src/hosted/chat-job.js +204 -0
  89. package/src/hosted/chat-permissions.js +203 -0
  90. package/src/hosted/chat-routes.js +1009 -0
  91. package/src/hosted/chat-store.js +477 -0
  92. package/src/hosted/comment-files.js +11 -1
  93. package/src/hosted/comments.js +1 -1
  94. package/src/hosted/console-routes.js +122 -34
  95. package/src/hosted/db.js +295 -0
  96. package/src/hosted/device-routes.js +88 -10
  97. package/src/hosted/device.js +54 -4
  98. package/src/hosted/events.js +3 -3
  99. package/src/hosted/guards.js +337 -27
  100. package/src/hosted/http.js +6 -3
  101. package/src/hosted/membership.js +22 -3
  102. package/src/hosted/operator-access.js +418 -0
  103. package/src/hosted/operator.js +106 -2
  104. package/src/hosted/ops-cli.js +238 -3
  105. package/src/hosted/pages.js +4 -0
  106. package/src/hosted/passkey-policy.js +75 -0
  107. package/src/hosted/permissions.js +52 -6
  108. package/src/hosted/presence.js +31 -2
  109. package/src/hosted/projects.js +30 -2
  110. package/src/hosted/public.js +4 -4
  111. package/src/hosted/ratelimit.js +14 -0
  112. package/src/hosted/retention.js +18 -2
  113. package/src/hosted/server.js +123 -11
  114. package/src/hosted/shares.js +2 -2
  115. package/src/hosted/step-up-settings.js +68 -0
  116. package/src/hosted/step-up.js +135 -0
  117. package/src/hosted/tailnet-identity.js +24 -0
  118. package/src/hosted/tailnet-routes.js +342 -0
  119. package/src/hosted/tailnet.js +220 -0
  120. package/src/hosted/tailscale.js +75 -9
  121. package/src/hosted/tenancy.js +66 -1
  122. package/src/hosted/token-api.js +119 -20
  123. package/src/hosted/token-events.js +5 -3
  124. package/src/hosted/tokens.js +29 -6
  125. package/src/hosted/turn-hub.js +152 -0
  126. package/src/hosted/usage-core.js +624 -0
  127. package/src/hosted/usage-layout.js +724 -0
  128. package/src/hosted/usage-routes.js +375 -0
  129. package/src/hosted/usage-shape.js +256 -0
  130. package/src/hosted/usage.js +641 -0
  131. package/src/remote/agent-connect.js +469 -0
  132. package/src/remote/agent-fix.js +611 -0
  133. package/src/remote/agent-job.js +216 -0
  134. package/src/remote/agent-link.js +906 -0
  135. package/src/remote/agent-policy-file.js +242 -0
  136. package/src/remote/credentials.js +35 -1
  137. package/src/remote/login.js +44 -9
  138. package/src/remote/share.js +1 -1
  139. package/src/remote/tailnet-cli.js +547 -0
  140. package/src/server/app.js +15 -0
  141. package/src/server/pairing-routes.js +34 -5
  142. package/src/server/tailscale.js +97 -4
  143. package/src/tailnet-proof.js +766 -0
  144. package/dist/client/assets/BoardChat-DxehB761.js +0 -27
  145. package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
  146. package/dist/client/assets/History-C9N32kCv.js +0 -1
  147. package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
  148. package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
  149. package/dist/client/assets/index-CsqLdqGx.css +0 -2
  150. package/dist/client/assets/index-DnZarJ3U.js +0 -37
  151. package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
  152. package/dist/client/assets/ui-DI-FGWWK.js +0 -41
  153. package/dist/console/assets/LocalAccountsApp-VOCZg8dg.js +0 -1
  154. package/dist/console/assets/LocalConsole-CMwAW-mF.js +0 -1
  155. package/dist/console/assets/app-update-dialog-Cw6UDqNP.js +0 -1
  156. package/dist/console/assets/console-DgfWmUEx.js +0 -55
  157. package/dist/console/assets/console-Lis0vvdL.css +0 -2
package/src/hosted/app.js CHANGED
@@ -31,7 +31,7 @@ import { listTokens, mountAdmin } from './admin.js'
31
31
  import { createDeviceLogins } from './device.js'
32
32
  import { cleanEmail } from './mail.js'
33
33
  import { mountShareAdmin, mountSharePublic } from './shares.js'
34
- import { createCommentStore } from './comments.js'
34
+ import { REPLY_AUTHOR, createCommentStore } from './comments.js'
35
35
  import { createUsercontent } from './usercontent.js'
36
36
  import { mountRebind, prepareOrigins, previousHostGate } from './rebind.js'
37
37
  import { mountPublicAdmin } from './public.js'
@@ -61,7 +61,16 @@ import { createBoardContext } from './board-context.js'
61
61
  import { mountBoardRoutes } from './board-routes.js'
62
62
  import { mountTokenApi } from './token-api.js'
63
63
  import { createAgentPresence } from './agent-presence.js'
64
+ import { COMMENT_ROUTING, createAgentStore } from './agents.js'
65
+ import { mountAgentRoutes } from './agent-routes.js'
66
+ import { mountUsageRoutes } from './usage-routes.js'
67
+ import { createAgentLinks, createChatService } from './agent-link.js'
68
+ import { createChatStore } from './chat-store.js'
69
+ import { createTurnHub } from './turn-hub.js'
64
70
  import { mountPages } from './pages.js'
71
+ import { createPasskeyPolicy } from './passkey-policy.js'
72
+ import { createTailnetAccounts } from './tailnet.js'
73
+ import { mountTailnetAdmin } from './tailnet-routes.js'
65
74
 
66
75
  export { changeEventType, publicBoardView, TOKEN_SCOPES, INVITE_DAYS }
67
76
 
@@ -120,7 +129,23 @@ export function createHostedApp({
120
129
  turnstile = null,
121
130
  // Where "Report this board" on a public project goes (an email address).
122
131
  reportEmail = null,
132
+ // Whether a workspace's owners see their members' account events (sign-ins,
133
+ // passkeys, sessions) in Activity. Off: those are each person's own. For a
134
+ // server with one team; set by the operator (--owners-see-sign-ins).
135
+ ownersSeeSignIns = false,
136
+ // Tailnet sign-in: { identityOf(req) } says who a request is from, by the
137
+ // identity headers of Tailscale's serve proxy and only when the request
138
+ // provably came through it (src/hosted/tailnet-identity.js). null: off,
139
+ // and the headers are never read.
140
+ tailnet = null,
141
+ // With tailnet sign-in: also allow passkeys (--tailnet-passkeys). Off by
142
+ // default there; the operator's switch in the database wins
143
+ // (src/hosted/passkey-policy.js). Without tailnet sign-in passkeys are
144
+ // always the sign-in.
145
+ tailnetPasskeys = false,
123
146
  }) {
147
+ if (guestOnly) tailnet = null
148
+ const passkeys = createPasskeyPolicy({ db, tailnet, flag: tailnetPasskeys })
124
149
  const base = new URL(origin)
125
150
  if (!['http:', 'https:'].includes(base.protocol) || base.pathname !== '/' || base.search)
126
151
  throw new Error('origin: an http(s) origin without a path')
@@ -132,16 +157,49 @@ export function createHostedApp({
132
157
  deviceLogins ||= createDeviceLogins({ db, now })
133
158
  const ownsEvents = !eventBus
134
159
  const events = eventBus || createEventBus()
135
- // Which projects have an agent listening right now (in memory only).
160
+ // Which agents are listening right now (in memory only).
136
161
  const agentPresence = createAgentPresence({ now })
137
162
  const streams = new Set()
163
+ // Connected agents (`shapewall agent connect`): the chat turns in flight
164
+ // and the open links, in memory (src/hosted/turn-hub.js, agent-link.js).
165
+ const turnHub = createTurnHub({ now: () => now().getTime() })
166
+ const links = createAgentLinks({ hub: turnHub })
167
+ const chatFor = (t) => createChatStore(t, { now })
138
168
  const policyFor = (t) =>
139
169
  createAgentPolicy(t, { now, handleFor: (user) => q.get('SELECT handle FROM users WHERE id = :id', { id: user })?.handle || null })
170
+ const handleFor = (user) => q.get('SELECT handle FROM users WHERE id = :id', { id: user })?.handle || null
171
+ // The agents of a workspace (src/hosted/agents.js), one store per tenant
172
+ // handle: a handle lives for one request, so what the store read once in
173
+ // a request is not read again.
174
+ const agentStores = new WeakMap()
175
+ const agentsFor = (t) => {
176
+ if (!agentStores.has(t)) agentStores.set(t, createAgentStore(t, { now, presence: agentPresence, handleFor, links }))
177
+ return agentStores.get(t)
178
+ }
140
179
  const dispatchView = (t, project, board, comment) => {
141
180
  const row = createCommentStore(t, project, board).get(comment.id)
181
+ const agents = agentsFor(t)
142
182
  // dispatchHash: what "Send to agent" must send back, so a vouch covers
143
183
  // the text its sender saw (src/hosted/board-routes.js).
144
- return { ...comment, ...policyFor(t).state(project, board, row), dispatchHash: dispatchHash(row) }
184
+ return {
185
+ ...comment,
186
+ ...policyFor(t).state(project, board, row),
187
+ dispatchHash: dispatchHash(row),
188
+ // The agent the comment was sent to (null: any agent), and which agent
189
+ // wrote each agent reply, for its face on the thread. Agent ids only.
190
+ agentId: agents.addressOf(project, row),
191
+ replies: comment.replies.map((reply) => {
192
+ const agentId = agents.idForToken(project, reply[REPLY_AUTHOR]?.token)
193
+ return agentId ? { ...reply, agentId } : reply
194
+ }),
195
+ // What decides whether a token may take it (never serialized).
196
+ [COMMENT_ROUTING]: {
197
+ agent_id: row.agent_id || null,
198
+ dispatch_by: row.dispatch_by || null,
199
+ board_id: row.board_id,
200
+ number: row.number,
201
+ },
202
+ }
145
203
  }
146
204
  const q = accounts(db)
147
205
  const iso = (offset = 0) => new Date(now().getTime() + offset).toISOString()
@@ -174,6 +232,10 @@ export function createHostedApp({
174
232
  const SESSION_COOKIE = sessionCookieName(secure)
175
233
  const CEREMONY_COOKIE = secure ? '__Host-sw_ceremony' : 'sw_ceremony'
176
234
 
235
+ // What ends a chat turn from the server's side (the machine went away, a
236
+ // revoked agent, a restart).
237
+ const chat = createChatService({ db, hub: turnHub, links, events, chatFor, now })
238
+
177
239
  const app = express()
178
240
  app.disable('x-powered-by')
179
241
  app.use(publishMutations({ events, streams }))
@@ -208,12 +270,42 @@ export function createHostedApp({
208
270
  needSession,
209
271
  needFresh,
210
272
  needFreshOrRecovery,
211
- freshOrThrow,
273
+ passkeysGate,
274
+ tailnetOnly,
275
+ needFreshOrHeldRemoval,
276
+ removesHeldPasskey,
277
+ noPasskey,
278
+ needUnrestricted,
279
+ needPasskeyBacked,
280
+ fixedCore,
281
+ cutTailnetSessions,
282
+ passkeyOrThrow,
283
+ machineTokensOrThrow,
284
+ adminMakingOrThrow,
285
+ coreFreshOrThrow,
286
+ stepUp,
287
+ stepUpOrThrow,
212
288
  inWorkspace,
213
289
  inProject,
214
290
  handle,
215
291
  auditOf,
216
- } = createGuards({ db, dataDir, auth, quotas, secure, trustProxy, rateLimiter, siteOf, SESSION_COOKIE, iso, now, log })
292
+ } = createGuards({
293
+ db,
294
+ dataDir,
295
+ auth,
296
+ quotas,
297
+ secure,
298
+ trustProxy,
299
+ rateLimiter,
300
+ siteOf,
301
+ SESSION_COOKIE,
302
+ iso,
303
+ now,
304
+ log,
305
+ tailnet,
306
+ passkeys,
307
+ })
308
+ const tailnetAccounts = createTailnetAccounts({ db, iso, now })
217
309
 
218
310
  // ---- Headers for everything ----
219
311
  app.use((req, res, next) => {
@@ -228,8 +320,18 @@ export function createHostedApp({
228
320
  app.use(usercontent.gate)
229
321
  // While moving: which origin a request is for; the old one serves only the move.
230
322
  app.use(previousHostGate(sites))
323
+ // Passkeys off (a tailnet sign-in server, by default): their routes say so.
324
+ app.use(passkeysGate)
325
+ // The fixed core: what always asks for a fresh passkey check (one table, src/hosted/step-up.js).
326
+ app.use(fixedCore)
231
327
 
232
- const { publicUser, createWorkspace, acceptInvite, revokeGrantsOf, ownerCount } = createMembership({ db, quotas, iso, auditOf })
328
+ const { publicUser, createWorkspace, acceptInvite, revokeGrantsOf, ownerCount } = createMembership({
329
+ db,
330
+ quotas,
331
+ iso,
332
+ auditOf,
333
+ agentsFor,
334
+ })
233
335
 
234
336
  // ---- Health ----
235
337
  app.get('/api/health', (req, res) => res.json({ ok: true }))
@@ -269,12 +371,23 @@ export function createHostedApp({
269
371
  needSession,
270
372
  needFresh,
271
373
  needFreshOrRecovery,
374
+ tailnetOnly,
375
+ passkeys,
376
+ needFreshOrHeldRemoval,
377
+ removesHeldPasskey,
378
+ noPasskey,
379
+ needUnrestricted,
380
+ needPasskeyBacked,
381
+ cutTailnetSessions,
272
382
  auditOf,
273
383
  publicUser,
274
384
  createWorkspace,
275
385
  acceptInvite,
276
386
  revokeGrantsOf,
277
387
  ownerCount,
388
+ secure,
389
+ tailnet,
390
+ tailnetAccounts,
278
391
  })
279
392
  app.use('/auth', authRouter)
280
393
  app.use('/auth', (req, res) => fail(res, 404, 'not_found', 'No such route.'))
@@ -304,7 +417,9 @@ export function createHostedApp({
304
417
  inWorkspace,
305
418
  inProject,
306
419
  needFresh,
307
- freshOrThrow,
420
+ coreFreshOrThrow,
421
+ stepUp,
422
+ stepUpOrThrow,
308
423
  auditOf,
309
424
  HttpError,
310
425
  listTokens,
@@ -333,9 +448,10 @@ export function createHostedApp({
333
448
  consoleApi.use(guardWrites)
334
449
  consoleApi.use(express.json({ limit: '64kb' }))
335
450
 
336
- const createApiToken = createApiTokens({ iso, auditOf })
451
+ const createApiToken = createApiTokens({ iso, auditOf, agentsFor })
337
452
  const consoleDeps = {
338
453
  db,
454
+ passkeys,
339
455
  dataDir,
340
456
  auth,
341
457
  iso,
@@ -354,13 +470,22 @@ export function createHostedApp({
354
470
  inWorkspace,
355
471
  inProject,
356
472
  needFresh,
357
- freshOrThrow,
473
+ coreFreshOrThrow,
474
+ passkeyOrThrow,
475
+ machineTokensOrThrow,
476
+ adminMakingOrThrow,
477
+ stepUp,
478
+ stepUpOrThrow,
358
479
  auditOf,
359
480
  createWorkspace,
360
481
  acceptInvite,
361
482
  revokeGrantsOf,
362
483
  ownerCount,
363
484
  createApiToken,
485
+ agentsFor,
486
+ ownersSeeSignIns: ownersSeeSignIns === true,
487
+ tailnet,
488
+ tailnetAccounts,
364
489
  }
365
490
  mountInviteLinks(consoleApi, consoleDeps)
366
491
  // Share link preview and opening: no session, the link is the secret.
@@ -371,6 +496,9 @@ export function createHostedApp({
371
496
  consoleApi.use(needSession)
372
497
  consoleApi.use(limit('session', (req) => req.user.id))
373
498
  mountConsoleRoutes(consoleApi, consoleDeps)
499
+ mountTailnetAdmin(consoleApi, consoleDeps)
500
+ mountAgentRoutes(consoleApi, consoleDeps)
501
+ mountUsageRoutes(consoleApi, consoleDeps)
374
502
  mountAdmin(consoleApi, deps)
375
503
  mountShareAdmin(consoleApi, deps)
376
504
  mountPublicAdmin(consoleApi, deps)
@@ -390,16 +518,22 @@ export function createHostedApp({
390
518
  inWorkspace,
391
519
  inProject,
392
520
  handle,
393
- freshOrThrow,
521
+ stepUp,
522
+ stepUpOrThrow,
394
523
  auditOf,
395
524
  clientIp,
396
525
  policyFor,
526
+ agentsFor,
397
527
  dispatchView,
398
528
  boardContext,
399
529
  cleaner,
400
530
  quotas,
401
531
  workspaceQuota,
402
532
  deps,
533
+ chatFor,
534
+ hub: turnHub,
535
+ links,
536
+ chat,
403
537
  })
404
538
 
405
539
  mountDeviceApi(app, consoleDeps)
@@ -423,10 +557,15 @@ export function createHostedApp({
423
557
  auditOf,
424
558
  clientIp,
425
559
  policyFor,
560
+ agentsFor,
426
561
  boardContext,
427
562
  dispatchView,
428
- needFresh,
563
+ stepUp,
429
564
  approvalApi,
565
+ chatFor,
566
+ hub: turnHub,
567
+ links,
568
+ chat,
430
569
  })
431
570
  // The token API first: it never falls through to the cookie-based console API.
432
571
  mountMcpHttp(app, { tokenAuth, origin: publicOrigin, dataDir, db })
@@ -471,6 +610,7 @@ export function createHostedApp({
471
610
  return mintSetupLink(db, publicOrigin, now())
472
611
  }
473
612
  app.locals.deviceLogins = deviceLogins
613
+ app.locals.cutTailnetSessions = cutTailnetSessions
474
614
  app.locals.sites = sites
475
615
 
476
616
  // Staging sweep at start and on a timer (unref'd: it never keeps the
@@ -486,6 +626,25 @@ export function createHostedApp({
486
626
  if (!guestOnly) sweep()
487
627
  const sweepTimer = !guestOnly && sweepEveryMs ? setInterval(sweep, sweepEveryMs) : null
488
628
  sweepTimer?.unref()
629
+ // No turn survives a restart: what the database still calls running ends here.
630
+ if (!guestOnly) {
631
+ try {
632
+ chat.sweepStale()
633
+ } catch (error) {
634
+ log(`turn sweep at start: ${error.message}`)
635
+ }
636
+ }
637
+ // Chat turns whose machine went away end as interrupted (unref'd too).
638
+ const turnTimer = guestOnly
639
+ ? null
640
+ : setInterval(() => {
641
+ try {
642
+ chat.sweep()
643
+ } catch (error) {
644
+ log(`turn sweep: ${error.message}`)
645
+ }
646
+ }, 5000)
647
+ turnTimer?.unref()
489
648
  const ownsPresence = !presenceHub
490
649
  const hub = presenceHub || createPresenceHub({ events })
491
650
  // A WebSocket upgrade never passes through Express, so its address is
@@ -504,10 +663,14 @@ export function createHostedApp({
504
663
  while (i < hops.length - 1 && hops[i + 1] && trusted(hops[i], i)) i += 1
505
664
  return hops[i] || 'unknown'
506
665
  }
507
- const presence = hub.listener(presenceAuthorizer({ db, dataDir, origin: publicOrigin, auth, now, guestOnly }), { clientIp: upgradeIp })
666
+ const presence = hub.listener(presenceAuthorizer({ db, dataDir, origin: publicOrigin, auth, now, guestOnly, tailnet, passkeys }), {
667
+ clientIp: upgradeIp,
668
+ })
508
669
  app.locals.presence = { attach: presence.attach, close: presence.close, hub }
509
670
  app.locals.events = events
510
671
  app.locals.agentPresence = agentPresence
672
+ app.locals.agentsFor = agentsFor
673
+ app.locals.chat = { hub: turnHub, links, service: chat, storeFor: chatFor }
511
674
  app.locals.sweep = sweep
512
675
  app.locals.close = async () => {
513
676
  presence.close()
@@ -515,6 +678,9 @@ export function createHostedApp({
515
678
  if (ownsEvents) events.close()
516
679
  for (const check of [...streams]) check(true)
517
680
  if (sweepTimer) clearInterval(sweepTimer)
681
+ if (turnTimer) clearInterval(turnTimer)
682
+ chat.close()
683
+ links.closeAll()
518
684
  await cleaner.close()
519
685
  }
520
686
  return app
@@ -15,10 +15,12 @@ import { HttpError, fail, cleanWorkspaceName } from './http.js'
15
15
  import { LIMITS } from './ratelimit.js'
16
16
  import { moveInfo } from './rebind.js'
17
17
  import { removeSetupLink } from './setup-link.js'
18
+ import { mountTailnetAuth } from './tailnet-routes.js'
18
19
  import {
19
20
  accountEvents,
20
21
  auditAccount,
21
22
  deleteAccountEvents,
23
+ deleteSavedViewsOf,
22
24
  findInviteByHash,
23
25
  inviteUsable,
24
26
  lastRecoverySignIn,
@@ -51,20 +53,31 @@ export function mountAuthRoutes(authRouter, deps) {
51
53
  needSession,
52
54
  needFresh,
53
55
  needFreshOrRecovery,
56
+ passkeys,
57
+ needFreshOrHeldRemoval,
58
+ removesHeldPasskey,
59
+ noPasskey,
60
+ needPasskeyBacked,
54
61
  auditOf,
55
62
  publicUser,
56
63
  createWorkspace,
57
64
  acceptInvite,
58
65
  revokeGrantsOf,
59
66
  ownerCount,
67
+ tailnetAccounts,
60
68
  } = deps
61
69
 
70
+ // Tailnet sign-in (src/hosted/tailnet-routes.js); `tailnet.on` false when the server runs without it.
71
+ const tailnet = mountTailnetAuth(authRouter, deps)
72
+
62
73
  // What the sign-up ceremony keeps (in challenges.data) is the SHA-256 of
63
74
  // the invite or setup token, never the token itself.
64
- function signupAllowed({ invite, setup }) {
75
+ function signupAllowed({ invite, setup }, req) {
65
76
  if (typeof invite === 'string' && invite) {
66
77
  const row = findInviteByHash(db, sha256(invite))
67
- if (!inviteUsable(row, iso())) throw new AuthError(403, 'invalid_invite', 'This invite link is used, expired or revoked.')
78
+ // An invite made for a tailnet login is that login's alone.
79
+ if (!inviteUsable(row, iso()) || (row.tailnet_login && row.tailnet_login !== tailnet.identityOf(req)?.login))
80
+ throw new AuthError(403, 'invalid_invite', 'This invite link is used, expired or revoked.')
68
81
  return { inviteHash: sha256(invite) }
69
82
  }
70
83
  if (typeof setup === 'string' && setup) {
@@ -84,6 +97,7 @@ export function mountAuthRoutes(authRouter, deps) {
84
97
  const token = auth.createSession(userId, { ...options, device: deviceLabel(req.headers['user-agent']), ip: clientIp(req) })
85
98
  setCookie(res, SESSION_COOKIE, token, 30 * 86400)
86
99
  setCookie(res, CEREMONY_COOKIE, '', 0)
100
+ if (tailnet.on) tailnet.clearSignedOut(res)
87
101
  return sha256(token)
88
102
  }
89
103
  const ceremonyCookie = (req) => parseCookies(req.headers.cookie)[CEREMONY_COOKIE]
@@ -93,7 +107,14 @@ export function mountAuthRoutes(authRouter, deps) {
93
107
  limit('ceremony'),
94
108
  handle(async (req, res) => {
95
109
  const { handle: name, displayName, invite, setup, workspaceName } = req.body || {}
96
- const allowed = signupAllowed({ invite, setup })
110
+ // "Also sign me in from this tailnet login": asked for on the sign-up
111
+ // page, never assumed. Kept with the ceremony as the login it was said for.
112
+ const wantsLink = req.body?.linkTailnet === true ? tailnet.identityOf(req)?.login || null : null
113
+ if (req.body?.linkTailnet === true && !wantsLink)
114
+ throw new HttpError(403, 'tailnet_required', 'Linking a tailnet login works only when you arrive through the tailnet.')
115
+ if (wantsLink && tailnetAccounts.userFor(wantsLink))
116
+ throw new HttpError(409, 'tailnet_login_taken', 'That tailnet login is linked to another account here.')
117
+ const allowed = signupAllowed({ invite, setup }, req)
97
118
  // Without an invite the account gets a workspace of its own: its name
98
119
  // can be picked now (the setup page asks for it).
99
120
  let ownWorkspace = null
@@ -113,7 +134,7 @@ export function mountAuthRoutes(authRouter, deps) {
113
134
  const { options, ceremony } = await auth.registrationOptions({
114
135
  handle: name,
115
136
  displayName,
116
- data: { ...allowed, ...(ownWorkspace ? { workspaceName: ownWorkspace } : {}) },
137
+ data: { ...allowed, ...(ownWorkspace ? { workspaceName: ownWorkspace } : {}), ...(wantsLink ? { linkTailnet: wantsLink } : {}) },
117
138
  })
118
139
  setCookie(res, CEREMONY_COOKIE, ceremony, 300)
119
140
  res.json({ options })
@@ -127,8 +148,16 @@ export function mountAuthRoutes(authRouter, deps) {
127
148
  limit('ceremony'),
128
149
  handle(async (req, res) => {
129
150
  const ip = clientIp(req)
151
+ // Signing up while arriving through the tailnet links the new account
152
+ // to that login only when the person asked for it on the sign-up page,
153
+ // and the request that finishes the sign-up still arrives as that login.
154
+ const identity = tailnet.identityOf(req)
130
155
  const { user, extra } = await auth.finishSignUp(ceremonyCookie(req), req.body?.response, (created, data) => {
131
156
  let workspaceId
157
+ const linked =
158
+ identity && data.linkTailnet === identity.login ? tailnetAccounts.link(created.id, identity.login, identity.userId) : null
159
+ if (linked)
160
+ auditAccount(db, { userId: created.id, action: 'tailnet_linked', ip, at: iso(), detail: { tailnet: linked, atSignUp: true } })
132
161
  // Ceremonies started before hashes were kept carry the raw token.
133
162
  const inviteHash = data.inviteHash || (data.invite ? sha256(data.invite) : null)
134
163
  const setupHash = data.setupHash || (data.setup ? sha256(data.setup) : null)
@@ -136,7 +165,7 @@ export function mountAuthRoutes(authRouter, deps) {
136
165
  const invite = findInviteByHash(db, inviteHash)
137
166
  if (!inviteUsable(invite, iso())) throw new AuthError(403, 'invalid_invite', 'This invite link is used, expired or revoked.')
138
167
  const t = tenant(db, invite.workspace_id)
139
- acceptInvite(t, invite, created.id, ip)
168
+ acceptInvite(t, invite, created.id, ip, { tailnetLogin: linked })
140
169
  workspaceId = invite.workspace_id
141
170
  } else {
142
171
  if (setupHash) {
@@ -153,7 +182,11 @@ export function mountAuthRoutes(authRouter, deps) {
153
182
  action: 'account_created',
154
183
  ip,
155
184
  at: iso(),
156
- detail: { via: inviteHash ? 'invite' : setupHash ? 'setup' : 'open', device: deviceLabel(req.headers['user-agent']) },
185
+ detail: {
186
+ via: inviteHash ? 'invite' : setupHash ? 'setup' : 'open',
187
+ device: deviceLabel(req.headers['user-agent']),
188
+ ...(linked ? { tailnet: linked } : {}),
189
+ },
157
190
  })
158
191
  return { workspaceId, recoveryCodes: auth.newRecoveryCodes(created.id), setup: Boolean(setupHash) }
159
192
  })
@@ -251,15 +284,30 @@ export function mountAuthRoutes(authRouter, deps) {
251
284
  '/logout',
252
285
  handle(async (req, res) => {
253
286
  const current = sessionOf(req)
254
- if (current) auth.deleteSession(current.session.hash)
287
+ const viaTailnet = current?.session.via === 'tailnet'
288
+ if (current) {
289
+ auth.deleteSession(current.session.hash)
290
+ auditAccount(db, {
291
+ userId: current.user.id,
292
+ action: 'signed_out',
293
+ ip: clientIp(req),
294
+ at: iso(),
295
+ detail: { device: deviceLabel(req.headers['user-agent']), ...(viaTailnet ? { via: 'tailnet' } : {}) },
296
+ })
297
+ }
255
298
  setCookie(res, SESSION_COOKIE, '', 0)
256
- res.json({ ok: true })
299
+ // The tailnet would sign this browser straight back in: it stays
300
+ // signed out until the person presses "Continue as ...".
301
+ if (viaTailnet) tailnet.markSignedOut(res)
302
+ res.json({ ok: true, ...(viaTailnet ? { tailnet: current.session.tailnet_login } : {}) })
257
303
  }),
258
304
  )
259
305
 
260
306
  authRouter.get(
261
307
  '/me',
262
308
  // Signed out is an answer here, not an error (the console asks on every page).
309
+ // A GET makes no session: a linked tailnet login is answered 'linked', and the client signs in with
310
+ // POST /auth/tailnet/sign-in (tailnet-routes.js).
263
311
  (req, res, next) =>
264
312
  sessionOf(req)
265
313
  ? next()
@@ -269,6 +317,9 @@ export function mountAuthRoutes(authRouter, deps) {
269
317
  rpId: siteOf(req).auth.rpId,
270
318
  ...(turnstile && signup === 'open' ? { turnstile: turnstile.siteKey } : {}),
271
319
  ...moveInfo(sites, req),
320
+ ...tailnet.anonInfo(req),
321
+ // Tailscale is the only sign-in here: no passkey is asked for or offered.
322
+ ...(passkeys.on() ? {} : { passkeysOff: true }),
272
323
  }),
273
324
  needSession,
274
325
  handle(async (req, res) => {
@@ -283,7 +334,15 @@ export function mountAuthRoutes(authRouter, deps) {
283
334
  recoveryCodesLeft: auth.recoveryRemaining(req.user.id),
284
335
  fresh: auth.isFresh(req.auth.session),
285
336
  viaRecovery: req.auth.session.via === 'recovery',
337
+ // How this session came in: passkey, recovery or tailnet.
338
+ via: req.auth.session.via,
339
+ ...tailnet.memberInfo(req),
286
340
  securityNotice: securityNoticeFor(req),
341
+ ...(passkeys.on() ? {} : { passkeysOff: true }),
342
+ // Passkeys a tailnet session added that are still on hold: shown on every page ("Not you? Remove it").
343
+ heldPasskeys: auth
344
+ .heldPasskeys(req.user.id, passkeys.holdsAt?.() || null)
345
+ .map((key) => ({ ...key, canRemove: req.auth.session.via === 'tailnet' })),
287
346
  signup,
288
347
  })
289
348
  }),
@@ -374,6 +433,7 @@ export function mountAuthRoutes(authRouter, deps) {
374
433
  revokeGrantsOf(req, user.id, null, { revokeTokens: true, t })
375
434
  }
376
435
  deleteAccountEvents(db, user.id)
436
+ deleteSavedViewsOf(db, user.id)
377
437
  q.run('DELETE FROM users WHERE id = :id', { id: user.id })
378
438
  auditAccount(db, {
379
439
  action: 'account_deleted',
@@ -436,7 +496,7 @@ export function mountAuthRoutes(authRouter, deps) {
436
496
  'This passkey was added with a recovery code; it can confirm this 24 hours after it was added. Use another passkey.',
437
497
  )
438
498
  }
439
- if (req.auth.session.via !== 'passkey') {
499
+ if (req.auth.session.via === 'recovery') {
440
500
  // A recovery session becomes a passkey session once a trusted passkey confirms it.
441
501
  q.run("UPDATE sessions SET via = 'passkey' WHERE token_hash = :hash", { hash: req.auth.session.hash })
442
502
  }
@@ -445,12 +505,49 @@ export function mountAuthRoutes(authRouter, deps) {
445
505
  }),
446
506
  )
447
507
 
508
+ // The first passkey of an account that its tailnet signs in is added with
509
+ // no passkey check. Two conditions on top of the 24 hour hold, both cheap
510
+ // for a person and both honest about what they are:
511
+ // - the request comes from a page of this site in a browser (the fetch
512
+ // metadata a browser sets and a page cannot change), and not from a
513
+ // command line session. A program on the person's device can forge
514
+ // those headers; this is not a barrier against it. It keeps the
515
+ // command line's own tailnet sessions and cross-site requests out.
516
+ // - the person confirmed a step that names the device ("Add a passkey
517
+ // from Chrome on Linux?"): the request carries that name, the server
518
+ // compares it with the device it sees, and the passkey is stored with
519
+ // it, so the notice on the person's other devices can say where the
520
+ // passkey came from.
521
+ // What protects the account is the hold, the notice on every page and
522
+ // "Not you? Remove it" from any of the person's tailnet devices.
523
+ function firstByTailnetOrThrow(req) {
524
+ if (!noPasskey(req)) return null
525
+ const device = deviceLabel(req.headers['user-agent'])
526
+ const inBrowser =
527
+ req.headers['sec-fetch-site'] === 'same-origin' &&
528
+ ['cors', 'same-origin'].includes(req.headers['sec-fetch-mode']) &&
529
+ req.headers['sec-fetch-dest'] === 'empty' &&
530
+ device !== 'Shapewall CLI'
531
+ if (!inBrowser) {
532
+ throw new AuthError(403, 'browser_required', 'A first passkey is added in a browser, on the Account page of this server.')
533
+ }
534
+ if (req.body?.confirmDevice !== device) {
535
+ throw new HttpError(
536
+ 409,
537
+ 'confirm_device',
538
+ 'Confirm adding a passkey from ' + device + '. It is your account’s first passkey: it will be on hold for 24 hours.',
539
+ { device },
540
+ )
541
+ }
542
+ return device
543
+ }
448
544
  authRouter.post(
449
545
  '/passkeys/options',
450
546
  limit('ceremony'),
451
547
  needSession,
452
548
  needFreshOrRecovery,
453
549
  handle(async (req, res) => {
550
+ firstByTailnetOrThrow(req)
454
551
  const { options, ceremony } = await auth.registrationOptions({ userId: req.user.id })
455
552
  setCookie(res, CEREMONY_COOKIE, ceremony, 300)
456
553
  res.json({ options })
@@ -462,9 +559,29 @@ export function mountAuthRoutes(authRouter, deps) {
462
559
  needSession,
463
560
  needFreshOrRecovery,
464
561
  handle(async (req, res) => {
465
- const viaRecovery = !auth.isFresh(req.auth.session)
466
- const { id } = await auth.finishAddPasskey(ceremonyCookie(req), req.body?.response, req.user.id, req.body?.nickname, { viaRecovery })
467
- auditAccount(db, { userId: req.user.id, action: 'passkey_added', ip: clientIp(req), at: iso(), detail: { viaRecovery } })
562
+ // On hold for 24 hours (it signs in at once, it cannot confirm the
563
+ // fixed core until then) when it was added without a passkey check: by
564
+ // a recovery-code session, or as the first passkey of an account that
565
+ // only its tailnet signs in. A tailnet identity is per device: whatever
566
+ // runs as that person on one of their devices could add a passkey of
567
+ // its own, and without the hold use it at once for everything.
568
+ const tailnetFrom = firstByTailnetOrThrow(req)
569
+ const firstByTailnet = Boolean(tailnetFrom)
570
+ const viaRecovery = (req.auth.session.via === 'recovery' || firstByTailnet) && !auth.isFresh(req.auth.session)
571
+ const { id } = await auth.finishAddPasskey(ceremonyCookie(req), req.body?.response, req.user.id, req.body?.nickname, {
572
+ viaRecovery,
573
+ tailnetFrom: firstByTailnet && viaRecovery ? tailnetFrom : null,
574
+ })
575
+ auditAccount(db, {
576
+ userId: req.user.id,
577
+ action: 'passkey_added',
578
+ ip: clientIp(req),
579
+ at: iso(),
580
+ detail: {
581
+ viaRecovery: viaRecovery && !firstByTailnet,
582
+ ...(firstByTailnet ? { viaTailnet: true, onHold: viaRecovery, device: tailnetFrom } : {}),
583
+ },
584
+ })
468
585
  setCookie(res, CEREMONY_COOKIE, '', 0)
469
586
  res.status(201).json({ id, passkeys: auth.listPasskeys(req.user.id) })
470
587
  }),
@@ -480,8 +597,32 @@ export function mountAuthRoutes(authRouter, deps) {
480
597
  authRouter.delete(
481
598
  '/passkeys/:id',
482
599
  needSession,
483
- needFresh,
600
+ needFreshOrHeldRemoval,
484
601
  handle(async (req, res) => {
602
+ // "Not you? Remove it": a tailnet session takes away a passkey that a
603
+ // tailnet session added and that is still on hold (guards.js).
604
+ if (!auth.isFresh(req.auth.session) && removesHeldPasskey(req, req.params.id)) {
605
+ const key = auth.removeHeldPasskey(req.user.id, req.params.id, passkeys.holdsAt?.() || null)
606
+ auth.deleteOtherSessions(req.user.id, req.auth.session.hash)
607
+ auditAccount(db, {
608
+ userId: req.user.id,
609
+ action: 'passkey_removed',
610
+ ip: clientIp(req),
611
+ at: iso(),
612
+ detail: {
613
+ otherSessionsEnded: true,
614
+ onHold: true,
615
+ via: 'tailnet',
616
+ device: deviceLabel(req.headers['user-agent']),
617
+ addedFrom: key.tailnet_added_from,
618
+ addedAt: key.created_at,
619
+ },
620
+ })
621
+ return res.json({
622
+ passkeys: auth.listPasskeys(req.user.id),
623
+ heldPasskeys: auth.heldPasskeys(req.user.id, passkeys.holdsAt?.() || null),
624
+ })
625
+ }
485
626
  auth.removePasskey(req.user.id, req.params.id)
486
627
  // A session signed in with that passkey must not outlive it.
487
628
  auth.deleteOtherSessions(req.user.id, req.auth.session.hash)
@@ -508,7 +649,7 @@ export function mountAuthRoutes(authRouter, deps) {
508
649
  authRouter.post(
509
650
  '/sessions/revoke-others',
510
651
  needSession,
511
- needFresh,
652
+ needPasskeyBacked,
512
653
  handle(async (req, res) => {
513
654
  auth.deleteOtherSessions(req.user.id, req.auth.session.hash)
514
655
  auditAccount(db, { userId: req.user.id, action: 'sessions_revoked', ip: clientIp(req), at: iso(), detail: {} })
@@ -526,7 +667,7 @@ export function mountAuthRoutes(authRouter, deps) {
526
667
  authRouter.delete(
527
668
  '/sessions/:id',
528
669
  needSession,
529
- needFresh,
670
+ needPasskeyBacked,
530
671
  handle(async (req, res) => {
531
672
  const id = String(req.params.id)
532
673
  if (!SESSION_ID_RE.test(id)) throw new AuthError(404, 'unknown_session', 'No such session.')