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

179 lines
6.8 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest'
import { authorizeConnect, authorizeReattach } from '../src/authz/decide.js'
import { accountIdFromToken, AuthzInputError } from '../src/authz/principal.js'
import { resetDpopCacheForTest } from '../src/capability/verify.js'
import type { CapabilityToken } from 'relay-contracts'
import {
setupP5SigningKey,
makeHost,
fakeHostRegistry,
fakeSessionRegistry,
fakeRevocationStore,
issueWithDpop,
uuid,
} from './_helpers.js'
const NOW = 1_700_000_000
const AUD_A = 'alice.term.example.com'
describe('deny-by-default authz (INV1/INV3/INV6)', () => {
let signingKey: CryptoKey
beforeEach(async () => {
;({ signingKey } = await setupP5SigningKey())
resetDpopCacheForTest()
})
it('happy path: account A + own host + attach right + live host → ok', async () => {
const hostA = uuid()
const hosts = fakeHostRegistry([makeHost('acct-A', hostA)])
const rev = fakeRevocationStore()
const { raw, dpop } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostA, aud: AUD_A, now: NOW })
const out = await authorizeConnect(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: hostA, requiredRight: 'attach' },
dpop,
hosts,
rev,
NOW,
)
expect(out.ok).toBe(true)
if (out.ok) {
expect(out.hostId).toBe(hostA)
expect(out.principal.accountId).toBe('acct-A')
expect(out.jti.length).toBeGreaterThan(0)
}
})
it('INV1 cross-tenant: A token but host owned by B → 403', async () => {
const hostB = uuid()
const hosts = fakeHostRegistry([makeHost('acct-B', hostB)]) // host owned by B
const rev = fakeRevocationStore()
// A mints a token scoped to hostB (IDOR attempt) — sub=acct-A, host=hostB
const { raw, dpop } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostB, aud: AUD_A, now: NOW })
const out = await authorizeConnect(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: hostB, requiredRight: 'attach' },
dpop,
hosts,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 403, reason: 'cross_tenant' })
})
it('accountIdFromToken returns sub; throws on empty sub', () => {
const tok = { sub: 'acct-A' } as CapabilityToken
expect(accountIdFromToken(tok)).toBe('acct-A')
expect(() => accountIdFromToken({ sub: '' } as CapabilityToken)).toThrow(AuthzInputError)
})
it('INV6 reattach cross-tenant: valid host-A token but session owned by B → 403', async () => {
const hostA = uuid()
const sessionB = uuid()
const hosts = fakeHostRegistry([makeHost('acct-A', hostA)])
const sessions = fakeSessionRegistry([{ sessionId: sessionB, hostId: uuid(), accountId: 'acct-B' }])
const rev = fakeRevocationStore()
const { raw, dpop } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostA, aud: AUD_A, now: NOW })
const out = await authorizeReattach(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: hostA, requiredRight: 'attach', sessionId: sessionB },
dpop,
hosts,
sessions,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 403, reason: 'cross_tenant_session' })
})
it('missing/invalid token → 401', async () => {
const hosts = fakeHostRegistry([])
const rev = fakeRevocationStore()
const out = await authorizeConnect(
{ capabilityRaw: 'garbage', expectedAud: AUD_A, requestedHostId: uuid(), requiredRight: 'attach' },
{ proofJws: 'x.y.z', htu: 'h', htm: 'GET' },
hosts,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 401 })
})
it('failing DPoP proof (PoP mismatch) → 401', async () => {
const hostA = uuid()
const hosts = fakeHostRegistry([makeHost('acct-A', hostA)])
const rev = fakeRevocationStore()
const { raw } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostA, aud: AUD_A, now: NOW })
const out = await authorizeConnect(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: hostA, requiredRight: 'attach' },
{ proofJws: 'a.b.c', htu: 'h', htm: 'GET' }, // bogus proof
hosts,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 401, reason: 'dpop_proof_failed' })
})
it('revoked jti → 403', async () => {
const hostA = uuid()
const hosts = fakeHostRegistry([makeHost('acct-A', hostA)])
const rev = fakeRevocationStore()
const { raw, dpop } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostA, aud: AUD_A, now: NOW })
// revoke the token's jti
const { verifyCapabilityToken } = await import('../src/capability/verify.js')
const tok = await verifyCapabilityToken(raw, AUD_A, NOW)
await rev.revokeJti(tok.jti, tok.exp)
const out = await authorizeConnect(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: hostA, requiredRight: 'attach' },
dpop,
hosts,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 403, reason: 'token_revoked' })
})
it('requiredRight=kill with attach-only token → 403', async () => {
const hostA = uuid()
const hosts = fakeHostRegistry([makeHost('acct-A', hostA)])
const rev = fakeRevocationStore()
const { raw, dpop } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostA, aud: AUD_A, rights: ['attach'], now: NOW })
const out = await authorizeConnect(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: hostA, requiredRight: 'kill' },
dpop,
hosts,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 403, reason: 'insufficient_rights' })
})
it('token.host !== requestedHostId (path-swap IDOR) → 403', async () => {
const hostA = uuid()
const otherHost = uuid()
const hosts = fakeHostRegistry([makeHost('acct-A', hostA), makeHost('acct-A', otherHost)])
const rev = fakeRevocationStore()
const { raw, dpop } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostA, aud: AUD_A, now: NOW })
const out = await authorizeConnect(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: otherHost, requiredRight: 'attach' },
dpop,
hosts,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 403, reason: 'host_scope_mismatch' })
})
it('revoked host status → 403', async () => {
const hostA = uuid()
const hosts = fakeHostRegistry([makeHost('acct-A', hostA, 'revoked')])
const rev = fakeRevocationStore()
const { raw, dpop } = await issueWithDpop(signingKey, { accountId: 'acct-A', host: hostA, aud: AUD_A, now: NOW })
const out = await authorizeConnect(
{ capabilityRaw: raw, expectedAud: AUD_A, requestedHostId: hostA, requiredRight: 'attach' },
dpop,
hosts,
rev,
NOW,
)
expect(out).toMatchObject({ ok: false, status: 403, reason: 'host_revoked' })
})
})