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

68 lines
2.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
APP_SUBPROTOCOL,
ContractDecodeError,
TOKEN_SUBPROTOCOL_PREFIX,
decodeBase64UrlBytes,
encodeBase64UrlBytes,
encodeBase64UrlString,
decodeBase64UrlString,
encodeTokenSubprotocol,
extractTokenFromSubprotocols,
} from '../src/index.js'
describe('base64url (isomorphic)', () => {
it('KAT: encodes "hello" to "aGVsbG8" (no padding)', () => {
expect(encodeBase64UrlString('hello')).toBe('aGVsbG8')
})
it('KAT: uses url-safe "-" and "_" instead of "+" and "/"', () => {
expect(encodeBase64UrlBytes(Uint8Array.of(0xfb, 0xff))).toBe('-_8')
})
it('round-trips arbitrary bytes', () => {
const bytes = Uint8Array.from({ length: 40 }, (_, i) => (i * 37) & 0xff)
expect([...decodeBase64UrlBytes(encodeBase64UrlBytes(bytes))]).toEqual([...bytes])
})
it('round-trips a unicode string', () => {
const s = 'café-☕-relay'
expect(decodeBase64UrlString(encodeBase64UrlString(s))).toBe(s)
})
it('rejects non-alphabet characters', () => {
expect(() => decodeBase64UrlBytes('abc*def')).toThrow(ContractDecodeError)
})
})
describe('§4.3 FIX 5 token subprotocol transport', () => {
it('encodeTokenSubprotocol prefixes with term.token. + base64url', () => {
expect(encodeTokenSubprotocol('hello')).toBe(`${TOKEN_SUBPROTOCOL_PREFIX}aGVsbG8`)
})
it('round-trips a raw token through the subprotocol list', () => {
const raw = 'v4.public.aBcD_-123'
const entry = encodeTokenSubprotocol(raw)
expect(extractTokenFromSubprotocols([APP_SUBPROTOCOL, entry])).toBe(raw)
})
it('returns null when no token entry is present', () => {
expect(extractTokenFromSubprotocols([APP_SUBPROTOCOL])).toBeNull()
})
it('ignores the app subprotocol and picks the token entry regardless of order', () => {
const entry = encodeTokenSubprotocol('tok')
expect(extractTokenFromSubprotocols([entry, APP_SUBPROTOCOL])).toBe('tok')
})
it('throws when the token entry is malformed base64url', () => {
expect(() => extractTokenFromSubprotocols([`${TOKEN_SUBPROTOCOL_PREFIX}not*valid`])).toThrow(
ContractDecodeError,
)
})
it('APP_SUBPROTOCOL is the single frozen value', () => {
expect(APP_SUBPROTOCOL).toBe('term.relay.v1')
})
})