Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): forward the canonical authorize URI and normalize the exemption
Alfredo Lopez950e202449c9
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 6792f55e7..177496fb9 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.716",+ "version": "0.1.717","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.716",+ "version": "0.1.717","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex 8ee1b9f0e..fa9a4b96e 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.716",+ "version": "0.1.717","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/src/app.ts b/control-api/src/app.tsindex 86e5c756c..5459f2759 100644--- a/control-api/src/app.ts+++ b/control-api/src/app.ts@@ -1,4 +1,5 @@import express from 'express'+import { posix } from 'node:path'import { config } from './config.js'import type { DbClient } from './db.js'import { clerumErrorHandler } from './http/errorHandler.js'@@ -72,6 +73,21 @@ const GFS_UPLOAD_PART_PATH =// The global 150mb parser must not buffer an unauthenticated body.const LLM_AUTHORIZE_PATH = /^\/api\/v1\/mcp-host\/llm\/provider-attempts\/authorize\/?$/i+// nginx selects the authorize location on the percent-decoded, dot-resolved,+// slash-collapsed URI. Apply the same canonicalization here, so an alias that+// nginx admitted with the 35 MiB allowance cannot reach the global parser.+// Returns null when the path holds a malformed percent escape.+function canonicalRequestPath(path: string): string | null {+ let decoded: string+ try {+ decoded = decodeURIComponent(path)+ } catch (err) {+ if (err instanceof URIError) return null+ throw err+ }+ return posix.normalize(decoded).replace(/\/+/g, '/')+}+export function createApp(gateway: K8sGateway) {const traceIngestDb: DbClient = meterTracingDbClient({query: (text, params) => getTracingPools().traceIngestPool.query(text, params),@@ -95,11 +111,23 @@ export function createApp(gateway: K8sGateway) {next()return}- // Express permits repeated separators at router mount boundaries. Match- // the exemption on the normalized path without changing request routing.- if (req.method === 'POST' && LLM_AUTHORIZE_PATH.test(req.path.replace(/\/+/g, '/'))) {- next()- return+ // Match the exemption on the canonical path without changing request+ // routing. An alias that is not the routed path gets no body parser at+ // all and ends in the routing 401/404; only the exact route parses its+ // body, after the limiter and JWT. Express also permits repeated+ // separators at router mount boundaries, which the canonical form covers.+ if (req.method === 'POST') {+ const canonicalPath = canonicalRequestPath(req.path)+ if (canonicalPath === null) {+ // The path cannot be classified, so its body is not parsed. The raw+ // URI is not logged: it is caller-controlled.+ res.status(400).json({ error: 'invalid_request' })+ return+ }+ if (LLM_AUTHORIZE_PATH.test(canonicalPath)) {+ next()+ return+ }}jsonBodyParser(req, res, next)})diff --git a/control-api/test/deploy.controlPlaneUnavailableGateway.test.ts b/control-api/test/deploy.controlPlaneUnavailableGateway.test.tsindex 1dd1bc8c3..722873daf 100644--- a/control-api/test/deploy.controlPlaneUnavailableGateway.test.ts+++ b/control-api/test/deploy.controlPlaneUnavailableGateway.test.ts@@ -51,6 +51,13 @@ const GROK_REDEEM = 'location = /api/v1/internal/llm/grok/provider-attempts/redeconst CODEX_FINALIZE = 'location = /api/v1/internal/llm/provider-attempts/finalize {'const GROK_FINALIZE = 'location = /api/v1/internal/llm/grok/provider-attempts/finalize {'+// Authorize forwards the canonical URI: its exact location matches the+// normalized URI, and a proxy_pass without a URI part would forward the raw+// alias. Redeem keeps the raw forward.+const AUTHORIZE_PROXY_PASS =+ 'proxy_pass http://control_api_upstream/api/v1/mcp-host/llm/provider-attempts/authorize;'+const RAW_PROXY_PASS = 'proxy_pass http://control_api_upstream;'+/** One ConfigMap document of configmaps.yaml, selected by its metadata name. */function configMap(name: string): string {const docs = CONFIG.split('\n---\n').filter(doc => doc.includes(`\n name: ${name}\n`))@@ -85,15 +92,19 @@ const RPC_GATEWAY = 'control-api-rpc-gateway'describe('gateway-generated 502 on user-facing control-plane hops (G1-10, #720)', () => {it.each([- ['H2 authorize', WORKFLOW_APPROVAL_GATEWAY, AUTHORIZE],- ['H3 Codex redeem', RPC_GATEWAY, CODEX_REDEEM],- ['H3 Grok redeem', RPC_GATEWAY, GROK_REDEEM],- ])('G1-10a: %s answers its own 502 as control_plane_unavailable', (_hop, gateway, opening) => {- const block = locationBlock(directives(configMap(gateway)), opening)- // Witness: this is the block that proxies to control-api.- expect(block).toContain('proxy_pass http://control_api_upstream;')- expect(count(block, ERROR_PAGE)).toBe(1)- })+ ['H2 authorize', WORKFLOW_APPROVAL_GATEWAY, AUTHORIZE, AUTHORIZE_PROXY_PASS],+ ['H3 Codex redeem', RPC_GATEWAY, CODEX_REDEEM, RAW_PROXY_PASS],+ ['H3 Grok redeem', RPC_GATEWAY, GROK_REDEEM, RAW_PROXY_PASS],+ ])(+ 'G1-10a: %s answers its own 502 as control_plane_unavailable',+ (_hop, gateway, opening, proxyPass) => {+ const block = locationBlock(directives(configMap(gateway)), opening)+ // Witness: this is the block that proxies to control-api.+ expect(block).toContain(proxyPass)+ expect(count(block, /\bproxy_pass\b/g)).toBe(1)+ expect(count(block, ERROR_PAGE)).toBe(1)+ }+ )it.each([WORKFLOW_APPROVAL_GATEWAY, RPC_GATEWAY])('G1-10b: %s answers 503 JSON control_plane_unavailable with no-store headers',@@ -153,15 +164,19 @@ const CONNECT_FAILED = 'if ($upstream_connect_time = "-") {'describe('gateway-generated connect-timeout 504 on user-facing control-plane hops (G1-12, #820)', () => {it.each([- ['H2 authorize', WORKFLOW_APPROVAL_GATEWAY, AUTHORIZE],- ['H3 Codex redeem', RPC_GATEWAY, CODEX_REDEEM],- ['H3 Grok redeem', RPC_GATEWAY, GROK_REDEEM],- ])('G1-12a: %s sends its own 504 to the connect-timeout check', (_hop, gateway, opening) => {- const block = locationBlock(directives(configMap(gateway)), opening)- // Witness: this is the block that proxies to control-api.- expect(block).toContain('proxy_pass http://control_api_upstream;')- expect(count(block, CONNECT_TIMEOUT_ERROR_PAGE)).toBe(1)- })+ ['H2 authorize', WORKFLOW_APPROVAL_GATEWAY, AUTHORIZE, AUTHORIZE_PROXY_PASS],+ ['H3 Codex redeem', RPC_GATEWAY, CODEX_REDEEM, RAW_PROXY_PASS],+ ['H3 Grok redeem', RPC_GATEWAY, GROK_REDEEM, RAW_PROXY_PASS],+ ])(+ 'G1-12a: %s sends its own 504 to the connect-timeout check',+ (_hop, gateway, opening, proxyPass) => {+ const block = locationBlock(directives(configMap(gateway)), opening)+ // Witness: this is the block that proxies to control-api.+ expect(block).toContain(proxyPass)+ expect(count(block, /\bproxy_pass\b/g)).toBe(1)+ expect(count(block, CONNECT_TIMEOUT_ERROR_PAGE)).toBe(1)+ }+ )it.each([WORKFLOW_APPROVAL_GATEWAY, RPC_GATEWAY])('G1-12b: %s answers control_plane_unavailable only when no connection was made, else its own 504',@@ -204,7 +219,7 @@ describe('gateway-generated connect-timeout 504 on user-facing control-plane hopexpect(connectTimeoutMs(approval)).toBe(5000)const block = locationBlock(approval, AUTHORIZE)// Witness: the authorize block was read.- expect(block).toContain('proxy_pass http://control_api_upstream;')+ expect(block).toContain(AUTHORIZE_PROXY_PASS)expect(block).not.toMatch(/\bproxy_connect_timeout\b/)})diff --git a/control-api/test/deploy.llmAuthorizeBodyLimit.test.ts b/control-api/test/deploy.llmAuthorizeBodyLimit.test.tsindex 00220ad83..e50a200d8 100644--- a/control-api/test/deploy.llmAuthorizeBodyLimit.test.ts+++ b/control-api/test/deploy.llmAuthorizeBodyLimit.test.ts@@ -5,6 +5,8 @@ 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 {@@ -22,8 +24,11 @@ describe('LLM authorize route body limit (#731 R3-3)', () => {'utf-8')const block = locationBlock(config, AUTHORIZE_LOCATION)- // Witness: this is the block that proxies to control-api.- expect(block).toContain('proxy_pass http://control_api_upstream;')+ // 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,diff --git a/control-api/test/network.gateway.intent.test.ts b/control-api/test/network.gateway.intent.test.tsindex 897b75717..6e398cf23 100644--- a/control-api/test/network.gateway.intent.test.ts+++ b/control-api/test/network.gateway.intent.test.ts@@ -380,6 +380,26 @@ describe('network/gateway intent (manifest-level)', () => {expect(gatewayConf.match(/client_max_body_size/g)).toHaveLength(1)})+ it('forwards the canonical authorize URI, not the raw alias nginx matched', () => {+ const configmaps = read(`${BASE}/control-plane/configmaps.yaml`)+ const gatewayConf = docContaining(yamlDocs(configmaps), 'name: nginx-workflow-approval-gateway')+ const authorize = locationBlock(+ gatewayConf,+ 'location = /api/v1/mcp-host/llm/provider-attempts/authorize'+ )+ // The exact location matches the normalized URI. A proxy_pass with a URI+ // part replaces it with that canonical path; without one, nginx forwards+ // %61uthorize or ./authorize unchanged and control-api's parser exemption+ // would not recognise them.+ const proxyPasses = authorize+ .split('\n')+ .map(line => line.replace(/#.*$/, '').trim())+ .filter(line => line.startsWith('proxy_pass '))+ expect(proxyPasses).toEqual([+ 'proxy_pass http://control_api_upstream/api/v1/mcp-host/llm/provider-attempts/authorize;',+ ])+ })+it('pins the shipped Codex proxy visual envelope to 24MiB', () => {const proxy = read(`${BASE}/control-plane/codex-llm-proxy.yaml`)// #731: the ordinary body limit is derived from the contract cap plus thediff --git a/control-api/test/routes.mcp-host.llmProviderAttempts.createApp.test.ts b/control-api/test/routes.mcp-host.llmProviderAttempts.createApp.test.tsindex bcbd2ce39..c5c76850d 100644--- a/control-api/test/routes.mcp-host.llmProviderAttempts.createApp.test.ts+++ b/control-api/test/routes.mcp-host.llmProviderAttempts.createApp.test.ts@@ -1,7 +1,7 @@import { beforeEach, describe, expect, it, vi } from 'vitest'import { createServer, request as httpRequest } from 'node:http'import { gzipSync } from 'node:zlib'-import { registry } from '../src/observability/metrics.js'+import { rateLimitHitsTotal, registry } from '../src/observability/metrics.js'import * as authorizer from '../src/services/llmProviderAttemptAuthorizer.js'import { issueMcpHostAccessJwt } from '../src/utils/auth/mcpHostJwtToken.js'import { MockGateway } from './mockGateway.js'@@ -240,6 +240,202 @@ describe('createApp authorize parser boundary', () => {}})+const CANONICAL_AUTHORIZE_PATH = '/api/v1/mcp-host/llm/provider-attempts/authorize'++// nginx selects the exact authorize location (35 MiB body allowance) after+// percent-decoding, dot-segment resolution and slash collapsing. Each alias+// here is one nginx admits there; none is the path Express routes to the+// authorize handler. `status` is what the real app answers: no route matches+// the alias, and an authentication gate refuses it (the bearer is a valid+// mcp-host JWT, not an internal service token).+const RAW_AUTHORIZE_ALIASES = [+ {+ name: 'percent-encoded letter',+ path: '/api/v1/mcp-host/llm/provider-attempts/%61uthorize',+ status: 401,+ },+ {+ name: 'dot segment',+ path: '/api/v1/mcp-host/llm/provider-attempts/./authorize',+ status: 401,+ },+ {+ name: 'encoded slash',+ path: '/api/v1/mcp-host/llm/provider-attempts%2Fauthorize',+ status: 401,+ },+ {+ name: 'parent segment',+ path: '/api/v1/mcp-host/llm/provider-attempts/x/../authorize',+ status: 401,+ },+ {+ name: 'encoded parent segment',+ path: '/api/v1/mcp-host/llm/provider-attempts/x/%2e%2e/authorize',+ status: 401,+ },+] as const++/**+ * POST with the request target sent byte-for-byte. fetch() resolves dot+ * segments before sending, so it cannot produce `/./` or `/../` aliases.+ */+function postRaw(+ baseUrl: string,+ path: string,+ requestHeaders: Record<string, string>,+ body: string+): Promise<{ status: number; body: string }> {+ const { hostname, port } = new URL(baseUrl)+ const payload = Buffer.from(body)+ return new Promise((resolve, reject) => {+ const req = httpRequest(+ {+ hostname,+ port,+ method: 'POST',+ path,+ headers: { ...requestHeaders, 'content-length': payload.length },+ },+ res => {+ const status = res.statusCode+ if (status === undefined) {+ res.resume()+ reject(new Error('response has no status'))+ return+ }+ const chunks: string[] = []+ res.setEncoding('utf8')+ res.on('data', (chunk: string) => chunks.push(chunk))+ res.on('error', reject)+ res.on('end', () => resolve({ status, body: chunks.join('') }))+ }+ )+ req.on('error', reject)+ req.setTimeout(5_000, () => req.destroy(new Error('raw path probe timed out')))+ req.end(payload)+ })+}++/** Requests the authorize route's own limiter admitted (its bucket is unique to that route). */+function authorizeLimiterAdmissions(): number {+ const calls: unknown[][] = vi.mocked(rateLimitHitsTotal.inc).mock.calls+ return calls.filter(+ ([labels]) =>+ typeof labels === 'object' &&+ labels !== null &&+ (labels as { bucket_type?: unknown }).bucket_type === 'llm_provider_attempt_authorize'+ ).length+}++describe('createApp authorize parser exemption on raw path aliases', () => {+ const p = PROVIDERS[0]+ const success = {+ providerAttemptId: 'attempt-1',+ requestHash: 'a'.repeat(64),+ executionTicket: 'test-execution-ticket',+ expiresAt: '2026-01-01T00:00:00.000Z',+ }++ beforeEach(() => {+ vi.mocked(authorizer.authorizeLlmProviderAttempt).mockReset()+ vi.mocked(rateLimitHitsTotal.inc).mockClear()+ })++ /**+ * Liveness witness for the negative assertions of each test: the canonical+ * path, with the same JWT, passes the authorize limiter, has its body+ * parsed (seen by the same JSON.parse spy) and reaches the service.+ */+ async function expectCanonicalControl(+ url: string,+ parse: { mock: { calls: unknown[][] } },+ path = CANONICAL_AUTHORIZE_PATH+ ): Promise<void> {+ const admissionsBefore = authorizeLimiterAdmissions()+ vi.mocked(authorizer.authorizeLlmProviderAttempt).mockResolvedValueOnce(success)+ const response = await postRaw(url, path, headers(p), bodyOf(p))+ expect(response.status).toBe(200)+ expect(JSON.parse(response.body)).toEqual(success)+ expect(authorizeLimiterAdmissions()).toBe(admissionsBefore + 1)+ expect(parse.mock.calls.map(([text]) => text)).toContain(bodyOf(p))+ expect(authorizer.authorizeLlmProviderAttempt).toHaveBeenCalledExactlyOnceWith(+ expect.objectContaining({ workflowControlScopes: [p.scope] }),+ JSON.parse(bodyOf(p)),+ expect.any(Object)+ )+ }++ for (const alias of RAW_AUTHORIZE_ALIASES) {+ it(`${alias.name}: no parser reads the body and the authorize route is not reached`, async () => {+ // Valid JSON unique to this alias: any parser that read it would hand+ // exactly this text to JSON.parse.+ const aliasBody = JSON.stringify({ probe: 'raw-authorize-alias', path: alias.path })+ await withApp('', async url => {+ const parse = vi.spyOn(JSON, 'parse')+ try {+ const response = await postRaw(url, alias.path, headers(p), aliasBody)+ expect(response.status).toBe(alias.status)+ expect(parse.mock.calls.map(([text]) => text)).not.toContain(aliasBody)+ expect(authorizeLimiterAdmissions()).toBe(0)+ expect(authorizer.authorizeLlmProviderAttempt).not.toHaveBeenCalled()+ await expectCanonicalControl(url, parse)+ } finally {+ parse.mockRestore()+ }+ })+ })+ }++ it('canonical path with a query string: exempt from the global parser and routed to authorize', async () => {+ const path = `${CANONICAL_AUTHORIZE_PATH}?q=1`+ const unauthenticatedBody = JSON.stringify({ probe: 'authorize-with-query' })+ await withApp('', async url => {+ const parse = vi.spyOn(JSON, 'parse')+ try {+ const response = await postRaw(+ url,+ path,+ { 'content-type': 'application/json' },+ unauthenticatedBody+ )+ expect(response.status).toBe(401)+ expect(JSON.parse(response.body)).toEqual({ error: 'Unauthorized' })+ // Routed to authorize: its limiter ran, then the JWT check refused.+ expect(authorizeLimiterAdmissions()).toBe(1)+ expect(parse.mock.calls.map(([text]) => text)).not.toContain(unauthenticatedBody)+ expect(authorizer.authorizeLlmProviderAttempt).not.toHaveBeenCalled()+ await expectCanonicalControl(url, parse, path)+ } finally {+ parse.mockRestore()+ }+ })+ })++ it('malformed percent escape: 400 invalid_request before any parser reads the body', async () => {+ const malformedBody = JSON.stringify({ probe: 'malformed-escape' })+ await withApp('', async url => {+ const parse = vi.spyOn(JSON, 'parse')+ try {+ const response = await postRaw(+ url,+ '/api/v1/mcp-host/llm/provider-attempts/%GGuthorize',+ headers(p),+ malformedBody+ )+ expect(response.status).toBe(400)+ expect(JSON.parse(response.body)).toEqual({ error: 'invalid_request' })+ expect(parse.mock.calls.map(([text]) => text)).not.toContain(malformedBody)+ expect(authorizeLimiterAdmissions()).toBe(0)+ expect(authorizer.authorizeLlmProviderAttempt).not.toHaveBeenCalled()+ await expectCanonicalControl(url, parse)+ } finally {+ parse.mockRestore()+ }+ })+ })+})+async function getWithJsonBody(url: string,body: Buffer,diff --git a/deploy/base/control-plane/configmaps.yaml b/deploy/base/control-plane/configmaps.yamlindex c78d0813f..39febffec 100644--- a/deploy/base/control-plane/configmaps.yaml+++ b/deploy/base/control-plane/configmaps.yaml@@ -890,7 +890,12 @@ data:}error_page 502 = @control_plane_unavailable;error_page 504 = @control_plane_connect_timeout;- proxy_pass http://control_api_upstream;+ # This exact location matches the normalized URI, but a proxy_pass+ # without a URI part forwards the raw one. Aliases such as+ # %61uthorize or ./authorize would then reach control-api with the+ # 35 MiB allowance on a path its parser exemption does not know.+ # The URI part makes nginx send the canonical path (query kept).+ proxy_pass http://control_api_upstream/api/v1/mcp-host/llm/provider-attempts/authorize;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;diff --git a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts b/tests/e2e/integration/codex-subscription-contract-freeze.test.tsindex fd26b6c1b..34f546941 100644--- a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts+++ b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts@@ -37,7 +37,11 @@ it('admits visual authorization envelopes only on the exact gateway route', () =expect(route).toContain('client_max_body_size 36700160;')expect(route).toMatch(/limit_except POST\s*\{\s*deny all;/)expect(route).toContain('proxy_set_header Authorization $http_authorization;')- expect(route).toContain('proxy_pass http://control_api_upstream;')+ // The URI part forwards the canonical path, so a normalized alias nginx+ // admitted here cannot reach control-api on a raw, unrecognised path.+ expect(route).toContain(+ 'proxy_pass http://control_api_upstream/api/v1/mcp-host/llm/provider-attempts/authorize;'+ )expect(gateway.match(/client_max_body_size/g)).toHaveLength(1)})diff --git a/tests/e2e/package-lock.json b/tests/e2e/package-lock.jsonindex da7288544..8a4555cd5 100644--- a/tests/e2e/package-lock.json+++ b/tests/e2e/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-e2e-tests",- "version": "0.1.101",+ "version": "0.1.102","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-e2e-tests",- "version": "0.1.101",+ "version": "0.1.102","devDependencies": {"@clerum/llm-provider-attempt-contract": "file:../../packages/llm-provider-attempt-contract","@types/jsonwebtoken": "^9.0.10",diff --git a/tests/e2e/package.json b/tests/e2e/package.jsonindex 0c7ad7fa9..3d991ed53 100644--- a/tests/e2e/package.json+++ b/tests/e2e/package.json@@ -1,6 +1,6 @@{"name": "clerum-e2e-tests",- "version": "0.1.101",+ "version": "0.1.102","private": true,"type": "module","scripts": {