Files
web-terminal/relay-contracts/test/mux-frame.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

98 lines
3.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
ContractDecodeError,
ContractEncodeError,
decodeHeader,
decodeMuxFrame,
encodeMuxFrame,
type MuxFrameHeader,
} from '../src/index.js'
const baseHeader = (over: Partial<MuxFrameHeader> = {}): MuxFrameHeader => ({
version: 1,
type: 'data',
fin: false,
rst: false,
streamId: 1,
payloadLen: 0,
...over,
})
describe('§4.1 mux frame codec', () => {
it('KAT: encodes a known DATA frame to a fixed byte vector', () => {
const header = baseHeader({ type: 'data', fin: true, streamId: 0x01020304, payloadLen: 2 })
const payload = Uint8Array.of(0xaa, 0xbb)
const bytes = encodeMuxFrame(header, payload)
expect([...bytes]).toEqual([
0x01, // version
0x02, // type=data
0x01, // flags: FIN
0x01, 0x02, 0x03, 0x04, // streamId
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, // payloadLen uint64
0xaa, 0xbb, // payload
])
})
it('KAT: fixed bytes decode to fixed header + payload', () => {
const bytes = Uint8Array.of(
0x01, 0x07, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x99,
)
const { header, payload } = decodeMuxFrame(bytes)
expect(header).toEqual({
version: 1,
type: 'goaway',
fin: false,
rst: true,
streamId: 0,
payloadLen: 1,
})
expect([...payload]).toEqual([0x99])
})
it.each(['open', 'data', 'close', 'ping', 'pong', 'windowUpdate', 'goaway'] as const)(
'round-trips type=%s with flags',
(type) => {
const header = baseHeader({ type, fin: true, rst: true, streamId: 42, payloadLen: 3 })
const payload = Uint8Array.of(1, 2, 3)
const decoded = decodeMuxFrame(encodeMuxFrame(header, payload))
expect(decoded.header).toEqual(header)
expect([...decoded.payload]).toEqual([1, 2, 3])
},
)
it('decodeHeader reads only the header of a longer buffer', () => {
const bytes = encodeMuxFrame(baseHeader({ payloadLen: 4 }), Uint8Array.of(9, 9, 9, 9))
expect(decodeHeader(bytes).payloadLen).toBe(4)
})
it('rejects encode when payloadLen != payload.length', () => {
expect(() => encodeMuxFrame(baseHeader({ payloadLen: 5 }), Uint8Array.of(1))).toThrow(
ContractEncodeError,
)
})
it('rejects an unknown frame type code on decode', () => {
const bytes = Uint8Array.of(0x01, 0x7f, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
expect(() => decodeHeader(bytes)).toThrow(ContractDecodeError)
})
it('rejects reserved flag bits', () => {
const bytes = Uint8Array.of(0x01, 0x02, 0x04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
expect(() => decodeHeader(bytes)).toThrow(/reserved flag/)
})
it('rejects an unsupported version', () => {
const bytes = Uint8Array.of(0x02, 0x02, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
expect(() => decodeHeader(bytes)).toThrow(/version/)
})
it('rejects a truncated payload', () => {
const bytes = Uint8Array.of(0x01, 0x02, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x05)
expect(() => decodeMuxFrame(bytes)).toThrow(/truncated/)
})
it('rejects a buffer shorter than the header', () => {
expect(() => decodeHeader(Uint8Array.of(0x01, 0x02))).toThrow(ContractDecodeError)
})
})