Skip to content

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

TypeScript

test(llm-proxy): pin the raw-body member bound on every parser

Alfredo Lopezd921509731c9
diff --git a/codex-llm-proxy/test/bodyStructure.test.ts b/codex-llm-proxy/test/bodyStructure.test.ts
index 363cf41a2..45bd7e317 100644
--- a/codex-llm-proxy/test/bodyStructure.test.ts
+++ b/codex-llm-proxy/test/bodyStructure.test.ts
@@ -157,6 +157,7 @@ function measure(body: string) {
maxStructuralBytes: Number.MAX_SAFE_INTEGER,
maxContainers: Number.MAX_SAFE_INTEGER,
maxDepth: Number.MAX_SAFE_INTEGER,
+ maxMembers: Number.MAX_SAFE_INTEGER,
})
}
@@ -169,6 +170,18 @@ function bodyWithContainers(schemaVersion: SchemaVersion, containers: number, co
return body
}
+/** A body holding exactly `members` object members, the envelope included. */
+function bodyWithMembers(schemaVersion: SchemaVersion, members: number): string {
+ const draft = bodyWithFiller(schemaVersion, '{}')
+ const filler = `{${Array.from(
+ { length: members - measure(draft).members },
+ (_, i) => `"m${i}":0`
+ ).join(',')}}`
+ const body = bodyWithFiller(schemaVersion, filler)
+ expect(measure(body).members).toBe(members)
+ return body
+}
+
/** A body nesting exactly `depth` containers deep, the root included. */
function bodyWithDepth(depth: number): string {
// root and request hold the filler; filler adds depth - 2 arrays
@@ -347,6 +360,52 @@ describe('codex proxy raw-body structure bounds (A8)', () => {
expect(acquire).not.toHaveBeenCalled()
})
+ it('refuses a visual body with too many members before JSON.parse and frees the slot', async () => {
+ const acquire = vi.spyOn(visualStreamGate, 'acquire')
+ const largeParses = spyLargeParses()
+ const port = listen(createProxyApps(config({ maxBodyBytes: 4096 })))
+
+ const over = bodyWithMembers('codex-completion-request.v2', BODY_STRUCTURE_LIMITS.maxMembers + 1)
+ expect(over.length).toBeGreaterThan(4096)
+ const refused = await post(port, over)
+ expect(refused.status).toBe(413)
+ expect(await refused.json()).toEqual({ error: 'payload_too_large' })
+ expect(acquire).toHaveBeenCalledTimes(1)
+ expect(largeParses()).not.toContain(over.length)
+ await waitFor(() => visualStreamGate.snapshot().running === 0, 'the visual slot was not released')
+
+ // Witness: the same shape at the bound is parsed and reaches the ticket
+ // check through a free visual slot.
+ const atBound = bodyWithMembers('codex-completion-request.v2', BODY_STRUCTURE_LIMITS.maxMembers)
+ const admitted = await post(port, atBound)
+ expect(admitted.status).toBe(403)
+ expect(await admitted.json()).toEqual({ error: 'ticket_invalid' })
+ expect(acquire).toHaveBeenCalledTimes(2)
+ expect(largeParses()).toContain(atBound.length)
+ })
+
+ it('refuses an ordinary body with too many members before JSON.parse', async () => {
+ const acquire = vi.spyOn(visualStreamGate, 'acquire')
+ const largeParses = spyLargeParses()
+ const maxBodyBytes = LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES
+ const port = listen(createProxyApps(config({ maxBodyBytes })))
+
+ const over = bodyWithMembers('codex-completion-request.v1', BODY_STRUCTURE_LIMITS.maxMembers + 1)
+ expect(over.length).toBeLessThan(maxBodyBytes)
+ const refused = await post(port, over)
+ expect(refused.status).toBe(413)
+ expect(await refused.json()).toEqual({ error: 'payload_too_large' })
+ expect(largeParses()).not.toContain(over.length)
+
+ const atBound = bodyWithMembers('codex-completion-request.v1', BODY_STRUCTURE_LIMITS.maxMembers)
+ const admitted = await post(port, atBound)
+ expect(admitted.status).toBe(403)
+ expect(await admitted.json()).toEqual({ error: 'ticket_invalid' })
+ expect(largeParses()).toContain(atBound.length)
+ // Both bodies stayed on the ordinary parser.
+ expect(acquire).not.toHaveBeenCalled()
+ })
+
it('refuses an admin body with too many containers before JSON.parse', async () => {
const largeParses = spyLargeParses()
const port = listen(createProxyApps(config()), 'adminApp')
@@ -374,6 +433,34 @@ describe('codex proxy raw-body structure bounds (A8)', () => {
expect(largeParses()).toContain(atBound.length)
})
+ it('refuses an admin body with too many members before JSON.parse', async () => {
+ const largeParses = spyLargeParses()
+ const maxBodyBytes = LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES
+ const port = listen(createProxyApps(config({ maxBodyBytes })), 'adminApp')
+ const adminBody = (members: number): string =>
+ `{"filler":{${Array.from({ length: members - 1 }, (_, i) => `"m${i}":0`).join(',')}}}`
+ const postAdmin = (body: string): Promise<Response> =>
+ fetch(`http://127.0.0.1:${port}/internal/admin/v1/codex/models`, {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${adminPermit()}`, 'content-type': 'application/json' },
+ body,
+ })
+
+ const over = adminBody(BODY_STRUCTURE_LIMITS.maxMembers + 1)
+ const refused = await postAdmin(over)
+ expect(refused.status).toBe(413)
+ expect(await refused.json()).toEqual({ error: 'payload_too_large' })
+ expect(largeParses()).not.toContain(over.length)
+
+ // Witness: at the bound the body is parsed and reaches the admin handler,
+ // which refuses the unknown key.
+ const atBound = adminBody(BODY_STRUCTURE_LIMITS.maxMembers)
+ const admitted = await postAdmin(atBound)
+ expect(admitted.status).toBe(400)
+ expect(await admitted.json()).toEqual({ error: 'unknown_field' })
+ expect(largeParses()).toContain(atBound.length)
+ })
+
it('refuses a body denser than the structural bound before JSON.parse', async () => {
// Built before the spy: the builder parses strings of the same length.
const densest = densestAcceptedV2()
diff --git a/grok-llm-proxy/test/bodyStructure.test.ts b/grok-llm-proxy/test/bodyStructure.test.ts
index a92b5551b..351bb86c9 100644
--- a/grok-llm-proxy/test/bodyStructure.test.ts
+++ b/grok-llm-proxy/test/bodyStructure.test.ts
@@ -157,6 +157,7 @@ function measure(body: string) {
maxStructuralBytes: Number.MAX_SAFE_INTEGER,
maxContainers: Number.MAX_SAFE_INTEGER,
maxDepth: Number.MAX_SAFE_INTEGER,
+ maxMembers: Number.MAX_SAFE_INTEGER,
})
}
@@ -169,6 +170,18 @@ function bodyWithContainers(schemaVersion: SchemaVersion, containers: number, co
return body
}
+/** A body holding exactly `members` object members, the envelope included. */
+function bodyWithMembers(schemaVersion: SchemaVersion, members: number): string {
+ const draft = bodyWithFiller(schemaVersion, '{}')
+ const filler = `{${Array.from(
+ { length: members - measure(draft).members },
+ (_, i) => `"m${i}":0`
+ ).join(',')}}`
+ const body = bodyWithFiller(schemaVersion, filler)
+ expect(measure(body).members).toBe(members)
+ return body
+}
+
/** A body nesting exactly `depth` containers deep, the root included. */
function bodyWithDepth(depth: number): string {
// root and request hold the filler; filler adds depth - 2 arrays
@@ -347,6 +360,52 @@ describe('grok proxy raw-body structure bounds (A8)', () => {
expect(acquire).not.toHaveBeenCalled()
})
+ it('refuses a visual body with too many members before JSON.parse and frees the slot', async () => {
+ const acquire = vi.spyOn(visualStreamGate, 'acquire')
+ const largeParses = spyLargeParses()
+ const port = listen(createProxyApps(config({ maxBodyBytes: 4096 })))
+
+ const over = bodyWithMembers('grok-completion-request.v2', BODY_STRUCTURE_LIMITS.maxMembers + 1)
+ expect(over.length).toBeGreaterThan(4096)
+ const refused = await post(port, over)
+ expect(refused.status).toBe(413)
+ expect(await refused.json()).toEqual({ error: 'payload_too_large' })
+ expect(acquire).toHaveBeenCalledTimes(1)
+ expect(largeParses()).not.toContain(over.length)
+ await waitFor(() => visualStreamGate.snapshot().running === 0, 'the visual slot was not released')
+
+ // Witness: the same shape at the bound is parsed and reaches the ticket
+ // check through a free visual slot.
+ const atBound = bodyWithMembers('grok-completion-request.v2', BODY_STRUCTURE_LIMITS.maxMembers)
+ const admitted = await post(port, atBound)
+ expect(admitted.status).toBe(403)
+ expect(await admitted.json()).toEqual({ error: 'ticket_invalid' })
+ expect(acquire).toHaveBeenCalledTimes(2)
+ expect(largeParses()).toContain(atBound.length)
+ })
+
+ it('refuses an ordinary body with too many members before JSON.parse', async () => {
+ const acquire = vi.spyOn(visualStreamGate, 'acquire')
+ const largeParses = spyLargeParses()
+ const maxBodyBytes = LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES
+ const port = listen(createProxyApps(config({ maxBodyBytes })))
+
+ const over = bodyWithMembers('grok-completion-request.v1', BODY_STRUCTURE_LIMITS.maxMembers + 1)
+ expect(over.length).toBeLessThan(maxBodyBytes)
+ const refused = await post(port, over)
+ expect(refused.status).toBe(413)
+ expect(await refused.json()).toEqual({ error: 'payload_too_large' })
+ expect(largeParses()).not.toContain(over.length)
+
+ const atBound = bodyWithMembers('grok-completion-request.v1', BODY_STRUCTURE_LIMITS.maxMembers)
+ const admitted = await post(port, atBound)
+ expect(admitted.status).toBe(403)
+ expect(await admitted.json()).toEqual({ error: 'ticket_invalid' })
+ expect(largeParses()).toContain(atBound.length)
+ // Both bodies stayed on the ordinary parser.
+ expect(acquire).not.toHaveBeenCalled()
+ })
+
it('refuses an admin body with too many containers before JSON.parse', async () => {
const largeParses = spyLargeParses()
const port = listen(createProxyApps(config()), 'adminApp')
@@ -374,6 +433,34 @@ describe('grok proxy raw-body structure bounds (A8)', () => {
expect(largeParses()).toContain(atBound.length)
})
+ it('refuses an admin body with too many members before JSON.parse', async () => {
+ const largeParses = spyLargeParses()
+ const maxBodyBytes = LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES
+ const port = listen(createProxyApps(config({ maxBodyBytes })), 'adminApp')
+ const adminBody = (members: number): string =>
+ `{"filler":{${Array.from({ length: members - 1 }, (_, i) => `"m${i}":0`).join(',')}}}`
+ const postAdmin = (body: string): Promise<Response> =>
+ fetch(`http://127.0.0.1:${port}/internal/admin/v1/grok/models`, {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${adminPermit()}`, 'content-type': 'application/json' },
+ body,
+ })
+
+ const over = adminBody(BODY_STRUCTURE_LIMITS.maxMembers + 1)
+ const refused = await postAdmin(over)
+ expect(refused.status).toBe(413)
+ expect(await refused.json()).toEqual({ error: 'payload_too_large' })
+ expect(largeParses()).not.toContain(over.length)
+
+ // Witness: at the bound the body is parsed and reaches the admin handler,
+ // which refuses the unknown key.
+ const atBound = adminBody(BODY_STRUCTURE_LIMITS.maxMembers)
+ const admitted = await postAdmin(atBound)
+ expect(admitted.status).toBe(400)
+ expect(await admitted.json()).toEqual({ error: 'unknown_field' })
+ expect(largeParses()).toContain(atBound.length)
+ })
+
it('refuses a body denser than the structural bound before JSON.parse', async () => {
// Built before the spy: the builder parses strings of the same length.
const densest = densestAcceptedV2()
diff --git a/grok-llm-proxy/test/contractFreeze.test.ts b/grok-llm-proxy/test/contractFreeze.test.ts
index 8ef268cf9..3edebb5e4 100644
--- a/grok-llm-proxy/test/contractFreeze.test.ts
+++ b/grok-llm-proxy/test/contractFreeze.test.ts
@@ -69,6 +69,7 @@ const RUNTIME_ONLY_LIMIT_KEYS = [
'maxNestingDepth',
'maxVisualRequestBodyBytes',
'maxRequestContainers',
+ 'maxRequestMembers',
] as const
type Fixture = {
diff --git a/packages/grok-provider-attempt-contract/index.d.ts b/packages/grok-provider-attempt-contract/index.d.ts
index 99261090f..251b02bd9 100644
--- a/packages/grok-provider-attempt-contract/index.d.ts
+++ b/packages/grok-provider-attempt-contract/index.d.ts
@@ -80,7 +80,7 @@ export interface BodyStructureError extends Error {
export declare function scanJsonStructure(
buf: Uint8Array,
limits: BodyStructureLimits
-): { structuralBytes: number; containers: number; deepest: number }
+): { structuralBytes: number; containers: number; deepest: number; members: number }
/**
* A body-parser `verify` hook running scanJsonStructure. The error it throws
diff --git a/packages/llm-provider-attempt-contract/index.d.ts b/packages/llm-provider-attempt-contract/index.d.ts
index 0e8be3c92..83d846c9f 100644
--- a/packages/llm-provider-attempt-contract/index.d.ts
+++ b/packages/llm-provider-attempt-contract/index.d.ts
@@ -88,7 +88,7 @@ export interface BodyStructureError extends Error {
export declare function scanJsonStructure(
buf: Uint8Array,
limits: BodyStructureLimits
-): { structuralBytes: number; containers: number; deepest: number }
+): { structuralBytes: number; containers: number; deepest: number; members: number }
/**
* A body-parser `verify` hook running scanJsonStructure. The error it throws
diff --git a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
index fb3e2ba7e..45fb74dbb 100644
--- a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
+++ b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
@@ -87,7 +87,12 @@ const PUBLISHED_RUNTIME_LIMIT_KEYS = [
// Publishing it takes four edits: the fixture's `limits`, REQUIRED_LIMIT_KEYS,
// PUBLISHED_RUNTIME_LIMIT_KEYS and the architecture doc table. Keeping it
// runtime-only takes one: add it here.
-const RUNTIME_ONLY_LIMIT_KEYS = ['maxIdLength', 'maxNestingDepth', 'maxRequestContainers'] as const
+const RUNTIME_ONLY_LIMIT_KEYS = [
+ 'maxIdLength',
+ 'maxNestingDepth',
+ 'maxRequestContainers',
+ 'maxRequestMembers',
+] as const
const SENSITIVE_VALUE_PATTERN =
/^(?:sk-[A-Za-z0-9]+|Bearer\s+\S+|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|(?!https?:\/\/)[^;\s]+=[^;\s]+(?:;|$))/
diff --git a/tests/e2e/package-lock.json b/tests/e2e/package-lock.json
index 5111bfe43..5a53b1c45 100644
--- a/tests/e2e/package-lock.json
+++ b/tests/e2e/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.98",
+ "version": "0.1.99",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-e2e-tests",
- "version": "0.1.98",
+ "version": "0.1.99",
"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.json
index c27e783d6..8cdb46303 100644
--- a/tests/e2e/package.json
+++ b/tests/e2e/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.98",
+ "version": "0.1.99",
"private": true,
"type": "module",
"scripts": {