Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(core): domain-separated kid.mac node tokens with three-way verification

Claude Code2534d66b7aee
diff --git a/src/core/agents/node-auth-token.test.ts b/src/core/agents/node-auth-token.test.ts
new file mode 100644
index 000000000..9c585076e
--- /dev/null
+++ b/src/core/agents/node-auth-token.test.ts
@@ -0,0 +1,74 @@
+import { describe, it, expect } from 'vitest'
+import { createHmac, randomBytes } from 'node:crypto'
+import {
+ isSafeNodeId,
+ nodeAuthKid,
+ nodeAuthToken,
+ verifyNodeToken
+} from './node-auth-token'
+
+const secret = randomBytes(32)
+const other = randomBytes(32)
+
+describe('node auth token', () => {
+ it('is kid.mac, domain-separated, and stable for a given secret+node', () => {
+ const t = nodeAuthToken(secret, 'node-1')
+ expect(t).toMatch(/^[A-Za-z0-9_-]{8}\.[A-Za-z0-9_-]{43}$/)
+ expect(t.split('.')[0]).toBe(nodeAuthKid(secret))
+ expect(nodeAuthToken(secret, 'node-1')).toBe(t)
+ // Domain separation: the bare-nodeId HMAC #167 shipped must NOT be a valid mac here.
+ const bareMac = createHmac('sha256', secret).update('node-1').digest('base64url')
+ expect(t.split('.')[1]).not.toBe(bareMac)
+ })
+
+ it("verifies our kid + right node, and 403s our kid + another node's mac", () => {
+ expect(verifyNodeToken(secret, 'node-1', nodeAuthToken(secret, 'node-1'))).toBe('verified')
+ expect(verifyNodeToken(secret, 'node-1', nodeAuthToken(secret, 'node-2'))).toBe('forged')
+ })
+
+ it('403s a mutated mac in every shape (flipped, truncated, padded, empty-mac)', () => {
+ const t = nodeAuthToken(secret, 'node-1')
+ const [kid, mac] = t.split('.')
+ const flipped = `${kid}.${mac.slice(0, -1)}${mac.endsWith('A') ? 'B' : 'A'}`
+ for (const bad of [flipped, `${kid}.${mac.slice(0, 20)}`, `${kid}.${mac} `, `${kid}.`]) {
+ expect(verifyNodeToken(secret, 'node-1', bad), bad).toBe('forged')
+ }
+ })
+
+ it('treats a FOREIGN kid as legacy, never as forgery (the failover path)', () => {
+ expect(verifyNodeToken(secret, 'node-1', nodeAuthToken(other, 'node-1'))).toBe('legacy')
+ })
+
+ it('treats absent / empty / shapeless / non-string tokens as legacy', () => {
+ for (const p of [undefined, '', 'not-a-token', ['a', 'b']] as const) {
+ expect(verifyNodeToken(secret, 'node-1', p as never)).toBe('legacy')
+ }
+ // A leading dot is not our shape (dot at index 0).
+ expect(verifyNodeToken(secret, 'node-1', '.mac')).toBe('legacy')
+ })
+
+ it('is legacy when the server has no secret at all (mixed-version machines)', () => {
+ expect(verifyNodeToken(null, 'node-1', nodeAuthToken(secret, 'node-1'))).toBe('legacy')
+ })
+
+ it('refuses unsafe node ids before hashing — including bare dot segments', () => {
+ for (const bad of ['..', '.', '../x', 'a b', 'a/b', '', 'x'.repeat(300)]) {
+ expect(isSafeNodeId(bad), bad).toBe(false)
+ }
+ for (const good of ['node-1', 'term_abc.9', 'A.B-C_d']) {
+ expect(isSafeNodeId(good), good).toBe(true)
+ }
+ expect(nodeAuthToken(secret, '../etc')).toBe('')
+ // An unsafe id UNDER our kid is forgery, not legacy — the id can never be a real token.
+ expect(verifyNodeToken(secret, '..', `${nodeAuthKid(secret)}.whatever`)).toBe('forged')
+ })
+
+ it('does not throw on unequal-length compares', () => {
+ expect(() => verifyNodeToken(secret, 'node-1', `${nodeAuthKid(secret)}.x`)).not.toThrow()
+ })
+
+ it('handles an enormous presented string without throwing or stalling', () => {
+ const huge = `${nodeAuthKid(secret)}.${'A'.repeat(10000)}`
+ expect(verifyNodeToken(secret, 'node-1', huge)).toBe('forged')
+ })
+})
diff --git a/src/core/agents/node-auth-token.ts b/src/core/agents/node-auth-token.ts
new file mode 100644
index 000000000..8c6a48e91
--- /dev/null
+++ b/src/core/agents/node-auth-token.ts
@@ -0,0 +1,80 @@
+import { createHmac, timingSafeEqual } from 'crypto'
+
+/**
+ * Per-node capability, derived rather than minted-and-stored.
+ *
+ * Derived is the only shape that survives the restart constraint: tmux sessions outlive the app,
+ * so a table minted at spawn is empty for every already-running session after a restart, and there
+ * is no way to rebuild it (a node's tmux session can exist with no record on our side). A
+ * derivation over a stable input lives exactly as long as the secret, which is the lifetime the
+ * endpoint file already assumes.
+ *
+ * kid = base64url(HMAC-SHA256(secret, "nt-node-auth-kid-v1"))[0..8]
+ * mac = base64url(HMAC-SHA256(secret, "nt-node-auth-v1|" + nodeId))
+ * token = kid + "." + mac
+ *
+ * The prefix is domain separation, so the same secret can later mint other capability CLASSES (a
+ * per-node relay capability, a per-project one) without one being a valid other. #167 hashed the
+ * bare nodeId; it is retrofitted onto this derivation in a later task.
+ */
+export const NODE_ID_CHARSET = /^[A-Za-z0-9._-]+$/
+const KID_CONTEXT = 'nt-node-auth-kid-v1'
+const MAC_PREFIX = 'nt-node-auth-v1|'
+const MAX_NODE_ID = 128
+
+/**
+ * The charset alone is NOT enough: `.` and `..` both MATCH `NODE_ID_CHARSET`, and this id is also a
+ * path segment under the token dir (`<tokenDir>/<nodeId>`). A `..` there would resolve to the token
+ * dir's PARENT. The nodeId is attacker-controlled — it arrives from `project.json`, which travels in
+ * cloned/shared repos — so refuse `.` and `..` by name, refuse empty, refuse over-length, BEFORE the
+ * id ever reaches a hash or a path join.
+ */
+export function isSafeNodeId(id: string): boolean {
+ return (
+ typeof id === 'string' &&
+ id.length > 0 &&
+ id.length <= MAX_NODE_ID &&
+ id !== '.' &&
+ id !== '..' &&
+ NODE_ID_CHARSET.test(id)
+ )
+}
+
+export function nodeAuthKid(secret: Buffer): string {
+ return createHmac('sha256', secret).update(KID_CONTEXT).digest('base64url').slice(0, 8)
+}
+
+/** '' for an unsafe node id — never a token, and never a hash of attacker-shaped input. */
+export function nodeAuthToken(secret: Buffer, nodeId: string): string {
+ if (!isSafeNodeId(nodeId)) return ''
+ const mac = createHmac('sha256', secret).update(`${MAC_PREFIX}${nodeId}`).digest('base64url')
+ return `${nodeAuthKid(secret)}.${mac}`
+}
+
+export type NodeTokenVerdict = 'verified' | 'legacy' | 'forged'
+
+/**
+ * Three-way, not two-way. `legacy` is NOT a failure — it is "we cannot judge this", and per-route
+ * policy decides what that means (a missing token, or another instance's kid during the documented
+ * cross-instance failover). `forged` — our kid with a bad mac — is the ONLY unambiguous attack
+ * signal we have and is a 403 on every route, including /hook/*.
+ */
+export function verifyNodeToken(
+ secret: Buffer | null,
+ nodeId: string,
+ presented: string | string[] | undefined
+): NodeTokenVerdict {
+ if (!secret) return 'legacy' // no secret ⇒ open (mixed-version machines during rollout)
+ if (typeof presented !== 'string' || presented === '') return 'legacy'
+ const dot = presented.indexOf('.')
+ if (dot <= 0) return 'legacy' // not our wire shape at all
+ // The kid is a NON-SECRET routing decision, so a plain compare is correct and cheap; only the
+ // full-token compare gets the constant-time treatment, and only once the kid says the token is
+ // ours to judge. A foreign kid is the failover path and must be `legacy`, never `forged`.
+ if (presented.slice(0, dot) !== nodeAuthKid(secret)) return 'legacy'
+ const expected = nodeAuthToken(secret, nodeId)
+ if (!expected) return 'forged' // our kid, unusable node id ⇒ forgery, not legacy
+ const a = Buffer.from(presented)
+ const b = Buffer.from(expected)
+ return a.length === b.length && timingSafeEqual(a, b) ? 'verified' : 'forged'
+}