Skip to content

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

TypeScript

feat(control-api): authorize Grok V2 visual requests with 413 size refusals

Alfredo Lopezf857aeda4ff2
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index f535df78c..404ca1cdf 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.634",
+ "version": "0.1.635",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.634",
+ "version": "0.1.635",
"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 a6d907f97..3199e881b 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.634",
+ "version": "0.1.635",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/services/llmProviderAttemptAuthorizer.ts b/control-api/src/services/llmProviderAttemptAuthorizer.ts
index db4239a6c..22767cf7a 100644
--- a/control-api/src/services/llmProviderAttemptAuthorizer.ts
+++ b/control-api/src/services/llmProviderAttemptAuthorizer.ts
@@ -1,8 +1,11 @@
import {
LIMITS as GROK_LIMITS,
+ buildGrokProxyEnvelope,
computeGrokPolicyHash,
- hashGrokCompletionRequestV1,
- parseGrokCompletionRequestV1,
+ requestBodyLimitBytes as grokRequestBodyLimitBytes,
+ hashGrokCompletionRequest,
+ measureNonImageAuthorizeBytes as measureGrokNonImageAuthorizeBytes,
+ parseGrokCompletionRequest,
} from '@clerum/grok-provider-attempt-contract'
import {
ENVELOPE_ALLOWANCE_BYTES,
@@ -276,11 +279,27 @@ async function authorizeGrokProviderAttempt(
}
assertBodyNestingWithinLimit(body)
const serialized = JSON.stringify(body)
+ // Same budgets as the Codex path (#784): the larger of the non-image cap plus
+ // the envelope allowance and the V2 visual envelope bounds the whole body;
+ // the wrapper outside image data stays on the non-image cap.
+ const wholeBodyLimit = Math.max(
+ grokRequestBodyLimitBytes(body.request),
+ GROK_LIMITS.maxRequestBodyBytes + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES
+ )
+ if (Buffer.byteLength(serialized, 'utf8') > wholeBodyLimit) {
+ throw new LlmProviderAttemptAuthorizeError(
+ 'payload_too_large',
+ 'request body exceeds the limit'
+ )
+ }
if (
- Buffer.byteLength(serialized, 'utf8') >
+ measureGrokNonImageAuthorizeBytes(body) >
GROK_LIMITS.maxRequestBodyBytes + AUTHORIZE_ENVELOPE_ALLOWANCE_BYTES
) {
- throw new LlmProviderAttemptAuthorizeError('invalid_request', 'request body exceeds the limit')
+ throw new LlmProviderAttemptAuthorizeError(
+ 'payload_too_large',
+ 'authorize wrapper exceeds the non-image limit'
+ )
}
const unknown = firstUnknownKey(body)
if (unknown) {
@@ -288,9 +307,13 @@ async function authorizeGrokProviderAttempt(
}
const caller = resolveCaller(claims)
assertClaimBinding(body, claims)
- const parsed = parseGrokCompletionRequestV1(body.request)
+ const parsed = parseGrokCompletionRequest(body.request)
if (!parsed.ok) {
- throw new LlmProviderAttemptAuthorizeError('invalid_request', parsed.message)
+ // `kind: 'size'` is 413: byte ceilings. Range, count and depth stay 400.
+ throw new LlmProviderAttemptAuthorizeError(
+ parsed.code === 'limit' && parsed.kind === 'size' ? 'payload_too_large' : 'invalid_request',
+ parsed.message
+ )
}
const request = parsed.value
const invocationId = typeof body.invocationId === 'string' ? body.invocationId.trim() : ''
@@ -322,7 +345,7 @@ async function authorizeGrokProviderAttempt(
'policyRevision and policyHash are required'
)
}
- const requestHash = hashGrokCompletionRequestV1(request)
+ const requestHash = hashGrokCompletionRequest(request)
if (typeof body.requestHash === 'string' && body.requestHash !== requestHash) {
throw new LlmProviderAttemptAuthorizeError(
'invalid_request',
@@ -591,6 +614,23 @@ async function authorizeGrokProviderAttempt(
connectionRevision: connection.credentialRevision,
connectionId: connection.id,
})
+ if (request.schemaVersion === 'grok-completion-request.v2') {
+ // Measure the exact proxy envelope while the attempt, ticket and any
+ // new reservation are still transactional, as the Codex path does.
+ const envelope = buildGrokProxyEnvelope({
+ executionTicket: issued.executionTicket,
+ requestHash,
+ request,
+ })
+ if (!envelope.ok) {
+ throw new LlmProviderAttemptAuthorizeError(
+ envelope.code === 'limit' && envelope.kind === 'size'
+ ? 'payload_too_large'
+ : 'invalid_request',
+ envelope.message
+ )
+ }
+ }
log.info(
{
event: 'grok_attempt_authorized',
diff --git a/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts b/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
index a3b980264..fe7b812ba 100644
--- a/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
+++ b/control-api/test/services.llmProviderAttemptAuthorizer.grok.test.ts
@@ -478,10 +478,13 @@ describe('authorizeLlmProviderAttempt grok-subscription', () => {
message: 'request body exceeds the limit',
})
expect(current.insertAttempt).not.toHaveBeenCalled()
- // Witness: one byte less is authorized.
+ // Witness: one byte less passes the size check and is refused later, by
+ // the caller binding the filler breaks.
const fits = body({ request, recipeName: 'r'.repeat(limit - withoutFiller) })
- await authorizeLlmProviderAttempt(claims(), fits, current)
- expect(current.insertAttempt).toHaveBeenCalledTimes(1)
+ expect(Buffer.byteLength(JSON.stringify(fits), 'utf8')).toBe(limit)
+ await expect(authorizeLlmProviderAttempt(claims(), fits, current)).rejects.toMatchObject({
+ message: 'body recipeName does not match the token caller',
+ })
})
})
@@ -514,7 +517,7 @@ describe('authorizeLlmProviderAttempt grok-subscription', () => {
authorizeLlmProviderAttempt(claims(), body({ request }), current)
).rejects.toMatchObject({
code: 'payload_too_large',
- message: `image exceeds ${GROK_VISUAL_LIMITS.maxImageBytes} decoded bytes`,
+ message: `messages[0].contentParts[1]: image exceeds ${GROK_VISUAL_LIMITS.maxImageBytes} decoded bytes`,
})
expect(current.insertAttempt).not.toHaveBeenCalled()
// Witness: the same authorizer admits the fixture image.