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.
78 lines
3.3 KiB
TypeScript
78 lines
3.3 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
policyForPlan,
|
|
PRE_AUTH_POLICY,
|
|
checkPreAuthRate,
|
|
checkConnectRate,
|
|
checkConcurrentSessions,
|
|
checkEnrollRate,
|
|
} from '../src/ratelimit/quota.js'
|
|
import { fakeCountingBucket } from './_helpers.js'
|
|
|
|
const NOW = 1_700_000_000
|
|
|
|
describe('rate-limits / quotas (T11)', () => {
|
|
it('pre-auth throttle (Finding-5): one IP hammering many subdomains is throttled, no account', async () => {
|
|
const bucket = fakeCountingBucket()
|
|
const ip = 'ip-hash-1'
|
|
let allowed = 0
|
|
for (let i = 0; i < PRE_AUTH_POLICY.preAuthPerMinPerIp + 5; i++) {
|
|
if (await checkPreAuthRate(ip, bucket, NOW)) allowed++
|
|
}
|
|
expect(allowed).toBe(PRE_AUTH_POLICY.preAuthPerMinPerIp) // burst then throttled
|
|
})
|
|
|
|
it('pre-auth throttle refills after its window', async () => {
|
|
const bucket = fakeCountingBucket()
|
|
const ip = 'ip-hash-2'
|
|
for (let i = 0; i < PRE_AUTH_POLICY.preAuthPerMinPerIp; i++) await checkPreAuthRate(ip, bucket, NOW)
|
|
expect(await checkPreAuthRate(ip, bucket, NOW)).toBe(false)
|
|
expect(await checkPreAuthRate(ip, bucket, NOW + 120)).toBe(true) // refilled
|
|
})
|
|
|
|
it('per-account connect rate: over connectPerMin → false, refills after window', async () => {
|
|
const bucket = fakeCountingBucket()
|
|
const policy = policyForPlan('free')
|
|
let allowed = 0
|
|
for (let i = 0; i < policy.connectPerMin + 3; i++) {
|
|
if (await checkConnectRate('acct-A', policy, bucket, NOW)) allowed++
|
|
}
|
|
expect(allowed).toBe(policy.connectPerMin)
|
|
expect(await checkConnectRate('acct-A', policy, bucket, NOW + 120)).toBe(true)
|
|
})
|
|
|
|
it('per-account limits are per accountId — A hitting its limit does not affect B', async () => {
|
|
const bucket = fakeCountingBucket()
|
|
const policy = policyForPlan('free')
|
|
for (let i = 0; i < policy.connectPerMin + 5; i++) await checkConnectRate('acct-A', policy, bucket, NOW)
|
|
expect(await checkConnectRate('acct-A', policy, bucket, NOW)).toBe(false)
|
|
expect(await checkConnectRate('acct-B', policy, bucket, NOW)).toBe(true) // B unaffected
|
|
})
|
|
|
|
it('cross-key isolation: pre-auth IP throttle and account quota use disjoint namespaces', async () => {
|
|
const bucket = fakeCountingBucket()
|
|
const policy = policyForPlan('free')
|
|
// Exhaust the pre-auth bucket for an IP hash that equals an accountId string.
|
|
for (let i = 0; i < PRE_AUTH_POLICY.preAuthPerMinPerIp; i++) await checkPreAuthRate('collide', bucket, NOW)
|
|
expect(await checkPreAuthRate('collide', bucket, NOW)).toBe(false)
|
|
// The account quota for the same string is a different key → still allowed.
|
|
expect(await checkConnectRate('collide', policy, bucket, NOW)).toBe(true)
|
|
})
|
|
|
|
it('concurrent sessions over the cap → denied', () => {
|
|
const policy = policyForPlan('free')
|
|
expect(checkConcurrentSessions('acct-A', policy.maxConcurrentSessions - 1, policy)).toBe(true)
|
|
expect(checkConcurrentSessions('acct-A', policy.maxConcurrentSessions, policy)).toBe(false)
|
|
})
|
|
|
|
it('enroll spam over enrollPerHour → denied', async () => {
|
|
const bucket = fakeCountingBucket()
|
|
const policy = policyForPlan('free')
|
|
let allowed = 0
|
|
for (let i = 0; i < policy.enrollPerHour + 2; i++) {
|
|
if (await checkEnrollRate('acct-A', policy, bucket, NOW)) allowed++
|
|
}
|
|
expect(allowed).toBe(policy.enrollPerHour)
|
|
})
|
|
})
|