Files
web-terminal/relay-web/src/api-schemas.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

73 lines
2.8 KiB
TypeScript

/**
* T2 — the frozen CLIENT-contract surface. Re-exports the `relay-contracts` §4.2/§4.3 shapes
* VERBATIM (never redefines them) and adds ONLY response envelopes needed to parse HTTP JSON.
*
* INV3: there is NO `account_id`/`tenant_id` REQUEST field anywhere here — identity is always the
* cookie/passkey session the browser already holds; the server derives the account.
*
* Wire note: over JSON, `HostRecord.agentPubkey` arrives base64url-encoded (a Uint8Array cannot
* ride JSON). The wire schema decodes it back to `Uint8Array` so callers receive a genuine
* `HostRecord` — validate-at-boundary (coding-style.md "Input Validation").
*/
import { z } from 'zod'
import {
HostStatusSchema,
decodeBase64UrlBytes,
type HostRecord,
type HostStatus,
type CapabilityRight,
} from 'relay-contracts'
// Re-export the frozen types verbatim so P6 modules import them from one place (the src/types.ts
// discipline: a new shared field is changed in relay-contracts, never redeclared locally).
export type { HostRecord, HostStatus, CapabilityRight }
export { HostStatusSchema }
/** Wire form of a HostRecord: `agentPubkey` is base64url text, decoded to bytes on parse. */
export const HostRecordWireSchema = z
.object({
hostId: z.string().uuid(),
accountId: z.string().uuid(),
subdomain: z.string().min(1),
agentPubkey: z.string().min(1),
enrollFpr: z.string().min(1),
status: HostStatusSchema,
lastSeen: z.string().min(1),
createdAt: z.string().min(1),
revokedAt: z.string().min(1).nullable(),
})
.strict()
.transform(
(w): HostRecord => ({
hostId: w.hostId,
accountId: w.accountId,
subdomain: w.subdomain,
// Copy into a fresh ArrayBuffer-backed Uint8Array so the type matches HostRecord exactly.
agentPubkey: new Uint8Array(decodeBase64UrlBytes(w.agentPubkey)),
enrollFpr: w.enrollFpr,
status: w.status,
lastSeen: w.lastSeen,
createdAt: w.createdAt,
revokedAt: w.revokedAt,
}),
)
/** `GET /api/hosts` → array of HostRecord. */
export const HostListWireSchema = z.array(HostRecordWireSchema).readonly()
/** `GET /api/hosts/:id/status` → the host's current status. */
export const HostStatusResponseSchema = z.object({ status: HostStatusSchema }).strict()
/** `POST /api/pairing-codes` → single-use short-TTL code + its expiry (§4.5 ISSUE). */
export const PairingCodeResponseSchema = z
.object({
code: z.string().min(1),
expiresAt: z.string().min(1),
})
.strict()
.readonly()
export type PairingCodeResponse = z.infer<typeof PairingCodeResponseSchema>
/** `POST /api/hosts/:id/capability-token` → opaque raw §4.3 token (v0.9+; never decoded to trust). */
export const CapabilityTokenResponseSchema = z.object({ token: z.string().min(1) }).strict()