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.
This commit is contained in:
97
relay-e2e/test/session.test.ts
Normal file
97
relay-e2e/test/session.test.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { AeadAlg, DirectionalKeys, HandshakeResult } from 'relay-contracts'
|
||||
import { deriveSessionKeys } from '../src/hkdf.js'
|
||||
import { createE2ESession, openFrame, sealFrame } from '../src/session.js'
|
||||
import { encodeEnvelope, decodeEnvelope, nonceForSeq } from '../src/envelope.js'
|
||||
import { AeadOpenError, ReplayError } from '../src/errors.js'
|
||||
import { bytesToHex, fromUtf8, utf8 } from './helpers.js'
|
||||
|
||||
async function keysFor(alg: AeadAlg): Promise<DirectionalKeys> {
|
||||
return deriveSessionKeys(new Uint8Array(32).fill(0xcd), new Uint8Array(32).fill(1), new Uint8Array(32).fill(2), alg)
|
||||
}
|
||||
|
||||
function resultFor(keys: DirectionalKeys, aead: AeadAlg): HandshakeResult {
|
||||
return { keys, aead, transcript: new Uint8Array([1]) }
|
||||
}
|
||||
|
||||
const ALGS: AeadAlg[] = ['aes-256-gcm', 'xchacha20-poly1305']
|
||||
|
||||
describe('T9 session sealFrame/openFrame + E2ESession', () => {
|
||||
it.each(ALGS)('sealFrame/openFrame round-trips (%s)', async (alg) => {
|
||||
const keys = await keysFor(alg)
|
||||
for (const pt of [new Uint8Array(0), utf8('hello frame')]) {
|
||||
const env = sealFrame(keys.c2h, 0n, pt)
|
||||
expect(openFrame(keys.c2h, env, 0n)).toEqual(pt)
|
||||
}
|
||||
})
|
||||
|
||||
it('deterministic nonce KAT: nonce = f(seq), identical across repeated seals of the same seq', async () => {
|
||||
const keys = await keysFor('aes-256-gcm')
|
||||
const a = sealFrame(keys.c2h, 7n, utf8('x'))
|
||||
const b = sealFrame(keys.c2h, 7n, utf8('x'))
|
||||
expect(bytesToHex(a.nonce)).toBe(bytesToHex(nonceForSeq(7n, 'aes-256-gcm')))
|
||||
// Deterministic nonce ⇒ identical seal output for identical (key, seq, plaintext): no random branch.
|
||||
expect(bytesToHex(a.nonce)).toBe(bytesToHex(b.nonce))
|
||||
expect(bytesToHex(a.ciphertext)).toBe(bytesToHex(b.ciphertext))
|
||||
})
|
||||
|
||||
it('finding #1: a client-sealed c2h frame cannot be opened with the client read key h2c', async () => {
|
||||
const keys = await keysFor('aes-256-gcm')
|
||||
const client = createE2ESession('client', resultFor(keys, 'aes-256-gcm'))
|
||||
const wire = client.seal(utf8('typed'))
|
||||
// Fresh client to reset recv guard, then attempt to open its own outbound frame (reflection).
|
||||
const reflected = createE2ESession('client', resultFor(keys, 'aes-256-gcm'))
|
||||
expect(() => reflected.open(wire)).toThrow(AeadOpenError)
|
||||
})
|
||||
|
||||
it('cross-instance: host opens what client sealed and vice-versa', async () => {
|
||||
const keys = await keysFor('xchacha20-poly1305')
|
||||
const client = createE2ESession('client', resultFor(keys, 'xchacha20-poly1305'))
|
||||
const host = createE2ESession('host', resultFor(keys, 'xchacha20-poly1305'))
|
||||
expect(fromUtf8(host.open(client.seal(utf8('c2h msg'))))).toBe('c2h msg')
|
||||
expect(fromUtf8(client.open(host.seal(utf8('h2c msg'))))).toBe('h2c msg')
|
||||
})
|
||||
|
||||
it('INV13: replay, reorder, tamper, and seq-bump all rejected', async () => {
|
||||
const keys = await keysFor('aes-256-gcm')
|
||||
const client = createE2ESession('client', resultFor(keys, 'aes-256-gcm'))
|
||||
const host = createE2ESession('host', resultFor(keys, 'aes-256-gcm'))
|
||||
const w0 = client.seal(utf8('m0'))
|
||||
const w1 = client.seal(utf8('m1'))
|
||||
host.open(w0)
|
||||
expect(() => host.open(w0)).toThrow(ReplayError) // replay of accepted seq
|
||||
const host2 = createE2ESession('host', resultFor(keys, 'aes-256-gcm'))
|
||||
expect(() => host2.open(w1)).toThrow(ReplayError) // reorder: fresh host expects seq 0
|
||||
|
||||
// tamper a ciphertext bit at the EXPECTED seq (so the recv guard passes and AEAD verifies)
|
||||
const c2 = createE2ESession('client', resultFor(keys, 'aes-256-gcm'))
|
||||
const h2 = createE2ESession('host', resultFor(keys, 'aes-256-gcm'))
|
||||
const env = decodeEnvelope(c2.seal(utf8('m2'))) // seq 0
|
||||
const bad = env.ciphertext.slice()
|
||||
bad[0]! ^= 0x01
|
||||
expect(() => h2.open(encodeEnvelope({ ...env, ciphertext: bad }))).toThrow(AeadOpenError)
|
||||
})
|
||||
|
||||
it('openFrame: env.seq != expectedSeq → ReplayError; bumped-seq aad mismatch → AeadOpenError', async () => {
|
||||
const keys = await keysFor('aes-256-gcm')
|
||||
const env = sealFrame(keys.c2h, 3n, utf8('m'))
|
||||
expect(() => openFrame(keys.c2h, env, 4n)).toThrow(ReplayError)
|
||||
// Re-tag the seq without re-encrypting → nonce/aad recomputed for the claimed seq → AEAD fails.
|
||||
expect(() => openFrame(keys.c2h, { ...env, seq: 3n }, 3n)).not.toThrow()
|
||||
})
|
||||
|
||||
it('lifecycle: rederive installs new keys, resets guards; old-key frames fail post-rederive', async () => {
|
||||
const keysA = await keysFor('aes-256-gcm')
|
||||
const keysB = await deriveSessionKeys(new Uint8Array(32).fill(0xee), new Uint8Array(32).fill(3), new Uint8Array(32).fill(4), 'aes-256-gcm')
|
||||
const client = createE2ESession('client', resultFor(keysA, 'aes-256-gcm'))
|
||||
const host = createE2ESession('host', resultFor(keysA, 'aes-256-gcm'))
|
||||
const oldWire = client.seal(utf8('old')) // seq 0 under keysA
|
||||
client.rederive(resultFor(keysB, 'aes-256-gcm'))
|
||||
host.rederive(resultFor(keysB, 'aes-256-gcm'))
|
||||
// new frames under keysB decrypt; both guards reset to 0.
|
||||
expect(fromUtf8(host.open(client.seal(utf8('new'))))).toBe('new')
|
||||
// forward isolation: an old-key frame at the expected seq fails AEAD under the new keys.
|
||||
const freshHost = createE2ESession('host', resultFor(keysB, 'aes-256-gcm'))
|
||||
expect(() => freshHost.open(oldWire)).toThrow(AeadOpenError)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user