Skip to content

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

TypeScript

test: require an envelope allowance on the LLM authorize route (#731)

Alfredo Lopez866bac7bcd1e
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index cc624b3db..f64de5231 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.599",
+ "version": "0.1.600",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.599",
+ "version": "0.1.600",
"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.json
index 944361935..20033d94b 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.599",
+ "version": "0.1.600",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/deploy.llmAuthorizeBodyLimit.test.ts b/control-api/test/deploy.llmAuthorizeBodyLimit.test.ts
new file mode 100644
index 000000000..8151ee512
--- /dev/null
+++ b/control-api/test/deploy.llmAuthorizeBodyLimit.test.ts
@@ -0,0 +1,37 @@
+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 {'
+
+/** 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 sets the gateway body limit to the request cap plus the envelope allowance', () => {
+ 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.
+ expect(block).toContain('proxy_pass http://control_api_upstream;')
+ 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()
+ // Without the directive nginx applies its 1m default and refuses a
+ // contract-valid request before control-api sees it.
+ const cap = Math.max(CODEX_LIMITS.maxRequestBodyBytes, GROK_LIMITS.maxRequestBodyBytes)
+ expect(Number(directive![1])).toBe(cap + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES)
+ })
+})
diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
index 1b4688158..6dd4f28df 100644
--- a/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
+++ b/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
@@ -1,10 +1,12 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
+ LIMITS,
computeGrokPolicyHash,
hashGrokCompletionRequestV1,
} from '@clerum/grok-provider-attempt-contract'
import { config } from '../src/config.js'
import {
+ AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES,
LlmProviderAttemptAuthorizeError,
type LlmProviderAttemptAuthorizerDeps,
authorizeLlmProviderAttempt,
@@ -361,4 +363,69 @@ describe('authorizeLlmProviderAttempt grok-subscription', () => {
expect(getPluginWorkloadSdkProviderAttemptForUpdate).not.toHaveBeenCalled()
expect(current.insertAttempt).not.toHaveBeenCalled()
})
+
+ // #731 R3-3: the byte cap belongs to `request`; the authorize envelope around
+ // it gets its own allowance, as the proxies' body limit does (R2-6).
+ describe('request cap and envelope allowance (#731 R3-3)', () => {
+ /** REQUEST padded so that JSON.stringify(request) is exactly `bytes` long. */
+ function requestOfBytes(bytes: number) {
+ const base = Buffer.byteLength(JSON.stringify(REQUEST), 'utf8')
+ const content = 'x'.repeat(REQUEST.messages[0]!.content.length + bytes - base)
+ const request = { ...REQUEST, messages: [{ role: 'user' as const, content }] }
+ expect(Buffer.byteLength(JSON.stringify(request), 'utf8')).toBe(bytes)
+ return request
+ }
+
+ it('T-R3-3a-grok authorizes a request just under the cap although the whole body is over it', async () => {
+ const current = deps()
+ const request = requestOfBytes(LIMITS.maxRequestBodyBytes - 64)
+ const payload = body({ request })
+ // Fixture check: the envelope takes the whole body past the request cap.
+ expect(Buffer.byteLength(JSON.stringify(payload), 'utf8')).toBeGreaterThan(
+ LIMITS.maxRequestBodyBytes
+ )
+ const result = await authorizeLlmProviderAttempt(claims(), payload, current)
+ expect(result).toMatchObject({
+ executionTicket: 'grok-ticket.jwt',
+ requestHash: hashGrokCompletionRequestV1(request),
+ })
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ })
+
+ it('T-R3-3b-grok refuses a request one byte over the cap with the contract message', async () => {
+ const current = deps()
+ const request = requestOfBytes(LIMITS.maxRequestBodyBytes + 1)
+ await expect(
+ authorizeLlmProviderAttempt(claims(), body({ request }), current)
+ ).rejects.toMatchObject({
+ code: 'invalid_request',
+ message: 'request exceeds maxRequestBodyBytes',
+ })
+ expect(current.insertAttempt).not.toHaveBeenCalled()
+ // Witness: the same authorizer admits the request once it fits.
+ await authorizeLlmProviderAttempt(
+ claims(),
+ body({ request: requestOfBytes(LIMITS.maxRequestBodyBytes) }),
+ current
+ )
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ })
+
+ it('T-R3-3c-grok refuses a body one byte past the cap plus the envelope allowance', async () => {
+ const current = deps()
+ const request = requestOfBytes(LIMITS.maxRequestBodyBytes)
+ const withoutFiller = Buffer.byteLength(
+ JSON.stringify(body({ request, recipeName: '' })),
+ 'utf8'
+ )
+ const limit = LIMITS.maxRequestBodyBytes + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES
+ const payload = body({ request, recipeName: 'r'.repeat(limit + 1 - withoutFiller) })
+ expect(Buffer.byteLength(JSON.stringify(payload), 'utf8')).toBe(limit + 1)
+ await expect(authorizeLlmProviderAttempt(claims(), payload, current)).rejects.toMatchObject({
+ code: 'invalid_request',
+ message: 'request body exceeds the limit',
+ })
+ expect(current.insertAttempt).not.toHaveBeenCalled()
+ })
+ })
})
diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
index bf5a2f531..d6556b078 100644
--- a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
+++ b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts
@@ -1,6 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
-import { hashCodexCompletionRequestV1 } from '@clerum/llm-provider-attempt-contract'
+import { LIMITS, hashCodexCompletionRequestV1 } from '@clerum/llm-provider-attempt-contract'
import {
+ AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES,
LlmProviderAttemptAuthorizeError,
type LlmProviderAttemptAuthorizerDeps,
authorizeLlmProviderAttempt,
@@ -660,4 +661,66 @@ describe('authorizeLlmProviderAttempt', () => {
).rejects.toMatchObject({ code: 'disabled' })
expect(current.insertAttempt).not.toHaveBeenCalled()
})
+
+ // #731 R3-3: the byte cap belongs to `request`; the authorize envelope around
+ // it gets its own allowance, as the proxies' body limit does (R2-6).
+ describe('request cap and envelope allowance (#731 R3-3)', () => {
+ /** REQUEST padded so that JSON.stringify(request) is exactly `bytes` long. */
+ function requestOfBytes(bytes: number) {
+ const base = Buffer.byteLength(JSON.stringify(REQUEST), 'utf8')
+ const content = 'x'.repeat(REQUEST.messages[0]!.content.length + bytes - base)
+ const request = { ...REQUEST, messages: [{ role: 'user' as const, content }] }
+ expect(Buffer.byteLength(JSON.stringify(request), 'utf8')).toBe(bytes)
+ return request
+ }
+
+ it('T-R3-3a authorizes a request just under the cap although the whole body is over it', async () => {
+ const request = requestOfBytes(LIMITS.maxRequestBodyBytes - 64)
+ const payload = body({ request })
+ // Fixture check: the envelope takes the whole body past the request cap.
+ expect(Buffer.byteLength(JSON.stringify(payload), 'utf8')).toBeGreaterThan(
+ LIMITS.maxRequestBodyBytes
+ )
+ const result = await authorizeLlmProviderAttempt(claims(), payload, current)
+ expect(result).toMatchObject({
+ executionTicket: 'ticket.jwt',
+ requestHash: hashCodexCompletionRequestV1(request),
+ })
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ })
+
+ it('T-R3-3b refuses a request one byte over the cap with the contract message', async () => {
+ const request = requestOfBytes(LIMITS.maxRequestBodyBytes + 1)
+ await expect(
+ authorizeLlmProviderAttempt(claims(), body({ request }), current)
+ ).rejects.toMatchObject({
+ code: 'invalid_request',
+ message: 'request exceeds maxRequestBodyBytes',
+ })
+ expect(current.insertAttempt).not.toHaveBeenCalled()
+ // Witness: the same authorizer admits the request once it fits.
+ await authorizeLlmProviderAttempt(
+ claims(),
+ body({ request: requestOfBytes(LIMITS.maxRequestBodyBytes) }),
+ current
+ )
+ expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ })
+
+ it('T-R3-3c refuses a body one byte past the cap plus the envelope allowance', async () => {
+ const request = requestOfBytes(LIMITS.maxRequestBodyBytes)
+ const withoutFiller = Buffer.byteLength(
+ JSON.stringify(body({ request, recipeName: '' })),
+ 'utf8'
+ )
+ const limit = LIMITS.maxRequestBodyBytes + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES
+ const payload = body({ request, recipeName: 'r'.repeat(limit + 1 - withoutFiller) })
+ expect(Buffer.byteLength(JSON.stringify(payload), 'utf8')).toBe(limit + 1)
+ await expect(authorizeLlmProviderAttempt(claims(), payload, current)).rejects.toMatchObject({
+ code: 'invalid_request',
+ message: 'request body exceeds the limit',
+ })
+ expect(current.insertAttempt).not.toHaveBeenCalled()
+ })
+ })
})