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

134 lines
5.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { mountPasskeyLogin, type PasskeyAuthApi } from '../src/login-passkey'
import { WebAuthnError, type AuthenticationResult, type WebAuthnClient } from '../src/webauthn'
import type { ApiClient } from '../src/api-client'
const fakeAssertion: AuthenticationResult = {
id: 'x',
rawId: 'cmF3',
type: 'public-key',
response: { clientDataJSON: 'Y2Q', authenticatorData: 'YWQ', signature: 'c2ln', userHandle: null },
}
function reqOptions(rpId: string): PublicKeyCredentialRequestOptions {
return { challenge: new Uint8Array([1]).buffer, rpId } as PublicKeyCredentialRequestOptions
}
const stubApi = {} as ApiClient
describe('mountPasskeyLogin (T7)', () => {
let root: HTMLElement
beforeEach(() => {
root = document.createElement('div')
document.body.append(root)
})
it('successful authenticate → assertion verified → "ok"', async () => {
const wa: WebAuthnClient = { register: vi.fn(), authenticate: vi.fn(async () => fakeAssertion) }
const authApi: PasskeyAuthApi = {
getLoginChallenge: vi.fn(async () => reqOptions('app.example.com')),
verifyLogin: vi.fn(async () => 'ok' as const),
getStepUpChallenge: vi.fn(),
verifyStepUp: vi.fn(),
}
const login = mountPasskeyLogin(root, stubApi, wa, authApi)
await expect(login.login()).resolves.toBe('ok')
expect(authApi.verifyLogin).toHaveBeenCalledWith(fakeAssertion)
})
it('user cancels the passkey prompt → "rejected", no session, no throw leak', async () => {
const wa: WebAuthnClient = {
register: vi.fn(),
authenticate: vi.fn(async () => {
throw new WebAuthnError('cancelled', 'dismissed')
}),
}
const authApi: PasskeyAuthApi = {
getLoginChallenge: vi.fn(async () => reqOptions('app.example.com')),
verifyLogin: vi.fn(async () => 'ok' as const),
getStepUpChallenge: vi.fn(),
verifyStepUp: vi.fn(),
}
const login = mountPasskeyLogin(root, stubApi, wa, authApi)
await expect(login.login()).resolves.toBe('rejected')
expect(authApi.verifyLogin).not.toHaveBeenCalled()
})
it('stepUp runs a FRESH ceremony each call (a stolen cookie alone cannot open a session)', async () => {
const authenticate = vi.fn(async () => fakeAssertion)
const wa: WebAuthnClient = { register: vi.fn(), authenticate }
const authApi: PasskeyAuthApi = {
getLoginChallenge: vi.fn(),
verifyLogin: vi.fn(),
getStepUpChallenge: vi.fn(async () => reqOptions('app.example.com')),
verifyStepUp: vi.fn(async () => 'ok' as const),
}
const login = mountPasskeyLogin(root, stubApi, wa, authApi)
await login.stepUp('h1')
await login.stepUp('h1')
expect(authenticate).toHaveBeenCalledTimes(2) // not cached — a new assertion every time
})
it('rpId comes from the SERVER challenge, never location.hostname (§8 Q#5)', async () => {
const authenticate = vi.fn((_c: PublicKeyCredentialRequestOptions) => Promise.resolve(fakeAssertion))
const wa: WebAuthnClient = { register: vi.fn(), authenticate }
const authApi: PasskeyAuthApi = {
getLoginChallenge: vi.fn(async () => reqOptions('app.example.com')),
verifyLogin: vi.fn(async () => 'ok' as const),
getStepUpChallenge: vi.fn(),
verifyStepUp: vi.fn(),
}
const login = mountPasskeyLogin(root, stubApi, wa, authApi)
await login.login()
const passed = authenticate.mock.calls[0]![0] as PublicKeyCredentialRequestOptions
expect(passed.rpId).toBe('app.example.com')
expect(passed.rpId).not.toBe(window.location.hostname)
})
it('a failed challenge fetch → "rejected" with an error message (no throw leak)', async () => {
const wa: WebAuthnClient = { register: vi.fn(), authenticate: vi.fn(async () => fakeAssertion) }
const authApi: PasskeyAuthApi = {
getLoginChallenge: vi.fn(async () => {
throw new Error('challenge 500')
}),
verifyLogin: vi.fn(),
getStepUpChallenge: vi.fn(),
verifyStepUp: vi.fn(),
}
const login = mountPasskeyLogin(root, stubApi, wa, authApi)
await expect(login.login()).resolves.toBe('rejected')
expect(root.querySelector('.passkey-error')?.textContent).toContain('Could not start')
})
it('a non-cancel authenticate failure → "rejected" and a failure message', async () => {
const wa: WebAuthnClient = {
register: vi.fn(),
authenticate: vi.fn(async () => {
throw new Error('authenticator exploded')
}),
}
const authApi: PasskeyAuthApi = {
getLoginChallenge: vi.fn(async () => reqOptions('app.example.com')),
verifyLogin: vi.fn(),
getStepUpChallenge: vi.fn(),
verifyStepUp: vi.fn(),
}
const login = mountPasskeyLogin(root, stubApi, wa, authApi)
await expect(login.login()).resolves.toBe('rejected')
expect(root.querySelector('.passkey-error')?.textContent).toContain('failed')
})
it('exposes EXACTLY login + stepUp — no phone/SMS/OTP field (never SMS)', () => {
const wa: WebAuthnClient = { register: vi.fn(), authenticate: vi.fn() }
const authApi = {
getLoginChallenge: vi.fn(),
verifyLogin: vi.fn(),
getStepUpChallenge: vi.fn(),
verifyStepUp: vi.fn(),
} as unknown as PasskeyAuthApi
const login = mountPasskeyLogin(root, stubApi, wa, authApi)
expect(Object.keys(login).sort()).toEqual(['login', 'stepUp'])
expect(JSON.stringify(Object.keys(login))).not.toMatch(/sms|phone|otp/i)
})
})