Files
web-terminal/control-plane/test/routing.test.ts
Yaojia Wang 2af57e6686 feat(relay): rendezvous-relay service — 7 packages + plans (contracts/transport/agent/control-plane/e2e/auth/web)
Multi-tenant reverse-tunnel service ("ngrok for Claude Code" with E2E): a
host-agent dials OUT to an operator-run relay; external devices reach the host
THROUGH the relay, routed by per-tenant subdomain, forwarding ciphertext only
(the relay never sees plaintext). Lets a customer reach their own self-hosted
web-terminal from anywhere with zero networking setup.

Packages — all tsc-strict + vitest green (656 tests), cross-package integration verified:
- relay-contracts: frozen shared contracts (mux frame codec, data model,
  capability token, E2E envelope, pairing) — the src/types.ts analog
- term-relay:   native WS mux + stateless data plane (subdomain routing, ciphertext forward)
- agent:        host-agent (pairing, per-host Ed25519 + mTLS dial-out, forwards to 127.0.0.1:3000)
- control-plane: accounts/hosts registry, pairing-code flow, routing table, provisioning
- relay-e2e:    browser<->agent E2E (X25519 ECDH through relay, AEAD, anti-replay, recoverable replay key)
- relay-auth:   Passkey/WebAuthn, capability tokens, per-host certs, deny-by-default tenant isolation
- relay-web:    browser login + Web Crypto E2E + client-side preview rendering

Security invariants INV1-15 enforced; cross-tenant isolation CI tripwire live
(.github/workflows/relay-tripwire.yml). Design + implementation-level plans in
docs/PLAN_RELAY_*.md and docs/EXPLORE_RELAY_SERVICE.md.

NOTE: generated autonomously per the reviewed plans. The security-critical
packages (relay-e2e, relay-auth) REQUIRE expert security audit before any real
deployment — passing tests prove self-consistency, not resistance to attackers.
Base app (src/, public/) unchanged; concurrent desktop work left uncommitted.
2026-07-02 06:10:16 +02:00

91 lines
4.4 KiB
TypeScript

import { describe, test, expect } from 'vitest'
import { createMemoryStores } from '../src/store/memory.js'
import { createRoutingTable, RouteAuthError } from '../src/routing/table.js'
import { createNodeCoordinator } from '../src/routing/nodes.js'
import { createInMemoryRevocationBus } from '../src/routing/bus.js'
import { deriveNodeIdentity, NodeAuthError, type NodeIdentity } from '../src/node-auth/identity.js'
import type { KillSignal } from 'relay-contracts'
const nodeA: NodeIdentity = { nodeId: 'spiffe://relay/node-a' }
const nodeB: NodeIdentity = { nodeId: 'spiffe://relay/node-b' }
const HOST = '33333333-3333-4333-8333-333333333333'
describe('T9 node identity (INV3-analog)', () => {
test('authorized cert with CN → id; unauthenticated → 401; no CN → 401', () => {
expect(deriveNodeIdentity({ authorized: true, subjectCommonName: 'node-x' }).nodeId).toBe('node-x')
expect(() => deriveNodeIdentity({ authorized: false, subjectCommonName: 'node-x' })).toThrow(NodeAuthError)
expect(() => deriveNodeIdentity({ authorized: true, subjectCommonName: null })).toThrow(NodeAuthError)
expect(() => deriveNodeIdentity(null)).toThrow(NodeAuthError)
})
})
describe('T9 routing table (INV7)', () => {
test('upsert then resolve; foreign relayNodeId rejected', async () => {
const { routes } = createMemoryStores()
const table = createRoutingTable({ routes })
await table.upsertRoute(nodeA, HOST, { relayNodeId: nodeA.nodeId, updatedAt: new Date().toISOString() }, 60)
expect((await table.resolveRoute(HOST))?.relayNodeId).toBe(nodeA.nodeId)
await expect(
table.upsertRoute(nodeA, HOST, { relayNodeId: nodeB.nodeId, updatedAt: new Date().toISOString() }, 60),
).rejects.toBeInstanceOf(RouteAuthError)
})
test('heartbeat-TTL expiry ⇒ resolveRoute null (fails closed)', async () => {
const { routes } = createMemoryStores()
const table = createRoutingTable({ routes })
await table.upsertRoute(nodeA, HOST, { relayNodeId: nodeA.nodeId, updatedAt: new Date().toISOString() }, 0)
expect(await table.resolveRoute(HOST)).toBeNull()
})
test('foreign node cannot heartbeat/hijack; only holder drops', async () => {
const { routes } = createMemoryStores()
const table = createRoutingTable({ routes })
await table.upsertRoute(nodeA, HOST, { relayNodeId: nodeA.nodeId, updatedAt: new Date().toISOString() }, 60)
await table.heartbeatRoute(nodeB, HOST, 60) // foreign → no-op
expect((await table.resolveRoute(HOST))?.relayNodeId).toBe(nodeA.nodeId)
await expect(table.dropRoute(nodeB, HOST)).rejects.toBeInstanceOf(RouteAuthError)
await table.dropRoute(nodeA, HOST)
expect(await table.resolveRoute(HOST)).toBeNull()
})
})
describe('T10 node registry + graceful drain (INV7)', () => {
test('beginDrain flips status, returns routed host_ids, publishes per-host KillSignal', async () => {
const stores = createMemoryStores()
const bus = createInMemoryRevocationBus()
const table = createRoutingTable({
routes: stores.routes,
nodeStatus: async (id) => (await stores.nodes.get(id))?.status ?? null,
})
const coord = createNodeCoordinator({ nodes: stores.nodes, routes: stores.routes, bus })
const received: KillSignal[] = []
bus.subscribe((s) => received.push(s))
await coord.registerNode(nodeA, '10.0.0.1:9000')
await table.upsertRoute(nodeA, HOST, { relayNodeId: nodeA.nodeId, updatedAt: new Date().toISOString() }, 60)
const { hostIds } = await coord.beginDrain(nodeA)
expect(hostIds).toEqual([HOST])
expect((await stores.nodes.get(nodeA.nodeId))?.status).toBe('draining')
expect(received.map((s) => s.scope)).toEqual([{ kind: 'host', hostId: HOST }])
// a draining node stops receiving new upserts
await expect(
table.upsertRoute(nodeA, '44444444-4444-4444-8444-444444444444', { relayNodeId: nodeA.nodeId, updatedAt: new Date().toISOString() }, 60),
).rejects.toBeInstanceOf(RouteAuthError)
await coord.completeDrain(nodeA)
expect(await stores.nodes.get(nodeA.nodeId)).toBeNull()
})
test('PTY-survival: draining touches no host/session row', async () => {
const stores = createMemoryStores()
const bus = createInMemoryRevocationBus()
const coord = createNodeCoordinator({ nodes: stores.nodes, routes: stores.routes, bus })
await coord.registerNode(nodeA, 'addr')
await coord.beginDrain(nodeA)
// no host rows were ever created by drain
expect(await stores.hosts.get(HOST)).toBeNull()
})
})