Files
web-terminal/relay-e2e/src/crypto-provider.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

52 lines
1.8 KiB
TypeScript

/**
* T1 — isomorphic crypto provider: entropy + constant-time compare + WebCrypto handle.
*
* Uses ONLY the WHATWG `globalThis.crypto` surface (present in browsers and Node >=20), so the
* package stays isomorphic with zero `node:crypto` import. `timingSafeEqual` is hand-rolled
* (no `Buffer`) so it runs unchanged in the browser bundle.
*/
import { CryptoUnavailableError } from './errors.js'
function globalCrypto(): Crypto {
const c = (globalThis as { crypto?: Crypto }).crypto
if (!c || typeof c.getRandomValues !== 'function') {
throw new CryptoUnavailableError('globalThis.crypto.getRandomValues is unavailable')
}
return c
}
/** Return the WebCrypto SubtleCrypto (browser `crypto.subtle` / Node `webcrypto.subtle`). */
export function getWebCrypto(): SubtleCrypto {
const subtle = globalCrypto().subtle
if (!subtle) {
throw new CryptoUnavailableError('WebCrypto SubtleCrypto is unavailable')
}
return subtle
}
/** CSPRNG bytes via `crypto.getRandomValues`. */
export function randomBytes(len: number): Uint8Array {
if (!Number.isInteger(len) || len < 0) {
throw new CryptoUnavailableError(`randomBytes length must be a non-negative integer; got ${len}`)
}
const out = new Uint8Array(len)
globalCrypto().getRandomValues(out)
return out
}
/**
* Constant-time, length-safe byte equality. Returns `false` on any length mismatch WITHOUT a
* content-dependent early exit: both branches XOR-accumulate over a fixed number of iterations.
*/
export function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean {
const lenDiff = a.length ^ b.length
const n = Math.max(a.length, b.length)
let acc = lenDiff
for (let i = 0; i < n; i++) {
const av = i < a.length ? a[i]! : 0
const bv = i < b.length ? b[i]! : 0
acc |= av ^ bv
}
return acc === 0
}