Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
12.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import fc from 'fast-check'
import { buildDcrRequest, registerDynamicClient } from '../src/oauth/dcr.js'
import type { DiscoveryResult } from '../src/oauth/discovery.js'
import {
  DCR_BASIC_REGISTRATION_RESPONSE,
  DCR_CONFIDENTIAL_REGISTRATION_RESPONSE,
  DCR_NONSTRING_AUTH_METHOD_REGISTRATION_JSON,
  DCR_PUBLIC_REGISTRATION_RESPONSE,
  DCR_PUBLIC_WITH_ECHOED_SECRET_REGISTRATION_JSON,
  DCR_REGISTRATION_ENDPOINT,
  DCR_VERCEL_DOWNGRADE_REGISTRATION_JSON,
  makeDcrTransport,
} from './fixtures/remoteOAuthDiscovery.js'

const PUBLIC_IP = async () => ['93.184.216.34']

// Endpoint origin. The public-boundary CI guard
// (scripts/tests/test-minikube-t2-public-boundary.sh) reads a quoted
// `token: '…'` literal as a materialized credential; deriving the endpoint from
// an interpolated origin keeps the value out of that shape (the guard exempts
// interpolated values). Do not inline this back to a plain string literal.
const NOTION = 'https://mcp.notion.com'

function discoveryWith(supportsRefresh: boolean): DiscoveryResult {
  return {
    prm: { resource: 'https://mcp.notion.com' },
    as: {
      issuer: 'https://mcp.notion.com',
      authorization_endpoint: 'https://mcp.notion.com/authorize',
      token_endpoint: 'https://mcp.notion.com/token',
    },
    resource: 'https://mcp.notion.com',
    issuer: 'https://mcp.notion.com',
    endpoints: {
      authorization: `${NOTION}/authorize`,
      token: `${NOTION}/token`,
      registration: DCR_REGISTRATION_ENDPOINT,
    },
    registrationMode: 'dcr',
    quirks: { bearerInBody: false, supportsRefresh },
  }
}

const REDIRECT_URIS = ['https://control.example.com/api/v1/oauth-callback/remote']

describe('buildDcrRequest (RFC 7591, pure) — T2 property', () => {
  it('always: response_types=[code], auth method by mode, redirect_uris passthrough, refresh iff advertised', () => {
    fc.assert(
      fc.property(
        fc.constantFrom<'public' | 'confidential'>('public', 'confidential'),
        fc.boolean(),
        fc.array(fc.webUrl(), { minLength: 1, maxLength: 3 }),
        fc.option(fc.array(fc.string({ minLength: 1 }), { maxLength: 4 }), { nil: undefined }),
        (clientMode, supportsRefresh, redirectUris, scopes) => {
          const req = buildDcrRequest(discoveryWith(supportsRefresh), {
            clientMode,
            redirectUris,
            scopes,
          })
          expect(req.response_types).toEqual(['code'])
          expect(req.client_name).toBe('Evenfire')
          expect(req.application_type).toBe('web')
          expect(req.token_endpoint_auth_method).toBe(
            clientMode === 'public' ? 'none' : 'client_secret_post'
          )
          expect(req.redirect_uris).toEqual(redirectUris)
          expect(req.grant_types).toContain('authorization_code')
          expect(req.grant_types.includes('refresh_token')).toBe(supportsRefresh)
          if (scopes && scopes.length > 0) {
            expect(req.scope).toBe(scopes.join(' '))
          } else {
            expect(req.scope).toBeUndefined()
          }
        }
      )
    )
  })
})

