Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(contracts): bound request object members
Alfredo Lopeze595df676f2b
diff --git a/packages/grok-provider-attempt-contract/bodyStructure.cjs b/packages/grok-provider-attempt-contract/bodyStructure.cjsindex 136794eab..818104ce2 100644--- a/packages/grok-provider-attempt-contract/bodyStructure.cjs+++ b/packages/grok-provider-attempt-contract/bodyStructure.cjs@@ -20,6 +20,8 @@* `maxRequestContainers` plus the envelope's own containers.* - depth: container nesting, bounded by the request depth plus the one* envelope level around it.+ * - members: every `:` outside a string, i.e. one per object member, bounded+ * by the contract's `maxRequestMembers` plus the envelope's own members.** Only UTF-8 is scanned. In UTF-16 or UTF-32 a quote or backslash byte can* be half of another character, so the scan would lose track of strings; any@@ -63,18 +65,27 @@ function tooDense(limits) {)}+function tooManyMembers(maxMembers) {+ return new BodyStructureError(+ 413,+ 'body.structure.too.many.members',+ `request body exceeds ${maxMembers} object members`+ )+}+/*** Scans `buf` (a Buffer holding UTF-8 JSON) and throws a BodyStructureError* on the first bound it crosses. Returns the counts it measured. It does not* validate JSON syntax; JSON.parse still does that.*/function scanJsonStructure(buf, limits) {- const { maxStructuralBytes, maxContainers, maxDepth } = limits+ const { maxStructuralBytes, maxContainers, maxDepth, maxMembers } = limitsconst length = buf.lengthlet structuralBytes = 0let containers = 0let depth = 0let deepest = 0+ let members = 0let i = 0while (i < length) {const byte = buf[i]@@ -110,10 +121,13 @@ function scanJsonStructure(buf, limits) {if (depth > deepest) deepest = depth} else if (byte === 0x7d || byte === 0x5d) {depth--+ } else if (byte === 0x3a) {+ members+++ if (members > maxMembers) throw tooManyMembers(maxMembers)}i++}- return { structuralBytes, containers, deepest }+ return { structuralBytes, containers, deepest, members }}/**@@ -123,10 +137,17 @@ function scanJsonStructure(buf, limits) {* body attached as `err.body`, so a handler must never log the error object.*/function createBodyStructureVerify(limits) {+ for (const name of ['maxStructuralBytes', 'maxContainers', 'maxDepth', 'maxMembers']) {+ const value = limits[name]+ if (typeof value !== 'number' || !Number.isSafeInteger(value) || value <= 0) {+ throw new TypeError(`body structure limit ${name} must be a positive safe integer`)+ }+ }const bounds = Object.freeze({maxStructuralBytes: limits.maxStructuralBytes,maxContainers: limits.maxContainers,maxDepth: limits.maxDepth,+ maxMembers: limits.maxMembers,})return function verifyBodyStructure(_req, _res, buf, encoding) {if (encoding !== 'utf-8') {diff --git a/packages/grok-provider-attempt-contract/index.cjs b/packages/grok-provider-attempt-contract/index.cjsindex 4744e1c65..0eea4c28f 100644--- a/packages/grok-provider-attempt-contract/index.cjs+++ b/packages/grok-provider-attempt-contract/index.cjs@@ -24,6 +24,7 @@ const COMPLETIONS_ORIGIN = 'https://cli-chat-proxy.grok.com/v1/responses'const CATALOG_ORIGIN = 'https://cli-chat-proxy.grok.com/v1/models'const MAX_REQUEST_BODY_BYTES = 8388608+const MAX_REQUEST_MEMBERS = 524288const MIB = 1024 * 1024const LIMITS = Object.freeze({@@ -70,6 +71,13 @@ const LIMITS = Object.freeze({// conversation needs a few thousand containers; tool results are strings// and never count. Must equal the Codex contract's value.maxRequestContainers: 262144,+ // Object members in one request, counted over every object in the tree.+ // JSON.parse keeps one property slot per member, so a body of short keys+ // with tiny values fits the byte cap with millions of members and exhausts+ // the proxy heap before the request is ever authorized. A realistic+ // request is about 190k members (A9 model window), and this bound leaves+ // more than twice that headroom. Must equal the Codex contract's value.+ maxRequestMembers: MAX_REQUEST_MEMBERS,// How long an execution ticket stays redeemable after authorize. control-api// signs Grok tickets with this TTL, and the proxy bounds its admission waits// against the remaining ticket life, so both read it from here (#739).@@ -96,6 +104,11 @@ const MAX_REQUEST_DEPTH = LIMITS.maxNestingDepth + REQUEST_ENVELOPE_DEPTH// several times that.const ENVELOPE_CONTAINER_ALLOWANCE = 16+// Object members a proxy envelope or an authorize body adds around its+// request: ids, hashes, tickets and revisions. 64 is several times the+// members such a wrapper actually carries.+const ENVELOPE_MEMBER_ALLOWANCE = 64+// Bounds for the raw-body scan (bodyStructure.cjs) that grok-llm-proxy and// control-api run before JSON.parse. Each is the matching request bound plus// the envelope around the request, so the scan refuses no request this@@ -105,6 +118,7 @@ const BODY_STRUCTURE_LIMITS = Object.freeze({maxStructuralBytes: LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES,maxContainers: LIMITS.maxRequestContainers + ENVELOPE_CONTAINER_ALLOWANCE,maxDepth: MAX_REQUEST_DEPTH + 1,+ maxMembers: LIMITS.maxRequestMembers + ENVELOPE_MEMBER_ALLOWANCE,})const ID_PATTERN = /^[A-Za-z0-9._:/-]{1,128}$/@@ -367,6 +381,7 @@ function checkStructure(value, maxDepth) {const stack = [value, 1]let elements = 1let containers = 1+ let members = 0while (stack.length > 0) {const depth = stack.pop()const node = stack.pop()@@ -390,6 +405,12 @@ function checkStructure(value, maxDepth) {// for such input this bound can only refuse earlier, never later.return fail('limit', 'request exceeds maxRequestBodyBytes element bound', 'size')}+ if (!Array.isArray(node)) {+ members += Object.keys(node).length+ if (members > LIMITS.maxRequestMembers) {+ return fail('limit', 'request exceeds maxRequestMembers', 'size')+ }+ }for (const child of children) {if (child !== null && typeof child === 'object') {containers++diff --git a/packages/grok-provider-attempt-contract/index.d.ts b/packages/grok-provider-attempt-contract/index.d.tsindex c1421093c..99261090f 100644--- a/packages/grok-provider-attempt-contract/index.d.ts+++ b/packages/grok-provider-attempt-contract/index.d.ts@@ -33,6 +33,8 @@ export declare const LIMITS: {readonly maxNestingDepth: 64/** Objects and arrays in one request, the root included; equal to the Codex contract's. */readonly maxRequestContainers: 262144+ /** Object members in one request, over every object in the tree; equal to the Codex contract's. */+ readonly maxRequestMembers: 524288/** Execution ticket TTL; control-api signs Grok tickets with it. */readonly executionTicketTtlMs: 60000}@@ -52,18 +54,21 @@ export declare const BODY_STRUCTURE_LIMITS: {readonly maxStructuralBytes: 8404992readonly maxContainers: 262160readonly maxDepth: 70+ readonly maxMembers: 524352}export interface BodyStructureLimits {readonly maxStructuralBytes: numberreadonly maxContainers: numberreadonly maxDepth: number+ readonly maxMembers: number}/*** Thrown by the scan. `type` is `body.structure.too.dense` (413),- * `body.structure.too.many.containers` (413), `body.structure.too.deep` (400)- * or, from the verify hook, `charset.unsupported` (415).+ * `body.structure.too.many.containers` (413), `body.structure.too.deep` (400),+ * `body.structure.too.many.members` (413) or, from the verify hook,+ * `charset.unsupported` (415).*/export interface BodyStructureError extends Error {readonly name: 'BodyStructureError'diff --git a/packages/llm-provider-attempt-contract/bodyStructure.cjs b/packages/llm-provider-attempt-contract/bodyStructure.cjsindex 136794eab..818104ce2 100644--- a/packages/llm-provider-attempt-contract/bodyStructure.cjs+++ b/packages/llm-provider-attempt-contract/bodyStructure.cjs@@ -20,6 +20,8 @@* `maxRequestContainers` plus the envelope's own containers.* - depth: container nesting, bounded by the request depth plus the one* envelope level around it.+ * - members: every `:` outside a string, i.e. one per object member, bounded+ * by the contract's `maxRequestMembers` plus the envelope's own members.** Only UTF-8 is scanned. In UTF-16 or UTF-32 a quote or backslash byte can* be half of another character, so the scan would lose track of strings; any@@ -63,18 +65,27 @@ function tooDense(limits) {)}+function tooManyMembers(maxMembers) {+ return new BodyStructureError(+ 413,+ 'body.structure.too.many.members',+ `request body exceeds ${maxMembers} object members`+ )+}+/*** Scans `buf` (a Buffer holding UTF-8 JSON) and throws a BodyStructureError* on the first bound it crosses. Returns the counts it measured. It does not* validate JSON syntax; JSON.parse still does that.*/function scanJsonStructure(buf, limits) {- const { maxStructuralBytes, maxContainers, maxDepth } = limits+ const { maxStructuralBytes, maxContainers, maxDepth, maxMembers } = limitsconst length = buf.lengthlet structuralBytes = 0let containers = 0let depth = 0let deepest = 0+ let members = 0let i = 0while (i < length) {const byte = buf[i]@@ -110,10 +121,13 @@ function scanJsonStructure(buf, limits) {if (depth > deepest) deepest = depth} else if (byte === 0x7d || byte === 0x5d) {depth--+ } else if (byte === 0x3a) {+ members+++ if (members > maxMembers) throw tooManyMembers(maxMembers)}i++}- return { structuralBytes, containers, deepest }+ return { structuralBytes, containers, deepest, members }}/**@@ -123,10 +137,17 @@ function scanJsonStructure(buf, limits) {* body attached as `err.body`, so a handler must never log the error object.*/function createBodyStructureVerify(limits) {+ for (const name of ['maxStructuralBytes', 'maxContainers', 'maxDepth', 'maxMembers']) {+ const value = limits[name]+ if (typeof value !== 'number' || !Number.isSafeInteger(value) || value <= 0) {+ throw new TypeError(`body structure limit ${name} must be a positive safe integer`)+ }+ }const bounds = Object.freeze({maxStructuralBytes: limits.maxStructuralBytes,maxContainers: limits.maxContainers,maxDepth: limits.maxDepth,+ maxMembers: limits.maxMembers,})return function verifyBodyStructure(_req, _res, buf, encoding) {if (encoding !== 'utf-8') {diff --git a/packages/llm-provider-attempt-contract/index.cjs b/packages/llm-provider-attempt-contract/index.cjsindex 1e702539a..1bb2cdd3e 100644--- a/packages/llm-provider-attempt-contract/index.cjs+++ b/packages/llm-provider-attempt-contract/index.cjs@@ -57,6 +57,13 @@ const LIMITS = Object.freeze({// codex-llm-proxy/src/requestLimits.ts. Must equal the Grok contract's// value.maxRequestContainers: 262144,+ // Object members in one request, counted over every object in the tree.+ // JSON.parse keeps one property slot per member, so a body of short keys+ // with tiny values fits the byte cap with millions of members and exhausts+ // the proxy heap before the request is ever authorized. A realistic+ // request is about 190k members (A9 model window), and this bound leaves+ // more than twice that headroom. Must equal the Grok contract's value.+ maxRequestMembers: 524288,// How long an execution ticket stays redeemable after authorize. control-api// signs tickets with this TTL, and the proxy bounds its admission waits// against the remaining ticket life, so both read it from here (#739).@@ -84,6 +91,11 @@ const MAX_REQUEST_DEPTH = LIMITS.maxNestingDepth + REQUEST_ENVELOPE_DEPTH// several times that.const ENVELOPE_CONTAINER_ALLOWANCE = 16+// Object members a proxy envelope or an authorize body adds around its+// request: ids, hashes, tickets and revisions. 64 is several times the+// members such a wrapper actually carries.+const ENVELOPE_MEMBER_ALLOWANCE = 64+// Bounds for the raw-body scan (bodyStructure.cjs) that codex-llm-proxy and// control-api run before JSON.parse. Each is the matching request bound plus// the envelope around the request, so the scan refuses no request this@@ -93,6 +105,7 @@ const BODY_STRUCTURE_LIMITS = Object.freeze({maxStructuralBytes: LIMITS.maxRequestBodyBytes + ENVELOPE_ALLOWANCE_BYTES,maxContainers: LIMITS.maxRequestContainers + ENVELOPE_CONTAINER_ALLOWANCE,maxDepth: MAX_REQUEST_DEPTH + 1,+ maxMembers: LIMITS.maxRequestMembers + ENVELOPE_MEMBER_ALLOWANCE,})const ID_PATTERN = /^[A-Za-z0-9._:/-]{1,128}$/@@ -347,6 +360,7 @@ function checkStructure(value, maxDepth) {const stack = [value, 1]let elements = 1let containers = 1+ let members = 0while (stack.length > 0) {const depth = stack.pop()const node = stack.pop()@@ -372,6 +386,12 @@ function checkStructure(value, maxDepth) {// for such input this bound can only refuse earlier, never later.return fail('limit', 'request exceeds maxRequestBodyBytes element bound', 'size')}+ if (!Array.isArray(node)) {+ members += Object.keys(node).length+ if (members > LIMITS.maxRequestMembers) {+ return fail('limit', 'request exceeds maxRequestMembers', 'size')+ }+ }for (const child of children) {if (child !== null && typeof child === 'object') {containers++diff --git a/packages/llm-provider-attempt-contract/index.d.ts b/packages/llm-provider-attempt-contract/index.d.tsindex a5d4724eb..0e8be3c92 100644--- a/packages/llm-provider-attempt-contract/index.d.ts+++ b/packages/llm-provider-attempt-contract/index.d.ts@@ -40,6 +40,8 @@ export declare const LIMITS: {readonly maxNestingDepth: 64/** Objects and arrays in one request, the root included; equal to the Grok contract's. */readonly maxRequestContainers: 262144+ /** Object members in one request, over every object in the tree; equal to the Grok contract's. */+ readonly maxRequestMembers: 524288/** Execution ticket TTL; control-api signs tickets with it. */readonly executionTicketTtlMs: 60000}@@ -60,18 +62,21 @@ export declare const BODY_STRUCTURE_LIMITS: {readonly maxStructuralBytes: 8404992readonly maxContainers: 262160readonly maxDepth: 70+ readonly maxMembers: 524352}export interface BodyStructureLimits {readonly maxStructuralBytes: numberreadonly maxContainers: numberreadonly maxDepth: number+ readonly maxMembers: number}/*** Thrown by the scan. `type` is `body.structure.too.dense` (413),- * `body.structure.too.many.containers` (413), `body.structure.too.deep` (400)- * or, from the verify hook, `charset.unsupported` (415).+ * `body.structure.too.many.containers` (413), `body.structure.too.deep` (400),+ * `body.structure.too.many.members` (413) or, from the verify hook,+ * `charset.unsupported` (415).*/export interface BodyStructureError extends Error {readonly name: 'BodyStructureError'