Files
web-terminal/agent/test/pair.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

106 lines
4.0 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { encodeBase64UrlBytes } from 'relay-contracts'
import { generateIdentity } from '../src/keys/identity.js'
import { openKeystore } from '../src/keys/keystore.js'
import {
DEFAULT_ENROLL_MODE,
EnrollError,
PairingCodeExpiredError,
PairingCodeSpentError,
redeemPairingCode,
} from '../src/enroll/pair.js'
const ENROLL_URL = 'https://example.com/enroll'
const VALID_UUID = '11111111-1111-4111-8111-111111111111'
function freshKs() {
const dir = mkdtempSync(join(tmpdir(), 'wta-pair-'))
return { dir, ks: openKeystore(dir) }
}
function jsonResponse(status: number, body: unknown): Response {
return {
ok: status >= 200 && status < 300,
status,
json: async () => body,
} as unknown as Response
}
function okBody(wrappedSecret: Uint8Array) {
return {
hostId: VALID_UUID,
subdomain: 'host-42',
cert: 'CERTPEM',
caChain: 'CAPEM',
hostContentSecret: encodeBase64UrlBytes(wrappedSecret),
}
}
describe('redeemPairingCode (§4.5, T4)', () => {
it('defaults to ed25519 mode', () => {
expect(DEFAULT_ENROLL_MODE).toBe('ed25519')
})
it('sends only pubkey + csr, never the private key (INV4)', async () => {
const { dir, ks } = freshKs()
const id = generateIdentity()
const fetchImpl = vi.fn(async (_u: string | URL | Request, init?: RequestInit) => {
const body = JSON.parse(String(init!.body)) as Record<string, unknown>
expect(body).toHaveProperty('agentPubkey')
expect(body).toHaveProperty('csr')
expect(JSON.stringify(body)).not.toContain('PRIVATE KEY')
expect(body).not.toHaveProperty('privateKey')
return jsonResponse(200, okBody(new Uint8Array([9, 9, 9])))
})
await redeemPairingCode(ENROLL_URL, 'ABCD-1234', id, ks, { fetchImpl: fetchImpl as unknown as typeof fetch })
rmSync(dir, { recursive: true, force: true })
})
it('stores cert + unwrapped content secret; wrapped bytes not persisted (FIX 3)', async () => {
const { dir, ks } = freshKs()
const id = generateIdentity()
const wrapped = new Uint8Array([1, 2, 3])
const unwrapped = new Uint8Array([7, 7, 7])
const fetchImpl = async () => jsonResponse(200, okBody(wrapped))
await redeemPairingCode(ENROLL_URL, 'ABCD-1234', id, ks, {
fetchImpl: fetchImpl as unknown as typeof fetch,
unwrapContentSecret: () => unwrapped,
})
expect(ks.loadCert()).toEqual({ certPem: 'CERTPEM', caChainPem: 'CAPEM' })
const stored = ks.loadContentSecret()!
expect(Buffer.from(stored).equals(Buffer.from(unwrapped))).toBe(true)
expect(Buffer.from(stored).equals(Buffer.from(wrapped))).toBe(false)
rmSync(dir, { recursive: true, force: true })
})
it('maps 409 → PairingCodeSpentError (single-use)', async () => {
const { dir, ks } = freshKs()
const fetchImpl = async () => jsonResponse(409, {})
await expect(
redeemPairingCode(ENROLL_URL, 'X', generateIdentity(), ks, { fetchImpl: fetchImpl as unknown as typeof fetch }),
).rejects.toBeInstanceOf(PairingCodeSpentError)
rmSync(dir, { recursive: true, force: true })
})
it('maps 410 → PairingCodeExpiredError', async () => {
const { dir, ks } = freshKs()
const fetchImpl = async () => jsonResponse(410, {})
await expect(
redeemPairingCode(ENROLL_URL, 'X', generateIdentity(), ks, { fetchImpl: fetchImpl as unknown as typeof fetch }),
).rejects.toBeInstanceOf(PairingCodeExpiredError)
rmSync(dir, { recursive: true, force: true })
})
it('rejects a schema-mismatched response (boundary validation)', async () => {
const { dir, ks } = freshKs()
const fetchImpl = async () => jsonResponse(200, { hostId: 'not-a-uuid', subdomain: 'x' })
await expect(
redeemPairingCode(ENROLL_URL, 'X', generateIdentity(), ks, { fetchImpl: fetchImpl as unknown as typeof fetch }),
).rejects.toBeInstanceOf(EnrollError)
rmSync(dir, { recursive: true, force: true })
})
})