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.
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
/**
|
|
* T7 — sequence guard (anti-replay/injection, INV13).
|
|
*
|
|
* The transport (§4.1 mux over an ordered WS/TCP stream) delivers per-stream in order, so INV13 is
|
|
* enforced as STRICT SUCCESSOR: recv requires `seq === last + 1` (from 0). A duplicate, reorder,
|
|
* gap, or rewind all raise `ReplayError`. There is deliberately NO sliding acceptance window.
|
|
*/
|
|
import { ReplayError } from './errors.js'
|
|
|
|
const U64_MAX = 2n ** 64n - 1n
|
|
|
|
export class SequenceGuard {
|
|
readonly role: 'send' | 'recv'
|
|
#next: bigint = 0n
|
|
#lastAccepted: bigint | null = null
|
|
|
|
constructor(role: 'send' | 'recv') {
|
|
this.role = role
|
|
}
|
|
|
|
/** send-side: return the current seq, then increment (strictly monotonic per direction). */
|
|
next(): bigint {
|
|
if (this.role !== 'send') {
|
|
throw new ReplayError('next() called on a recv guard')
|
|
}
|
|
const seq = this.#next
|
|
if (seq > U64_MAX) {
|
|
throw new ReplayError('sequence exhausted u64 — re-key required')
|
|
}
|
|
this.#next += 1n
|
|
return seq
|
|
}
|
|
|
|
/** recv-side: require the strict successor; else throw ReplayError (dup/reorder/gap/rewind). */
|
|
accept(seq: bigint): void {
|
|
if (this.role !== 'recv') {
|
|
throw new ReplayError('accept() called on a send guard')
|
|
}
|
|
if (seq < 0n || seq > U64_MAX) {
|
|
throw new ReplayError(`sequence out of u64 range: ${seq}`)
|
|
}
|
|
const expected = this.#lastAccepted === null ? 0n : this.#lastAccepted + 1n
|
|
if (seq !== expected) {
|
|
throw new ReplayError(`out-of-order sequence: expected ${expected}, got ${seq}`)
|
|
}
|
|
this.#lastAccepted = seq
|
|
}
|
|
|
|
get lastAccepted(): bigint | null {
|
|
return this.#lastAccepted
|
|
}
|
|
|
|
/** Next expected recv seq (send guards report their next send seq). */
|
|
get expected(): bigint {
|
|
return this.role === 'send'
|
|
? this.#next
|
|
: this.#lastAccepted === null
|
|
? 0n
|
|
: this.#lastAccepted + 1n
|
|
}
|
|
}
|