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.
- package/CHANGELOG.md +501 -0
- package/README.md +87 -4
- package/THIRD_PARTY.md +15 -0
- package/dist/agent/package-lock.json +594 -2
- package/dist/client/assets/BoardChat-WS8mAIW6.js +27 -0
- package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-B-Mlr6wF.js} +1 -1
- package/dist/client/assets/ChecksPanel-Dx1ZLBR6.js +1 -0
- package/dist/client/assets/History-sXhmpcvs.js +1 -0
- package/dist/client/assets/LocalSettings-CtGzz9F3.js +1 -0
- package/dist/client/assets/{Management-NVFDtSZB.js → Management-CGHe8Wt9.js} +1 -1
- package/dist/client/assets/QuickConnect-DEk84E4A.js +1 -0
- package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-CGGx-AYK.js} +2 -2
- package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-_bVp8hnC.js} +1 -1
- package/dist/client/assets/{circle-x-B6KfScOE.js → circle-x-BWoHA1QY.js} +1 -1
- package/dist/client/assets/index-Bm-Xd3YN.js +37 -0
- package/dist/client/assets/index-C7Ostk3K.css +2 -0
- package/dist/client/assets/play-BzQ3tRTv.js +1 -0
- package/dist/client/assets/{refresh-cw-vkgvCCfR.js → refresh-cw-BYBMyDKT.js} +1 -1
- package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
- package/dist/client/assets/ui-CAY6myRA.js +41 -0
- package/dist/client/build-info.json +1 -1
- package/dist/client/index.html +3 -3
- package/dist/client/sw.js +2 -2
- package/dist/console/assets/LocalAccountsApp-CWRguRRL.js +1 -0
- package/dist/console/assets/LocalConsole--I2SaIpr.js +2 -0
- package/dist/console/assets/LocalUsage-BmpCZ36k.js +1 -0
- package/dist/console/assets/app-update-dialog-B8M9plaT.js +1 -0
- package/dist/console/assets/app-version-muKrQqbL.js +1 -0
- package/dist/console/assets/console-CrhTsPbz.css +2 -0
- package/dist/console/assets/console-DxapQ9oS.js +11 -0
- package/dist/console/assets/local-agents-CoqD4dSN.js +6 -0
- package/dist/console/assets/minus-DDQ0ydcM.js +1 -0
- package/dist/console/assets/tailnet-C5Flwl_d.js +2 -0
- package/dist/console/assets/theme-DCd0qh4l.js +1 -0
- package/dist/console/assets/ui-Bry1mnCY.js +41 -0
- package/dist/console/assets/usage-CIPWnHaS.css +1 -0
- package/dist/console/assets/usage-CqVjMb7m.js +5 -0
- package/dist/console/assets/usage-D1G0GgE3.js +56 -0
- package/dist/console/build-info.json +1 -1
- package/dist/console/console.html +8 -2
- package/docs/agent-hardening.md +206 -0
- package/docs/agent-recipes.md +23 -0
- package/docs/agent-workflow.md +5 -0
- package/docs/agents.md +49 -6
- package/docs/architecture.md +10 -4
- package/docs/deployment.md +39 -17
- package/docs/design-system.md +56 -7
- package/docs/hosted.md +1800 -46
- 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/where-to-run.md +119 -3
- package/package.json +6 -1
- package/src/agent/backends/acp.js +745 -44
- package/src/agent/branch-mode.js +17 -3
- package/src/agent/card-hash.js +65 -0
- package/src/agent/context.js +41 -0
- package/src/agent/exec-paths.js +105 -0
- package/src/agent/routes.js +23 -2
- package/src/agent/run.js +11 -1
- package/src/agent/starter/cli.js +6 -1
- package/src/agent/turn-changes.js +70 -8
- package/src/agent/usage-log.js +251 -0
- package/src/cli/args.js +37 -0
- package/src/cli/commands.js +20 -5
- package/src/cli/help.js +73 -3
- package/src/cli/init.js +2 -1
- package/src/cli/serve.js +20 -5
- package/src/cli.js +8 -0
- package/src/hosted/access-cli.js +99 -1
- package/src/hosted/activity-core.js +232 -0
- package/src/hosted/activity.js +375 -0
- package/src/hosted/admin.js +87 -13
- package/src/hosted/agent-approvals.js +29 -4
- package/src/hosted/agent-link.js +1032 -0
- package/src/hosted/agent-policy.js +10 -3
- package/src/hosted/agent-presence.js +63 -22
- package/src/hosted/agent-routes.js +135 -0
- package/src/hosted/agents.js +1206 -0
- package/src/hosted/app.js +178 -12
- package/src/hosted/auth-routes.js +156 -15
- package/src/hosted/auth.js +123 -20
- package/src/hosted/board-routes.js +129 -5
- package/src/hosted/chat-frames.js +246 -0
- package/src/hosted/chat-job.js +204 -0
- package/src/hosted/chat-permissions.js +203 -0
- package/src/hosted/chat-routes.js +1009 -0
- package/src/hosted/chat-store.js +477 -0
- package/src/hosted/comment-files.js +11 -1
- package/src/hosted/comments.js +1 -1
- package/src/hosted/console-routes.js +122 -34
- package/src/hosted/db.js +295 -0
- package/src/hosted/device-routes.js +88 -10
- package/src/hosted/device.js +54 -4
- package/src/hosted/events.js +3 -3
- package/src/hosted/guards.js +337 -27
- package/src/hosted/http.js +6 -3
- package/src/hosted/membership.js +22 -3
- package/src/hosted/operator-access.js +418 -0
- package/src/hosted/operator.js +106 -2
- package/src/hosted/ops-cli.js +238 -3
- package/src/hosted/pages.js +4 -0
- package/src/hosted/passkey-policy.js +75 -0
- package/src/hosted/permissions.js +52 -6
- package/src/hosted/presence.js +31 -2
- package/src/hosted/projects.js +30 -2
- package/src/hosted/public.js +4 -4
- package/src/hosted/ratelimit.js +14 -0
- package/src/hosted/retention.js +18 -2
- package/src/hosted/server.js +123 -11
- package/src/hosted/shares.js +2 -2
- package/src/hosted/step-up-settings.js +68 -0
- package/src/hosted/step-up.js +135 -0
- package/src/hosted/tailnet-identity.js +24 -0
- package/src/hosted/tailnet-routes.js +342 -0
- package/src/hosted/tailnet.js +220 -0
- package/src/hosted/tailscale.js +75 -9
- package/src/hosted/tenancy.js +66 -1
- package/src/hosted/token-api.js +119 -20
- package/src/hosted/token-events.js +5 -3
- package/src/hosted/tokens.js +29 -6
- package/src/hosted/turn-hub.js +152 -0
- package/src/hosted/usage-core.js +624 -0
- package/src/hosted/usage-layout.js +724 -0
- package/src/hosted/usage-routes.js +375 -0
- package/src/hosted/usage-shape.js +256 -0
- package/src/hosted/usage.js +641 -0
- package/src/remote/agent-connect.js +469 -0
- package/src/remote/agent-fix.js +611 -0
- package/src/remote/agent-job.js +216 -0
- package/src/remote/agent-link.js +906 -0
- package/src/remote/agent-policy-file.js +242 -0
- package/src/remote/credentials.js +35 -1
- package/src/remote/login.js +44 -9
- package/src/remote/share.js +1 -1
- package/src/remote/tailnet-cli.js +547 -0
- package/src/server/app.js +15 -0
- package/src/server/pairing-routes.js +34 -5
- package/src/server/tailscale.js +97 -4
- package/src/tailnet-proof.js +766 -0
- package/dist/client/assets/BoardChat-DxehB761.js +0 -27
- package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
- package/dist/client/assets/History-C9N32kCv.js +0 -1
- package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
- package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
- package/dist/client/assets/index-CsqLdqGx.css +0 -2
- package/dist/client/assets/index-DnZarJ3U.js +0 -37
- package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
- package/dist/client/assets/ui-DI-FGWWK.js +0 -41
- package/dist/console/assets/LocalAccountsApp-VOCZg8dg.js +0 -1
- package/dist/console/assets/LocalConsole-CMwAW-mF.js +0 -1
- package/dist/console/assets/app-update-dialog-Cw6UDqNP.js +0 -1
- package/dist/console/assets/console-DgfWmUEx.js +0 -55
- package/dist/console/assets/console-Lis0vvdL.css +0 -2
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// Tailnet sign-in, the routes (src/hosted/tailnet.js has the rules, and
|
|
2
|
+
// src/hosted/tailnet-identity.js the trust in the headers).
|
|
3
|
+
//
|
|
4
|
+
// GET /auth/me (auth-routes.js asks the helpers here; never makes a session)
|
|
5
|
+
// POST /auth/tailnet/sign-in the only way a linked login gets a session: the console and the
|
|
6
|
+
// CLI send it when /auth/me says 'linked', a person after a sign-out
|
|
7
|
+
// POST /auth/tailnet/join a first visit: the account, and its invites
|
|
8
|
+
// POST /auth/tailnet/link link my account to the login I arrive with
|
|
9
|
+
// DELETE /auth/tailnet/link unlink it
|
|
10
|
+
// GET /api/workspaces/:ws/tailnet-visitors logins that arrived and belong to nobody
|
|
11
|
+
// DELETE /api/workspaces/:ws/tailnet-visitors/:login take one off the list
|
|
12
|
+
//
|
|
13
|
+
// The identity headers are ambient: the proxy adds them to every request a
|
|
14
|
+
// tailnet device makes, also one a page on another site made it send. So
|
|
15
|
+
// the identity alone only ever does two things, and both need a request
|
|
16
|
+
// from this origin: answer GET /auth/me (Sec-Fetch-Site: same-origin) and
|
|
17
|
+
// the POSTs above (the write guard: Origin or Sec-Fetch-Site, and JSON).
|
|
18
|
+
// Everything else needs the session cookie as before, and for a tailnet
|
|
19
|
+
// session the identity as well.
|
|
20
|
+
|
|
21
|
+
import { deviceLabel } from './auth.js'
|
|
22
|
+
import { parseCookies } from './cookies.js'
|
|
23
|
+
import { sha256 } from './crypto.js'
|
|
24
|
+
import { transaction } from './db.js'
|
|
25
|
+
import { HttpError } from './http.js'
|
|
26
|
+
import { NEEDS } from './permissions.js'
|
|
27
|
+
import { removeSetupLink } from './setup-link.js'
|
|
28
|
+
import { cleanTailnetLogin } from './tailnet-identity.js'
|
|
29
|
+
import { tailnetOutCookie } from './tailnet.js'
|
|
30
|
+
import { auditAccount, findInviteByHash, findInvitesForTailnetLogin, inviteUsable, roleIn, seenDevice, tenant } from './tenancy.js'
|
|
31
|
+
|
|
32
|
+
const OUT_DAYS = 30
|
|
33
|
+
// Live tailnet sessions an account keeps per login (each browser and each CLI run is one).
|
|
34
|
+
export const TAILNET_SESSIONS = 20
|
|
35
|
+
const OWN_MACHINE =
|
|
36
|
+
'This is the server’s own machine: a tailnet identity is never taken from here, because any program on this machine could ask the same way. Use a passkey, or another tailnet device.'
|
|
37
|
+
|
|
38
|
+
// What the server's operator runs when a person's own login stopped signing them in because Tailscale gave
|
|
39
|
+
// them a new user id (src/hosted/operator-access.js relinkMember). The login is one the server made safe.
|
|
40
|
+
const relinkCommand = (login) => `shapewall hosted admin members relink --login ${login} --data <the server's data folder>`
|
|
41
|
+
const userChanged = (login) =>
|
|
42
|
+
'This tailnet login is linked to an account here for a different tailnet user than the one asking (the name was given to someone else, it comes from another tailnet, or Tailscale gave you a new user id). ' +
|
|
43
|
+
'If the account is yours, ask whoever runs this server to run: ' +
|
|
44
|
+
relinkCommand(login)
|
|
45
|
+
|
|
46
|
+
export function mountTailnetAuth(authRouter, deps) {
|
|
47
|
+
const {
|
|
48
|
+
db,
|
|
49
|
+
dataDir,
|
|
50
|
+
auth,
|
|
51
|
+
q,
|
|
52
|
+
iso,
|
|
53
|
+
signup,
|
|
54
|
+
secure,
|
|
55
|
+
tailnet,
|
|
56
|
+
tailnetAccounts,
|
|
57
|
+
SESSION_COOKIE,
|
|
58
|
+
CEREMONY_COOKIE,
|
|
59
|
+
clientIp,
|
|
60
|
+
setCookie,
|
|
61
|
+
limit,
|
|
62
|
+
handle,
|
|
63
|
+
sessionOf,
|
|
64
|
+
needSession,
|
|
65
|
+
needFresh,
|
|
66
|
+
publicUser,
|
|
67
|
+
createWorkspace,
|
|
68
|
+
acceptInvite,
|
|
69
|
+
cutTailnetSessions = () => {},
|
|
70
|
+
} = deps
|
|
71
|
+
const OUT_COOKIE = tailnetOutCookie(secure)
|
|
72
|
+
const on = Boolean(tailnet)
|
|
73
|
+
const identityOf = (req) => (on ? tailnet.identityOf(req) : null)
|
|
74
|
+
const sameOrigin = (req) => req.headers['sec-fetch-site'] === 'same-origin'
|
|
75
|
+
const signedOutHere = (req) => parseCookies(req.headers.cookie)[OUT_COOKIE] === '1'
|
|
76
|
+
const usableInvites = (login) => findInvitesForTailnetLogin(db, login).filter((row) => inviteUsable(row, iso()))
|
|
77
|
+
|
|
78
|
+
function arrived(req) {
|
|
79
|
+
const identity = identityOf(req)
|
|
80
|
+
if (!identity && on && tailnet.ownMachine?.(req)) throw new HttpError(403, 'tailnet_own_machine', OWN_MACHINE)
|
|
81
|
+
if (!identity)
|
|
82
|
+
throw new HttpError(
|
|
83
|
+
403,
|
|
84
|
+
'tailnet_required',
|
|
85
|
+
on
|
|
86
|
+
? 'This works only when you arrive through the tailnet, from a device that belongs to a person.'
|
|
87
|
+
: 'Tailnet sign-in is off on this server.',
|
|
88
|
+
)
|
|
89
|
+
return identity
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// A session for the account linked to this identity. Sets the cookie and
|
|
93
|
+
// req.auth; the browser's earlier session (any account) ends.
|
|
94
|
+
function signIn(req, res, user, identity) {
|
|
95
|
+
const previous = sessionOf(req)
|
|
96
|
+
if (previous) auth.deleteSession(previous.session.hash)
|
|
97
|
+
const device = deviceLabel(req.headers['user-agent'])
|
|
98
|
+
const ip = clientIp(req)
|
|
99
|
+
const token = auth.createSession(user.id, { via: 'tailnet', tailnetLogin: identity.login, device, ip })
|
|
100
|
+
// A client that signs in again and again (a script in a loop) never piles up sessions: an account keeps its
|
|
101
|
+
// newest TAILNET_SESSIONS for a login, and the oldest beyond that end.
|
|
102
|
+
q.run(
|
|
103
|
+
`DELETE FROM sessions WHERE token_hash IN (
|
|
104
|
+
SELECT token_hash FROM sessions WHERE user_id = :user AND tailnet_login = :login
|
|
105
|
+
ORDER BY last_seen_at DESC, created_at DESC LIMIT -1 OFFSET :keep)`,
|
|
106
|
+
{ user: user.id, login: identity.login, keep: TAILNET_SESSIONS },
|
|
107
|
+
)
|
|
108
|
+
setCookie(res, SESSION_COOKIE, token, 30 * 86400)
|
|
109
|
+
setCookie(res, CEREMONY_COOKIE, '', 0)
|
|
110
|
+
setCookie(res, OUT_COOKIE, '', 0)
|
|
111
|
+
auditAccount(db, {
|
|
112
|
+
userId: user.id,
|
|
113
|
+
action: 'signed_in',
|
|
114
|
+
ip,
|
|
115
|
+
at: iso(),
|
|
116
|
+
detail: { via: 'tailnet', tailnet: identity.login, device, ...(seenDevice(db, user.id, device) ? {} : { newDevice: true }) },
|
|
117
|
+
})
|
|
118
|
+
req.sessionChecked = true
|
|
119
|
+
req.auth = auth.readSession(token)
|
|
120
|
+
return req.auth
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function anonInfo(req) {
|
|
124
|
+
if (!on) return {}
|
|
125
|
+
const identity = sameOrigin(req) ? identityOf(req) : null
|
|
126
|
+
// Tailnet sign-in is on, but this request names nobody: a tagged
|
|
127
|
+
// device, a shared node without a user, or not through the tailnet.
|
|
128
|
+
// From the server's own machine: never signed in by identity (any program there could ask the same way).
|
|
129
|
+
if (!identity && sameOrigin(req) && tailnet.ownMachine?.(req)) return { tailnet: { state: 'self' } }
|
|
130
|
+
if (!identity) return { tailnet: { state: 'none' } }
|
|
131
|
+
// The login is linked here, but for another tailnet user than this one: nobody, and the page says why.
|
|
132
|
+
if (tailnetAccounts.heldByAnother(identity))
|
|
133
|
+
return { tailnet: { state: 'changed', login: identity.login, fix: relinkCommand(identity.login) } }
|
|
134
|
+
// A linked login: GET never signs in (it makes no session). 'linked' says the client may sign in now with
|
|
135
|
+
// POST /auth/tailnet/sign-in; 'signed_out' says this browser signed out and waits for "Continue as ...".
|
|
136
|
+
if (tailnetAccounts.userFor(identity.login))
|
|
137
|
+
return { tailnet: { state: signedOutHere(req) ? 'signed_out' : 'linked', login: identity.login, name: identity.name } }
|
|
138
|
+
const invites = usableInvites(identity.login).map((row) => ({ workspace: row.workspace_name, role: row.role }))
|
|
139
|
+
// Listed for the admins until someone invites it (or it stops coming).
|
|
140
|
+
if (!invites.length) tailnetAccounts.noteVisitor(identity)
|
|
141
|
+
return {
|
|
142
|
+
tailnet: {
|
|
143
|
+
state: invites.length ? 'invited' : 'unknown',
|
|
144
|
+
login: identity.login,
|
|
145
|
+
name: identity.name,
|
|
146
|
+
invites,
|
|
147
|
+
suggested: tailnetAccounts.suggestion(identity),
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// For the signed-in answer of GET /auth/me.
|
|
152
|
+
function memberInfo(req) {
|
|
153
|
+
if (!on) return { tailnet: null }
|
|
154
|
+
const identity = sameOrigin(req) ? identityOf(req) : null
|
|
155
|
+
const linked = req.user.tailnet_login || null
|
|
156
|
+
return {
|
|
157
|
+
tailnet: {
|
|
158
|
+
// The login this request arrives with (null: not through the tailnet).
|
|
159
|
+
here: identity?.login || null,
|
|
160
|
+
// This browser is on the server's own machine: its tailnet identity is never taken.
|
|
161
|
+
ownMachine: Boolean(sameOrigin(req) && tailnet.ownMachine?.(req)),
|
|
162
|
+
linked,
|
|
163
|
+
linkedAt: req.user.tailnet_linked_at || null,
|
|
164
|
+
// Invites made for this login that the account has not taken yet.
|
|
165
|
+
invites:
|
|
166
|
+
identity && linked === identity.login
|
|
167
|
+
? usableInvites(linked)
|
|
168
|
+
.filter((row) => !roleIn(db, req.user.id, row.workspace_id))
|
|
169
|
+
.map((row) => ({ workspace: row.workspace_name, role: row.role }))
|
|
170
|
+
: [],
|
|
171
|
+
},
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Its own bucket, per login: the console sends it on arrival, so it must not use up the passkey ceremonies of
|
|
176
|
+
// everyone behind the same proxy address.
|
|
177
|
+
authRouter.post(
|
|
178
|
+
'/tailnet/sign-in',
|
|
179
|
+
limit('tailnetSignIn', (req) => identityOf(req)?.login || clientIp(req)),
|
|
180
|
+
handle(async (req, res) => {
|
|
181
|
+
const identity = arrived(req)
|
|
182
|
+
if (tailnetAccounts.heldByAnother(identity)) throw new HttpError(403, 'tailnet_user_changed', userChanged(identity.login))
|
|
183
|
+
const user = tailnetAccounts.accountOf(identity)
|
|
184
|
+
if (!user) throw new HttpError(404, 'no_account', `No account here is linked to ${identity.login}.`)
|
|
185
|
+
signIn(req, res, user, identity)
|
|
186
|
+
res.json({ user: publicUser(user) })
|
|
187
|
+
}),
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
// A first visit (no account for this login yet): makes the account and
|
|
191
|
+
// joins by an invite link, the setup link, the invites made for the login,
|
|
192
|
+
// or open sign-up. With a session of the linked account: takes the
|
|
193
|
+
// invites made for the login.
|
|
194
|
+
authRouter.post(
|
|
195
|
+
'/tailnet/join',
|
|
196
|
+
limit('ceremony'),
|
|
197
|
+
handle(async (req, res) => {
|
|
198
|
+
const identity = arrived(req)
|
|
199
|
+
// The command line may say who it expects to be (`shapewall hosted claim
|
|
200
|
+
// --login`): a flag never names the login, it can only refuse another.
|
|
201
|
+
const expected = req.body?.expectLogin
|
|
202
|
+
if (expected !== undefined && expected !== null && cleanTailnetLogin(expected) !== identity.login)
|
|
203
|
+
throw new HttpError(409, 'other_login', `You arrive as ${identity.login}, not as the login that was named.`)
|
|
204
|
+
const ip = clientIp(req)
|
|
205
|
+
const current = sessionOf(req)
|
|
206
|
+
if (current) {
|
|
207
|
+
if (current.user.tailnet_login !== identity.login)
|
|
208
|
+
throw new HttpError(409, 'not_linked', 'Your account is not linked to the tailnet login you arrive with.')
|
|
209
|
+
const joined = transaction(db, () =>
|
|
210
|
+
usableInvites(identity.login)
|
|
211
|
+
.filter((row) => !roleIn(db, current.user.id, row.workspace_id))
|
|
212
|
+
.map((row) => {
|
|
213
|
+
acceptInvite(tenant(db, row.workspace_id), row, current.user.id, ip, { tailnetLogin: identity.login })
|
|
214
|
+
return row.workspace_id
|
|
215
|
+
}),
|
|
216
|
+
)
|
|
217
|
+
return res.json({ joined, workspaceId: joined[0] || null })
|
|
218
|
+
}
|
|
219
|
+
if (tailnetAccounts.heldByAnother(identity)) throw new HttpError(403, 'tailnet_user_changed', userChanged(identity.login))
|
|
220
|
+
if (tailnetAccounts.userFor(identity.login))
|
|
221
|
+
throw new HttpError(409, 'has_account', `${identity.login} already has an account here. Continue as that account instead.`)
|
|
222
|
+
const { invite, setup, handle: wantedHandle, displayName, workspaceName } = req.body || {}
|
|
223
|
+
const made = transaction(db, () => {
|
|
224
|
+
let invites = []
|
|
225
|
+
let how = 'invite'
|
|
226
|
+
if (typeof invite === 'string' && invite) {
|
|
227
|
+
const row = findInviteByHash(db, sha256(invite))
|
|
228
|
+
if (!inviteUsable(row, iso()) || (row.tailnet_login && row.tailnet_login !== identity.login))
|
|
229
|
+
throw new HttpError(403, 'invalid_invite', 'This invite link is used, expired or revoked.')
|
|
230
|
+
invites = [row]
|
|
231
|
+
} else if (typeof setup === 'string' && setup) {
|
|
232
|
+
const used = q.run('UPDATE setup_codes SET used_at = :at WHERE code_hash = :hash AND used_at IS NULL AND expires_at > :at', {
|
|
233
|
+
at: iso(),
|
|
234
|
+
hash: sha256(setup),
|
|
235
|
+
})
|
|
236
|
+
if (!used.changes) throw new HttpError(403, 'invalid_setup', 'This setup link is used or expired.')
|
|
237
|
+
how = 'setup'
|
|
238
|
+
} else {
|
|
239
|
+
invites = usableInvites(identity.login)
|
|
240
|
+
if (!invites.length) {
|
|
241
|
+
if (signup !== 'open')
|
|
242
|
+
throw new HttpError(403, 'invite_required', `Ask an admin of a workspace here to invite ${identity.login}.`)
|
|
243
|
+
how = 'open'
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const user = tailnetAccounts.createUser(identity, { handle: wantedHandle, displayName })
|
|
247
|
+
let workspaceId
|
|
248
|
+
if (invites.length) {
|
|
249
|
+
for (const row of invites) acceptInvite(tenant(db, row.workspace_id), row, user.id, ip, { tailnetLogin: identity.login })
|
|
250
|
+
workspaceId = invites[0].workspace_id
|
|
251
|
+
} else workspaceId = createWorkspace(user, workspaceName || `${user.display_name}'s workspace`, ip)
|
|
252
|
+
auditAccount(db, {
|
|
253
|
+
userId: user.id,
|
|
254
|
+
action: 'account_created',
|
|
255
|
+
ip,
|
|
256
|
+
at: iso(),
|
|
257
|
+
detail: { via: how, signIn: 'tailnet', tailnet: identity.login, device: deviceLabel(req.headers['user-agent']) },
|
|
258
|
+
})
|
|
259
|
+
return { user, workspaceId, setup: how === 'setup' }
|
|
260
|
+
})
|
|
261
|
+
if (made.setup) removeSetupLink(dataDir)
|
|
262
|
+
signIn(req, res, made.user, identity)
|
|
263
|
+
res.status(201).json({ user: publicUser(made.user), workspaceId: made.workspaceId })
|
|
264
|
+
}),
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
// Linking adds a way to sign in to the account, so it is part of the
|
|
268
|
+
// fixed core: a fresh passkey check, like adding a passkey.
|
|
269
|
+
authRouter.post(
|
|
270
|
+
'/tailnet/link',
|
|
271
|
+
needSession,
|
|
272
|
+
needFresh,
|
|
273
|
+
handle(async (req, res) => {
|
|
274
|
+
const identity = arrived(req)
|
|
275
|
+
const login = tailnetAccounts.link(req.user.id, identity.login, identity.userId)
|
|
276
|
+
auditAccount(db, { userId: req.user.id, action: 'tailnet_linked', ip: clientIp(req), at: iso(), detail: { tailnet: login } })
|
|
277
|
+
res.json({ linked: login })
|
|
278
|
+
}),
|
|
279
|
+
)
|
|
280
|
+
// Unlinking takes one away: the same check. An account with no passkey
|
|
281
|
+
// would be locked out, so it adds one first.
|
|
282
|
+
authRouter.delete(
|
|
283
|
+
'/tailnet/link',
|
|
284
|
+
needSession,
|
|
285
|
+
(req, res, next) => {
|
|
286
|
+
if (auth.passkeyCount(req.user.id) > 0) return next()
|
|
287
|
+
res.status(409).json({
|
|
288
|
+
error: { code: 'no_passkey', message: 'Your tailnet login is the only way into this account. Add a passkey first, then unlink.' },
|
|
289
|
+
})
|
|
290
|
+
},
|
|
291
|
+
needFresh,
|
|
292
|
+
handle(async (req, res) => {
|
|
293
|
+
const viaTailnet = req.auth.session.via === 'tailnet'
|
|
294
|
+
const login = tailnetAccounts.unlink(req.user.id)
|
|
295
|
+
// What those sessions still had open (live streams) ends with them.
|
|
296
|
+
if (login) cutTailnetSessions(req.user.id)
|
|
297
|
+
if (login)
|
|
298
|
+
auditAccount(db, { userId: req.user.id, action: 'tailnet_unlinked', ip: clientIp(req), at: iso(), detail: { tailnet: login } })
|
|
299
|
+
if (viaTailnet) setCookie(res, SESSION_COOKIE, '', 0)
|
|
300
|
+
res.json({ linked: null, signedOut: viaTailnet })
|
|
301
|
+
}),
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
// Sign-out of a tailnet session: this browser stays signed out until
|
|
305
|
+
// "Continue as ..." (POST /auth/tailnet/sign-in), or a passkey sign-in.
|
|
306
|
+
const markSignedOut = (res) => setCookie(res, OUT_COOKIE, '1', OUT_DAYS * 86400)
|
|
307
|
+
const clearSignedOut = (res) => setCookie(res, OUT_COOKIE, '', 0)
|
|
308
|
+
|
|
309
|
+
return { on, identityOf, anonInfo, memberInfo, markSignedOut, clearSignedOut }
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// The admins' side: who arrived and belongs to nobody. Inviting one is an
|
|
313
|
+
// invite with `tailnetLogin` (console-routes.js); this is the list.
|
|
314
|
+
export function mountTailnetAdmin(consoleApi, deps) {
|
|
315
|
+
const { tailnet, tailnetAccounts, iso, handle, inWorkspace, auditOf } = deps
|
|
316
|
+
consoleApi.get(
|
|
317
|
+
'/workspaces/:ws/tailnet-visitors',
|
|
318
|
+
inWorkspace(NEEDS.invites),
|
|
319
|
+
handle(async (req, res) => {
|
|
320
|
+
if (!tailnet) return res.json({ enabled: false, visitors: [] })
|
|
321
|
+
const invited = new Set(
|
|
322
|
+
req.t
|
|
323
|
+
.all(
|
|
324
|
+
'SELECT tailnet_login FROM invites WHERE workspace_id = :ws AND tailnet_login IS NOT NULL AND used_at IS NULL AND revoked_at IS NULL AND expires_at > :now',
|
|
325
|
+
{ now: iso() },
|
|
326
|
+
)
|
|
327
|
+
.map((row) => row.tailnet_login),
|
|
328
|
+
)
|
|
329
|
+
res.json({ enabled: true, visitors: tailnetAccounts.visitors().filter((visitor) => !invited.has(visitor.login)) })
|
|
330
|
+
}),
|
|
331
|
+
)
|
|
332
|
+
consoleApi.delete(
|
|
333
|
+
'/workspaces/:ws/tailnet-visitors/:login',
|
|
334
|
+
inWorkspace(NEEDS.invites),
|
|
335
|
+
handle(async (req, res) => {
|
|
336
|
+
const login = cleanTailnetLogin(req.params.login)
|
|
337
|
+
if (!login || !tailnetAccounts.forgetVisitor(login)) throw new HttpError(404, 'unknown_visitor', 'No such visitor.')
|
|
338
|
+
auditOf(req, 'tailnet_visitor_dismissed', null, { tailnet: login })
|
|
339
|
+
res.json({ ok: true })
|
|
340
|
+
}),
|
|
341
|
+
)
|
|
342
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Tailnet sign-in, the account side (the trust in the headers is
|
|
2
|
+
// src/hosted/tailnet-identity.js): each tailnet login is linked to one
|
|
3
|
+
// account, so roles, audiences, the audit log and usage work as for any
|
|
4
|
+
// other account.
|
|
5
|
+
//
|
|
6
|
+
// Being on the tailnet alone gives nothing. A tailnet holds more than a
|
|
7
|
+
// team: family devices, a contractor's laptop, nodes shared in from another
|
|
8
|
+
// tailnet. So an admin still decides who is in a workspace and with which
|
|
9
|
+
// role, and every action is some one person's:
|
|
10
|
+
//
|
|
11
|
+
// - a login that is linked to an account is signed in as that account;
|
|
12
|
+
// - a login nobody knows sees who to ask, and is listed for the admins
|
|
13
|
+
// (tailnet_visitors) until one of them invites it;
|
|
14
|
+
// - an invite can be made for a login; that login joins with the invite's
|
|
15
|
+
// role, and nobody else can use it;
|
|
16
|
+
// - the first account on a new server is still made with the setup link.
|
|
17
|
+
//
|
|
18
|
+
// A session that came in this way is bound to the identity: it counts only
|
|
19
|
+
// on a request that carries the same login, proven the same way. Leave the
|
|
20
|
+
// tailnet, or lose the device, and its cookie is worth nothing elsewhere.
|
|
21
|
+
|
|
22
|
+
import { HANDLE_RE, cleanDisplayName } from './auth.js'
|
|
23
|
+
import { newId, randomToken } from './crypto.js'
|
|
24
|
+
import { transaction } from './db.js'
|
|
25
|
+
import { HttpError } from './http.js'
|
|
26
|
+
import { accounts } from './tenancy.js'
|
|
27
|
+
import { cleanTailnetLogin } from './tailnet-identity.js'
|
|
28
|
+
|
|
29
|
+
export const TAILNET_VISITORS_MAX = 200
|
|
30
|
+
export const TAILNET_VISITOR_DAYS = 30
|
|
31
|
+
const DAY = 86_400_000
|
|
32
|
+
|
|
33
|
+
// The cookie a tailnet sign-out leaves, so that the next page does not sign
|
|
34
|
+
// the person straight back in: they press "Continue as ..." for that.
|
|
35
|
+
export const tailnetOutCookie = (secure) => (secure ? '__Host-sw_tailnet_out' : 'sw_tailnet_out')
|
|
36
|
+
|
|
37
|
+
// (req, token) -> { user, session } | null. A tailnet session needs the
|
|
38
|
+
// request to carry the identity it was made for, and the account to be
|
|
39
|
+
// linked to it still. With tailnet sign-in off no such session counts.
|
|
40
|
+
// `passkeys` ({ on() }, src/hosted/passkey-policy.js): on a server where
|
|
41
|
+
// passkeys are off, a session that a passkey or a recovery code started
|
|
42
|
+
// (from before they were switched off) does not count.
|
|
43
|
+
export function sessionReader({ auth, tailnet = null, passkeys = null }) {
|
|
44
|
+
return (req, token) => {
|
|
45
|
+
const found = auth.readSession(token)
|
|
46
|
+
if (!found) return null
|
|
47
|
+
const bound = found.session.tailnet_login
|
|
48
|
+
if (!bound) return passkeys && !passkeys.on() ? null : found
|
|
49
|
+
const identity = tailnet ? tailnet.identityOf(req) : null
|
|
50
|
+
if (!identity || identity.login !== bound || found.user.tailnet_login !== bound) return null
|
|
51
|
+
// And the same tailnet user as the one the account is held to, not only the same login string.
|
|
52
|
+
if (!identity.userId || found.user.tailnet_user_id !== identity.userId) return null
|
|
53
|
+
return found
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A handle from a tailnet login: "ada.lovelace@example.com" -> "ada-lovelace".
|
|
58
|
+
export function handleFromLogin(login) {
|
|
59
|
+
let base = String(login || '')
|
|
60
|
+
.split('@')[0]
|
|
61
|
+
.toLowerCase()
|
|
62
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
63
|
+
.replace(/^-+|-+$/g, '')
|
|
64
|
+
if (!/^[a-z]/.test(base)) base = `u-${base}`
|
|
65
|
+
base = base.replace(/-+$/g, '').slice(0, 28)
|
|
66
|
+
while (base.length < 3) base += 'x'
|
|
67
|
+
return base
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createTailnetAccounts({ db, iso, now }) {
|
|
71
|
+
const q = accounts(db)
|
|
72
|
+
|
|
73
|
+
// The account a login string is linked to (for "is this login taken").
|
|
74
|
+
const userFor = (login) => q.get('SELECT * FROM users WHERE tailnet_login = :login', { login }) || null
|
|
75
|
+
|
|
76
|
+
// The account a proven identity is: linked to its login AND held to the
|
|
77
|
+
// same tailnet user. A login is a name (ada@example.com); a tailnet can
|
|
78
|
+
// give that name to someone else later, or a node shared in from another
|
|
79
|
+
// tailnet can carry a login that reads the same. Tailscale's own id of the
|
|
80
|
+
// user does not move with the name, so an account keeps the id it was
|
|
81
|
+
// linked with and a later request must carry the same one. An account
|
|
82
|
+
// linked before the id was kept (or by `hosted claim --login`, which knows
|
|
83
|
+
// only the name) takes the id of the first proven request that arrives as
|
|
84
|
+
// its login.
|
|
85
|
+
function accountOf(identity) {
|
|
86
|
+
const user = identity?.login ? userFor(identity.login) : null
|
|
87
|
+
if (!user || !identity.userId) return null
|
|
88
|
+
if (!user.tailnet_user_id) {
|
|
89
|
+
q.run('UPDATE users SET tailnet_user_id = :tid WHERE id = :id AND tailnet_user_id IS NULL', { tid: identity.userId, id: user.id })
|
|
90
|
+
return { ...user, tailnet_user_id: identity.userId }
|
|
91
|
+
}
|
|
92
|
+
return user.tailnet_user_id === identity.userId ? user : null
|
|
93
|
+
}
|
|
94
|
+
// The login is linked here, but to another tailnet user than this one.
|
|
95
|
+
const heldByAnother = (identity) => Boolean(identity?.login && userFor(identity.login)) && !accountOf(identity)
|
|
96
|
+
|
|
97
|
+
function freeHandle(wanted) {
|
|
98
|
+
for (let n = 1; n < 1000; n += 1) {
|
|
99
|
+
const handle = n === 1 ? wanted : `${wanted.slice(0, 28)}-${n}`
|
|
100
|
+
if (HANDLE_RE.test(handle) && !q.get('SELECT id FROM users WHERE handle = :handle', { handle })) return handle
|
|
101
|
+
}
|
|
102
|
+
return `u-${randomToken(6)
|
|
103
|
+
.toLowerCase()
|
|
104
|
+
.replace(/[^a-z0-9]/g, 'x')}`
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Links an account to a login. One account per login, one login per account.
|
|
108
|
+
function link(userId, login, tailnetUserId = null) {
|
|
109
|
+
login = cleanTailnetLogin(login)
|
|
110
|
+
if (!login) throw new HttpError(400, 'invalid_login', 'That is not a tailnet login.')
|
|
111
|
+
return transaction(db, () => {
|
|
112
|
+
const holder = userFor(login)
|
|
113
|
+
if (holder && holder.id !== userId)
|
|
114
|
+
throw new HttpError(409, 'tailnet_login_taken', 'That tailnet login is linked to another account here.')
|
|
115
|
+
q.run('UPDATE users SET tailnet_login = :login, tailnet_linked_at = :at, tailnet_user_id = :tid WHERE id = :id', {
|
|
116
|
+
login,
|
|
117
|
+
at: iso(),
|
|
118
|
+
tid: tailnetUserId ? String(tailnetUserId) : null,
|
|
119
|
+
id: userId,
|
|
120
|
+
})
|
|
121
|
+
q.run('DELETE FROM tailnet_visitors WHERE login = :login', { login })
|
|
122
|
+
return login
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Unlinks, and ends the sessions that came in by that identity.
|
|
127
|
+
function unlink(userId) {
|
|
128
|
+
return transaction(db, () => {
|
|
129
|
+
const user = q.get('SELECT tailnet_login FROM users WHERE id = :id', { id: userId })
|
|
130
|
+
if (!user?.tailnet_login) return null
|
|
131
|
+
q.run('UPDATE users SET tailnet_login = NULL, tailnet_linked_at = NULL, tailnet_user_id = NULL WHERE id = :id', { id: userId })
|
|
132
|
+
q.run('DELETE FROM sessions WHERE user_id = :user AND tailnet_login IS NOT NULL', { user: userId })
|
|
133
|
+
return user.tailnet_login
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// A new account for a login that has none: no passkey, no recovery codes
|
|
138
|
+
// (the tailnet signs it in; a passkey can be added later). The handle and
|
|
139
|
+
// name come from the request when given, else from the identity.
|
|
140
|
+
function createUser(identity, { handle = null, displayName = null } = {}) {
|
|
141
|
+
return transaction(db, () => {
|
|
142
|
+
if (userFor(identity.login)) throw new HttpError(409, 'tailnet_login_taken', 'That tailnet login already has an account here.')
|
|
143
|
+
let name
|
|
144
|
+
try {
|
|
145
|
+
name = cleanDisplayName(displayName || identity.name)
|
|
146
|
+
} catch {
|
|
147
|
+
name = identity.login.split('@')[0].slice(0, 60)
|
|
148
|
+
}
|
|
149
|
+
let wanted = typeof handle === 'string' && handle.trim() ? handle.trim().toLowerCase() : null
|
|
150
|
+
if (wanted) {
|
|
151
|
+
if (!HANDLE_RE.test(wanted))
|
|
152
|
+
throw new HttpError(400, 'invalid_handle', 'Handle: 3 to 32 lowercase letters, digits and dashes, starting with a letter.')
|
|
153
|
+
if (q.get('SELECT id FROM users WHERE handle = :handle', { handle: wanted }))
|
|
154
|
+
throw new HttpError(409, 'handle_taken', 'That handle is taken.')
|
|
155
|
+
} else wanted = freeHandle(handleFromLogin(identity.login))
|
|
156
|
+
const user = {
|
|
157
|
+
id: newId('us'),
|
|
158
|
+
handle: wanted,
|
|
159
|
+
display_name: name,
|
|
160
|
+
webauthn_user_id: randomToken(32),
|
|
161
|
+
created_at: iso(),
|
|
162
|
+
tailnet_login: identity.login,
|
|
163
|
+
}
|
|
164
|
+
q.run(
|
|
165
|
+
`INSERT INTO users (id, handle, display_name, webauthn_user_id, created_at, tailnet_login, tailnet_linked_at, tailnet_user_id)
|
|
166
|
+
VALUES (:id, :handle, :name, :webauthn, :at, :login, :at, :tid)`,
|
|
167
|
+
{
|
|
168
|
+
id: user.id,
|
|
169
|
+
handle: user.handle,
|
|
170
|
+
name,
|
|
171
|
+
webauthn: user.webauthn_user_id,
|
|
172
|
+
at: user.created_at,
|
|
173
|
+
login: identity.login,
|
|
174
|
+
tid: identity.userId ? String(identity.userId) : null,
|
|
175
|
+
},
|
|
176
|
+
)
|
|
177
|
+
q.run('DELETE FROM tailnet_visitors WHERE login = :login', { login: identity.login })
|
|
178
|
+
return user
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// What a first visit would be called, for the form.
|
|
183
|
+
const suggestion = (identity) => ({ handle: freeHandle(handleFromLogin(identity.login)), displayName: identity.name })
|
|
184
|
+
|
|
185
|
+
// ---- Logins that arrived and belong to nobody yet ----
|
|
186
|
+
|
|
187
|
+
function noteVisitor(identity) {
|
|
188
|
+
const at = iso()
|
|
189
|
+
q.run(
|
|
190
|
+
`INSERT INTO tailnet_visitors (login, name, first_seen_at, last_seen_at, visits) VALUES (:login, :name, :at, :at, 1)
|
|
191
|
+
ON CONFLICT(login) DO UPDATE SET name = excluded.name, last_seen_at = excluded.last_seen_at, visits = visits + 1`,
|
|
192
|
+
{ login: identity.login, name: identity.name || '', at },
|
|
193
|
+
)
|
|
194
|
+
q.run('DELETE FROM tailnet_visitors WHERE last_seen_at < :cutoff', {
|
|
195
|
+
cutoff: new Date(now().getTime() - TAILNET_VISITOR_DAYS * DAY).toISOString(),
|
|
196
|
+
})
|
|
197
|
+
q.run(
|
|
198
|
+
'DELETE FROM tailnet_visitors WHERE login NOT IN (SELECT login FROM tailnet_visitors ORDER BY last_seen_at DESC, login LIMIT :max)',
|
|
199
|
+
{ max: TAILNET_VISITORS_MAX },
|
|
200
|
+
)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const visitors = () =>
|
|
204
|
+
q
|
|
205
|
+
.all(
|
|
206
|
+
'SELECT v.* FROM tailnet_visitors v WHERE v.login NOT IN (SELECT tailnet_login FROM users WHERE tailnet_login IS NOT NULL) ORDER BY v.last_seen_at DESC',
|
|
207
|
+
{},
|
|
208
|
+
)
|
|
209
|
+
.map((row) => ({
|
|
210
|
+
login: row.login,
|
|
211
|
+
name: row.name || null,
|
|
212
|
+
firstSeenAt: row.first_seen_at,
|
|
213
|
+
lastSeenAt: row.last_seen_at,
|
|
214
|
+
visits: row.visits,
|
|
215
|
+
}))
|
|
216
|
+
|
|
217
|
+
const forgetVisitor = (login) => q.run('DELETE FROM tailnet_visitors WHERE login = :login', { login: String(login) }).changes > 0
|
|
218
|
+
|
|
219
|
+
return { userFor, accountOf, heldByAnother, link, unlink, createUser, suggestion, noteVisitor, visitors, forgetVisitor }
|
|
220
|
+
}
|