Skip to content

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

TypeScript
2.6 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { readFileSync } from 'node:fs'
import { LIMITS as GROK_LIMITS } from '@clerum/grok-provider-attempt-contract'
import { LIMITS as CODEX_LIMITS } from '@clerum/llm-provider-attempt-contract'
import { AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES } from '../src/services/llmProviderAttemptAuthorizer.js'

const AUTHORIZE_LOCATION = 'location = /api/v1/mcp-host/llm/provider-attempts/authorize {'
const CANONICAL_PROXY_PASS =
  'proxy_pass http://control_api_upstream/api/v1/mcp-host/llm/provider-attempts/authorize;'

/** The body of one nginx `location` block, from its opening line to its closing brace. */
function locationBlock(config: string, opening: string): string {
  const start = config.indexOf(opening)
  expect(start, `configmaps.yaml must declare ${opening}`).toBeGreaterThanOrEqual(0)
  const end = config.indexOf('\n        }', start)
  expect(end).toBeGreaterThan(start)
  return config.slice(start, end)
}

describe('LLM authorize route body limit (#731 R3-3)', () => {
  it('T-R3-3d covers the request cap plus the envelope allowance with one gateway directive', () => {
    const config = readFileSync(
      new URL('../../deploy/base/control-plane/configmaps.yaml', import.meta.url),
      'utf-8'
    )
    const block = locationBlock(config, AUTHORIZE_LOCATION)
    // Witness: this is the block that proxies to control-api. It forwards the
    // canonical URI, so an alias nginx admitted under this allowance cannot
    // reach control-api on a path its parser exemption does not recognise.
    expect(block).toContain(CANONICAL_PROXY_PASS)
    expect(block.replace(/#.*$/gm, '').match(/\bproxy_pass\b/g)).toHaveLength(1)
    const directive = /client_max_body_size\s+(\d+);/.exec(block)
    expect(
      directive,
      'the authorize location must set client_max_body_size in bytes'
    ).not.toBeNull()
    // nginx refuses to start when a location repeats the directive.
    expect(block.match(/client_max_body_size/g)).toHaveLength(1)
    // Without the directive nginx applies its 1m default and refuses a
    // contract-valid request before control-api sees it. The same directive
    // also carries the Codex (#660) and Grok (#784) V2 visual envelopes, so it
    // is the largest of the three budgets.
    const cap = Math.max(CODEX_LIMITS.maxRequestBodyBytes, GROK_LIMITS.maxRequestBodyBytes)
    expect(Number(directive![1])).toBe(
      Math.max(
        cap + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES,
        CODEX_LIMITS.maxVisualRequestBodyBytes,
        GROK_LIMITS.maxVisualRequestBodyBytes
      )
    )
    expect(Number(directive![1])).toBeGreaterThanOrEqual(cap + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES)
  })
})