Files
web-terminal/relay-auth/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

94 lines
3.5 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { startRegistration, finishRegistration } from '../src/human/webauthn/register.js'
import { startAuthentication, finishAuthentication } from '../src/human/webauthn/authenticate.js'
import {
WebAuthnError,
type WebAuthnVerifier,
type AuthenticationResponse,
type RegistrationResponse,
} from '../src/human/webauthn/verifier.js'
import type { WebAuthnCredential } from '../src/types.js'
const RP_ID = 'alice.term.example.com'
const ORIGIN = 'https://alice.term.example.com'
const NOW = 1_700_000_000
const verifier: WebAuthnVerifier = {
verifyRegistration: async () => ({
verified: true,
credentialId: 'cred-1',
publicKey: new Uint8Array([1, 2, 3]),
signCount: 0,
transports: ['internal'],
}),
verifyAuthentication: async (_r, _c, _rp, _o, stored) => ({ verified: true, newSignCount: stored + 1 }),
}
function regResp(over: Partial<RegistrationResponse> = {}): RegistrationResponse {
return { clientChallenge: 'chal', origin: ORIGIN, rpId: RP_ID, ...over }
}
function authResp(over: Partial<AuthenticationResponse> = {}): AuthenticationResponse {
return { clientChallenge: 'chal', origin: ORIGIN, rpId: RP_ID, ...over }
}
const cred: WebAuthnCredential = {
credentialId: 'cred-1',
accountId: 'acct-A',
publicKey: new Uint8Array([1, 2, 3]),
signCount: 5,
transports: ['internal'],
createdAt: '2026-01-01T00:00:00.000Z',
}
describe('WebAuthn / Passkey (T5, primary)', () => {
it('startRegistration issues rpId + a challenge', async () => {
const opts = await startRegistration('acct-A', RP_ID)
expect(opts.rpId).toBe(RP_ID)
expect(opts.challenge.length).toBeGreaterThan(0)
expect(opts.userId).toBe('acct-A')
})
it('finishRegistration mints a credential bound to the account', async () => {
const c = await finishRegistration('acct-A', regResp(), 'chal', RP_ID, ORIGIN, verifier)
expect(c.accountId).toBe('acct-A')
expect(c.credentialId).toBe('cred-1')
})
it('rejects a challenge mismatch (single-use / replayed challenge)', async () => {
await expect(finishRegistration('acct-A', regResp({ clientChallenge: 'other' }), 'chal', RP_ID, ORIGIN, verifier)).rejects.toThrow(
WebAuthnError,
)
})
it('rejects an origin mismatch (assertion from evil.com)', async () => {
await expect(
finishAuthentication(cred, authResp({ origin: 'https://evil.com' }), 'chal', RP_ID, ORIGIN, verifier, NOW),
).rejects.toThrow('origin')
})
it('happy path auth yields a principal with amr:[passkey]', async () => {
const startOpts = await startAuthentication('acct-A', RP_ID)
expect(startOpts.rpId).toBe(RP_ID)
const p = await finishAuthentication(cred, authResp(), 'chal', RP_ID, ORIGIN, verifier, NOW)
expect(p.accountId).toBe('acct-A')
expect(p.amr).toEqual(['passkey'])
expect(p.authAt).toBe(NOW)
})
it('rejects a signCount regression (cloned authenticator signal)', async () => {
const regressing: WebAuthnVerifier = {
...verifier,
verifyAuthentication: async () => ({ verified: true, newSignCount: 3 }), // < stored 5
}
await expect(
finishAuthentication(cred, authResp(), 'chal', RP_ID, ORIGIN, regressing, NOW),
).rejects.toThrow('signCount')
})
it('rejects when the verifier reports not-verified', async () => {
const bad: WebAuthnVerifier = { ...verifier, verifyAuthentication: async () => ({ verified: false, newSignCount: 99 }) }
await expect(finishAuthentication(cred, authResp(), 'chal', RP_ID, ORIGIN, bad, NOW)).rejects.toThrow(
WebAuthnError,
)
})
})