Files
web-terminal/relay-web/test/webauthn.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

75 lines
2.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { encodeBase64UrlBytes } from 'relay-contracts'
import {
base64UrlToBuffer,
bufferToBase64Url,
createWebAuthnClient,
WebAuthnError,
} from '../src/webauthn'
function ab(bytes: number[]): ArrayBuffer {
return new Uint8Array(bytes).buffer
}
const assertionCred = {
id: 'cred-1',
type: 'public-key',
rawId: ab([1, 2, 3]),
response: {
clientDataJSON: ab([4, 5]),
authenticatorData: ab([6]),
signature: ab([7, 8]),
userHandle: null,
},
}
describe('webauthn wrapper (T7)', () => {
it('base64url ↔ ArrayBuffer round-trip is lossless', () => {
const bytes = [0, 1, 250, 255, 128, 64]
const b64u = bufferToBase64Url(ab(bytes))
expect(new Uint8Array(base64UrlToBuffer(b64u))).toEqual(new Uint8Array(bytes))
})
it('authenticate maps the assertion to base64url fields', async () => {
const container = {
get: vi.fn(async () => assertionCred),
create: vi.fn(),
} as unknown as CredentialsContainer
const wa = createWebAuthnClient(container)
const res = await wa.authenticate({ challenge: ab([9]) } as PublicKeyCredentialRequestOptions)
expect(res.rawId).toBe(encodeBase64UrlBytes(new Uint8Array([1, 2, 3])))
expect(res.response.signature).toBe(encodeBase64UrlBytes(new Uint8Array([7, 8])))
expect(res.response.userHandle).toBeNull()
})
it('a user-cancelled prompt (NotAllowedError) maps to WebAuthnError("cancelled")', async () => {
const container = {
get: vi.fn(async () => {
throw Object.assign(new Error('dismissed'), { name: 'NotAllowedError' })
}),
create: vi.fn(),
} as unknown as CredentialsContainer
const wa = createWebAuthnClient(container)
await expect(
wa.authenticate({ challenge: ab([1]) } as PublicKeyCredentialRequestOptions),
).rejects.toMatchObject({ name: 'WebAuthnError', kind: 'cancelled' })
})
it('throws WebAuthnError("unsupported") when no credentials container is available', () => {
expect(() => createWebAuthnClient(undefined)).toThrow(WebAuthnError)
})
it('does not leak credential material to console', async () => {
const spy = vi.spyOn(console, 'log').mockImplementation(() => {})
const container = {
get: vi.fn(async () => assertionCred),
create: vi.fn(),
} as unknown as CredentialsContainer
await createWebAuthnClient(container).authenticate({
challenge: ab([1]),
} as PublicKeyCredentialRequestOptions)
expect(spy).not.toHaveBeenCalled()
spy.mockRestore()
})
})