describe('registerDynamicClient (RFC 7591, effectful via injected pinned transport)', () => {
  const publicReq = buildDcrRequest(discoveryWith(true), {
    clientMode: 'public',
    redirectUris: REDIRECT_URIS,
  })
  const confReq = buildDcrRequest(discoveryWith(true), {
    clientMode: 'confidential',
    redirectUris: REDIRECT_URIS,
  })

  it('an oversized client_id → invalid_response carrying the RFC 7592 handle for cleanup', async () => {
    const { transport } = makeDcrTransport({
      responseJson: JSON.stringify({
        ...DCR_CONFIDENTIAL_REGISTRATION_RESPONSE,
        client_id: `https://as.example.com/${'/'.repeat(200_000)}a`,
      }),
    })
    const started = performance.now()
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      confReq
    )
    expect(performance.now() - started).toBeLessThan(200)
    expect(outcome.ok).toBe(false)
    if (outcome.ok) return
    expect(outcome.error).toMatchObject({
      kind: 'invalid_response',
      minted: true,
      registrationClientUri: DCR_CONFIDENTIAL_REGISTRATION_RESPONSE.registration_client_uri,
      registrationAccessToken: DCR_CONFIDENTIAL_REGISTRATION_RESPONSE.registration_access_token,
    })
  })

  it('client_id length: 512 bytes accepted, 513 → invalid_response (the CRD caps spec.oauth.id at 512)', async () => {
    for (const [length, ok] of [
      [512, true],
      [513, false],
    ] as const) {
      const { transport } = makeDcrTransport({
        responseJson: JSON.stringify({
          ...DCR_PUBLIC_REGISTRATION_RESPONSE,
          client_id: 'c'.repeat(length),
        }),
      })
      const outcome = await registerDynamicClient(
        { transport, resolveDns: PUBLIC_IP },
        DCR_REGISTRATION_ENDPOINT,
        publicReq
      )
      expect(outcome.ok).toBe(ok)
      if (!outcome.ok)
        expect(outcome.error).toMatchObject({ kind: 'invalid_response', minted: true })
    }
  })

  it('public: parses the RFC 7591 response and returns the assigned client_id', async () => {
    const { transport, calls } = makeDcrTransport({
      responseJson: JSON.stringify(DCR_PUBLIC_REGISTRATION_RESPONSE),
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      publicReq
    )
    expect(outcome.ok).toBe(true)
    if (outcome.ok) expect(outcome.response.client_id).toBe('dyn-public-6f1c2a')
    // Single hop POST with content-type + content-length.
    expect(calls).toHaveLength(1)
    expect(calls[0].method).toBe('POST')
    expect(calls[0].headers['content-type']).toBe('application/json')
    expect(calls[0].headers['content-length']).toBe(String(Buffer.byteLength(calls[0].body ?? '')))
  })

  it('confidential: returns the client_secret + registration_access_token', async () => {
    const { transport } = makeDcrTransport({
      responseJson: JSON.stringify(DCR_CONFIDENTIAL_REGISTRATION_RESPONSE),
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      confReq
    )
    expect(outcome.ok).toBe(true)
    if (outcome.ok) {
      expect(outcome.response.client_secret).toBe('fixture-client-secret-not-probed')
      expect(outcome.response.registration_access_token).toBe('fixture-reg-access-token-not-probed')
      // The AS echoed `client_secret_post` WITH a secret → effective confidential.
      expect(outcome.effectiveAuthMethod).toBe('client_secret_post')
      expect(outcome.effectiveClientMode).toBe('confidential')
    }
  })

  it('DOWNGRADE: confidential request, AS returns `none` + no client_secret (Vercel) → ok as public', async () => {
    // Regression for the real Vercel DCR: a confidential (`client_secret_post`)
    // request that the AS downgrades to a public client. The guard must anchor on the
    // EFFECTIVE auth method, not the requested one — this returned `invalid_response`
    // before the fix (rejecting a perfectly usable PKCE public client).
    const { transport } = makeDcrTransport({
      responseJson: DCR_VERCEL_DOWNGRADE_REGISTRATION_JSON,
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      confReq
    )
    expect(outcome.ok).toBe(true)
    if (outcome.ok) {
      expect(outcome.effectiveAuthMethod).toBe('none')
      expect(outcome.effectiveClientMode).toBe('public')
      expect(outcome.response.client_id).toBe('cl_WbdtcToDrMR4ZHvXLGAmbfoYCsQjMeS8')
      // A downgraded public client carries NO secret.
      expect(outcome.response.client_secret).toBeUndefined()
    }
  })

  it('ADVERSARIAL: AS returns `none` but ALSO echoes a client_secret → classified public', async () => {
    const { transport } = makeDcrTransport({
      responseJson: DCR_PUBLIC_WITH_ECHOED_SECRET_REGISTRATION_JSON,
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      confReq
    )
    expect(outcome.ok).toBe(true)
    if (outcome.ok) {
      // The auth method is the authority: `none` ⇒ public, regardless of an echoed secret.
      expect(outcome.effectiveAuthMethod).toBe('none')
      expect(outcome.effectiveClientMode).toBe('public')
    }
  })

  it('FAIL-CLOSED: AS assigns a NON-STRING token_endpoint_auth_method → auth_method_unsupported', async () => {
    const { transport } = makeDcrTransport({
      responseJson: DCR_NONSTRING_AUTH_METHOD_REGISTRATION_JSON,
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      confReq
    )
    expect(outcome.ok).toBe(false)
    if (!outcome.ok) {
      expect(outcome.error.kind).toBe('auth_method_unsupported')
      // Minted-but-rejected: the RFC 7592 handle is exposed so the caller can clean up.
      if (outcome.error.kind === 'auth_method_unsupported') {
        expect(outcome.error.registrationClientUri).toBe(
          'https://mcp.notion.com/register/cl_NonStringAuthMethod'
        )
        expect(outcome.error.registrationAccessToken).toBe('fixture-reg-access-token-not-probed')
      }
    }
  })

  it('FAIL-CLOSED: AS assigns client_secret_basic → auth_method_unsupported (nothing usable)', async () => {
    const { transport } = makeDcrTransport({
      responseJson: JSON.stringify(DCR_BASIC_REGISTRATION_RESPONSE),
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      confReq
    )
    expect(outcome.ok).toBe(false)
    if (!outcome.ok) expect(outcome.error.kind).toBe('auth_method_unsupported')
  })

  it('a 3xx on the registration POST is redirect_blocked (never re-POSTed to Location)', async () => {
    const { transport } = makeDcrTransport({ responseJson: '', status: 302 })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      publicReq
    )
    expect(outcome.ok).toBe(false)
    if (!outcome.ok) expect(outcome.error.kind).toBe('redirect_blocked')
  })

  it('a 4xx with an OAuth error body surfaces error/error_description', async () => {
    const { transport } = makeDcrTransport({
      responseJson: JSON.stringify({
        error: 'invalid_redirect_uri',
        error_description: 'unregistered uri',
      }),
      status: 400,
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      publicReq
    )
    expect(outcome.ok).toBe(false)
    if (!outcome.ok) {
      expect(outcome.error.kind).toBe('fetch_failed')
      if (outcome.error.kind === 'fetch_failed') {
        expect(outcome.error.status).toBe(400)
        expect(outcome.error.detail).toContain('invalid_redirect_uri')
        expect(outcome.error.detail).toContain('unregistered uri')
      }
    }
  })

  it('a 2xx body without client_id is invalid_response', async () => {
    const { transport } = makeDcrTransport({ responseJson: JSON.stringify({ foo: 'bar' }) })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      publicReq
    )
    expect(outcome.ok).toBe(false)
    if (!outcome.ok) expect(outcome.error.kind).toBe('invalid_response')
  })

  it('a confidential registration that returns no client_secret is invalid_response', async () => {
    const { transport } = makeDcrTransport({
      responseJson: JSON.stringify({
        client_id: 'x',
        token_endpoint_auth_method: 'client_secret_post',
      }),
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      DCR_REGISTRATION_ENDPOINT,
      confReq
    )
    expect(outcome.ok).toBe(false)
    if (!outcome.ok) expect(outcome.error.kind).toBe('invalid_response')
  })

  it('a non-https registration endpoint is kernel_rejected before any POST', async () => {
    const { transport, calls } = makeDcrTransport({
      responseJson: JSON.stringify(DCR_PUBLIC_REGISTRATION_RESPONSE),
    })
    const outcome = await registerDynamicClient(
      { transport, resolveDns: PUBLIC_IP },
      'http://mcp.notion.com/register',
      publicReq
    )
    expect(outcome.ok).toBe(false)
    if (!outcome.ok) expect(outcome.error.kind).toBe('kernel_rejected')
    // Fail-closed: the transport was never invoked.
    expect(calls).toHaveLength(0)
  })
})