fix(relay-auth): close 5 pre-production security findings (F1–F5)

F1 (HIGH): bind step-up freshness to the required method (stepUpMethod) so a
  login-time or weaker (TOTP) factor can no longer satisfy a passkey requirement.
F2 (HIGH): make the step-up gate host-driven and fail-closed on connect+reattach
  (deny when policy.required and the principal is missing/stale/wrong-method).
F3 (MED): bind the WebAuthn assertion to the stored credential (credentialId
  cross-check + forward publicKey/credentialId to the verifier).
F4 (LOW): make verifyDpopProof totally fail-safe (no unhandled throw), wrap the
  authz boundary into a clean audited 401, and validate cnfJkt format at issue.
F5 (LOW): return newSignCount from finishAuthentication so the caller can persist
  the advanced counter (WebAuthn clone detection).

All fixes ship regression tests. relay-auth: 122 tests green, tsc clean.
This commit is contained in:
Yaojia Wang
2026-07-02 16:41:18 +02:00
parent 1529d2c94c
commit a09c131539
15 changed files with 248 additions and 60 deletions

View File

@@ -1,10 +1,10 @@
import { describe, it, expect } from 'vitest'
import { needsStepUp, recordStepUp, policyForHost } from '../src/human/stepup/stepup.js'
import { needsStepUp, recordStepUp, policyForHost, NO_STEPUP_POLICY } from '../src/human/stepup/stepup.js'
import type { StepUpPolicy } from '../src/types.js'
import { principal, makeHost, uuid } from './_helpers.js'
const NOW = 1_700_000_000
const POLICY: StepUpPolicy = { maxAgeSeconds: 300, requiredMethod: 'passkey' }
const POLICY: StepUpPolicy = { required: true, maxAgeSeconds: 300, requiredMethod: 'passkey' }
describe('step-up before session open (T8, Finding-3)', () => {
it('fresh login but stepUpAt=null → needsStepUp true (login ≠ step-up)', () => {
@@ -13,34 +13,61 @@ describe('step-up before session open (T8, Finding-3)', () => {
})
it('stale step-up (older than maxAgeSeconds) → true', () => {
const p = principal('acct-A', { stepUpAt: NOW - 301, amr: ['passkey', 'stepup'] })
const p = principal('acct-A', { stepUpAt: NOW - 301, amr: ['passkey', 'stepup'], stepUpMethod: 'passkey' })
expect(needsStepUp(p, POLICY, NOW)).toBe(true)
})
it('required method absent from amr → true', () => {
const p = principal('acct-A', { stepUpAt: NOW, amr: ['totp'] })
it('required method not the LAST step-up method → true', () => {
const p = principal('acct-A', { stepUpAt: NOW, amr: ['totp'], stepUpMethod: 'totp' })
expect(needsStepUp(p, POLICY, NOW)).toBe(true)
})
it('fresh passkey step-up → false', () => {
const p = principal('acct-A', { stepUpAt: NOW, amr: ['passkey'] })
it('fresh passkey step-up satisfies passkey policy → false (F1)', () => {
const p = principal('acct-A', { stepUpAt: NOW, amr: ['passkey'], stepUpMethod: 'passkey' })
expect(needsStepUp(p, POLICY, NOW)).toBe(false)
})
it('fresh TOTP step-up does NOT satisfy passkey policy → true (F1 factor-downgrade regression)', () => {
// A weaker (TOTP) step-up performed just now must not satisfy a passkey requirement, even though
// it is fresh — the required method itself must be the LAST step-up.
const p = principal('acct-A', { stepUpAt: NOW, amr: ['passkey', 'totp'], stepUpMethod: 'totp' })
expect(needsStepUp(p, POLICY, NOW)).toBe(true)
})
it('login-time passkey in amr but no step-up (stepUpMethod null) → true (login ≠ step-up)', () => {
const p = principal('acct-A', { stepUpAt: NOW, amr: ['passkey'], stepUpMethod: null })
expect(needsStepUp(p, POLICY, NOW)).toBe(true)
})
it('recordStepUp returns a NEW principal (immutable), original untouched', () => {
const p = principal('acct-A', { stepUpAt: null, amr: ['totp'] })
const p = principal('acct-A', { stepUpAt: null, amr: ['totp'], stepUpMethod: 'totp' })
const stepped = recordStepUp(p, 'passkey', NOW)
expect(stepped).not.toBe(p)
expect(p.stepUpAt).toBeNull()
expect(p.amr).toEqual(['totp'])
expect(p.stepUpMethod).toBe('totp')
expect(stepped.stepUpAt).toBe(NOW)
expect(stepped.amr).toContain('passkey')
expect(needsStepUp(stepped, POLICY, NOW)).toBe(false)
})
it('policyForHost yields a passkey freshness requirement', () => {
it('recordStepUp stamps stepUpMethod with the method used (F1)', () => {
const p = principal('acct-A', { stepUpAt: null, amr: ['passkey'], stepUpMethod: null })
const stepped = recordStepUp(p, 'passkey', NOW)
expect(stepped.stepUpMethod).toBe('passkey')
expect(stepped.stepUpAt).toBe(NOW)
})
it('policyForHost yields a REQUIRED passkey freshness requirement', () => {
const policy = policyForHost(makeHost('acct-A', uuid()))
expect(policy.required).toBe(true)
expect(policy.requiredMethod).toBe('passkey')
expect(policy.maxAgeSeconds).toBeGreaterThan(0)
})
it('NO_STEPUP_POLICY is the v0.9 never-required default', () => {
expect(NO_STEPUP_POLICY.required).toBe(false)
expect(NO_STEPUP_POLICY.requiredMethod).toBe('passkey')
expect(NO_STEPUP_POLICY.maxAgeSeconds).toBeGreaterThan(0)
})
})