Skip to content

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

TypeScript

fix(test): harden gfs gateway contract extraction

Josuéfa51e28772c3
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 45c93ac23..17b2019f2 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.471",
+ "version": "0.1.472",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.471",
+ "version": "0.1.472",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/image-policy": "file:../packages/image-policy",
diff --git a/control-api/package.json b/control-api/package.json
index d4f9cf5ff..4a5a57da1 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.471",
+ "version": "0.1.472",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/test/network.gateway.intent.test.ts b/control-api/test/network.gateway.intent.test.ts
index f2b4b85fd..264501062 100644
--- a/control-api/test/network.gateway.intent.test.ts
+++ b/control-api/test/network.gateway.intent.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest'
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
+import ts from 'typescript'
/** Paths are relative to this file: control-api/test/ → repo root is ../.. */
const BASE = '../../deploy/base'
@@ -83,6 +84,137 @@ function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
+function parseTypeScript(source: string, label: string): ts.SourceFile {
+ const file = ts.createSourceFile(label, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
+ const diagnostics = (file as ts.SourceFile & { parseDiagnostics: readonly ts.Diagnostic[] })
+ .parseDiagnostics
+ if (diagnostics.length > 0) throw new Error(`${label} contains malformed TypeScript`)
+ return file
+}
+
+function evaluateStaticByteExpression(
+ expression: ts.Expression,
+ sourceFile: ts.SourceFile,
+ label: string
+): number {
+ if (ts.isNumericLiteral(expression)) {
+ const literal = expression.getText(sourceFile)
+ if (!/^(?:0|[1-9][0-9]*(?:_[0-9]+)*)$/.test(literal))
+ throw new Error(`${label} must use a decimal integer literal`)
+ const value = Number(literal.replaceAll('_', ''))
+ if (!Number.isSafeInteger(value)) throw new Error(`${label} must be a safe integer`)
+ return value
+ }
+ if (
+ ts.isBinaryExpression(expression) &&
+ expression.operatorToken.kind === ts.SyntaxKind.AsteriskToken
+ ) {
+ const left = evaluateStaticByteExpression(expression.left, sourceFile, label)
+ const right = evaluateStaticByteExpression(expression.right, sourceFile, label)
+ const value = left * right
+ if (!Number.isSafeInteger(value)) throw new Error(`${label} must be a safe integer`)
+ return value
+ }
+ throw new Error(`${label} must use only decimal integers and multiplication`)
+}
+
+function staticBytesFromSource(source: string, symbol: string, label = symbol): number {
+ const sourceFile = parseTypeScript(source, label)
+ const matches: ts.VariableDeclaration[] = []
+ const visit = (node: ts.Node): void => {
+ if (
+ ts.isVariableDeclaration(node) &&
+ ts.isIdentifier(node.name) &&
+ node.name.text === symbol &&
+ ts.isVariableDeclarationList(node.parent) &&
+ (node.parent.flags & ts.NodeFlags.Const) !== 0
+ ) {
+ matches.push(node)
+ }
+ ts.forEachChild(node, visit)
+ }
+ visit(sourceFile)
+ if (matches.length !== 1 || !matches[0].initializer)
+ throw new Error(`${symbol} must have exactly one active const initializer in ${label}`)
+ return evaluateStaticByteExpression(matches[0].initializer, sourceFile, `${label}:${symbol}`)
+}
+
+function staticBytes(relPath: string, symbol: string): number {
+ return staticBytesFromSource(read(relPath), symbol, relPath)
+}
+
+function boundedEnvBytesFromSource(
+ source: string,
+ property: string,
+ envName: string,
+ label = envName
+): { fallback: number; ceiling: number } {
+ const sourceFile = parseTypeScript(source, label)
+ const matches: ts.CallExpression[] = []
+ const visit = (node: ts.Node): void => {
+ if (ts.isPropertyAssignment(node)) {
+ const propertyName =
+ ts.isIdentifier(node.name) || ts.isStringLiteral(node.name) ? node.name.text : null
+ if (
+ propertyName === property &&
+ ts.isCallExpression(node.initializer) &&
+ ts.isIdentifier(node.initializer.expression) &&
+ node.initializer.expression.text === 'boundedIntegerFromEnv' &&
+ node.initializer.arguments.length === 3 &&
+ ts.isStringLiteral(node.initializer.arguments[0]) &&
+ node.initializer.arguments[0].text === envName
+ ) {
+ matches.push(node.initializer)
+ }
+ }
+ ts.forEachChild(node, visit)
+ }
+ visit(sourceFile)
+ if (matches.length !== 1)
+ throw new Error(`${envName} must have exactly one active bounded config in ${label}`)
+ const [, fallback, ceiling] = matches[0].arguments
+ return {
+ fallback: evaluateStaticByteExpression(fallback, sourceFile, `${label}:${envName}:fallback`),
+ ceiling: evaluateStaticByteExpression(ceiling, sourceFile, `${label}:${envName}:ceiling`),
+ }
+}
+
+function boundedEnvBytes(
+ relPath: string,
+ property: string,
+ envName: string
+): { fallback: number; ceiling: number } {
+ return boundedEnvBytesFromSource(read(relPath), property, envName, relPath)
+}
+
+function assertUploadV2TransportBounds(input: {
+ writerProtocol: number
+ writerPart: number
+ protocolMirrors: number[]
+ partMirrors: number[]
+ relayBounds: Array<{ fallback: number; ceiling: number }>
+ gatewayBytes: number
+}): void {
+ if (input.writerProtocol !== 1024 * 1024 * 1024)
+ throw new Error('writer protocol maximum must remain 1 GiB')
+ if (input.writerPart !== 16 * 1024 * 1024)
+ throw new Error('writer part maximum must remain 16 MiB')
+ if (input.writerPart >= input.writerProtocol)
+ throw new Error('writer part maximum must be below the protocol maximum')
+ if (input.protocolMirrors.some(value => value !== input.writerProtocol))
+ throw new Error('client protocol maximum diverges from the writer')
+ if (input.partMirrors.some(value => value !== input.writerPart))
+ throw new Error('client part maximum diverges from the writer')
+ if (
+ input.relayBounds.some(
+ bounds => bounds.fallback !== input.writerPart || bounds.ceiling !== input.writerPart
+ )
+ )
+ throw new Error('relay part maximum diverges from the writer')
+ if (input.writerPart >= input.gatewayBytes)
+ throw new Error('gateway request cap cannot safely carry an Upload v2 part')
+}
+
function expectCanonicalPublicEgressExceptions(): void {
const exceptions = read(`${BASE}/public-egress-exceptions.yaml`)
for (const cidr of SPECIAL_USE_PUBLIC_EGRESS_EXCEPTS) {
@@ -246,6 +378,75 @@ function routeIsAllowlisted(route: RouteRef, locations: NginxLocation[]): boolea
// invariants in deploy/base/{rpc-proxy,control-plane,mcp-host}/ keep the gateway
// topology locked down (rpc-proxy egress only via control-api-rpc-gateway, HCC
// ingress only via host-context-controller-api-gateway, and nginx allowlist routes).
+describe('GFS static byte contract extraction', () => {
+ const SYMBOL = 'GFS_UPLOAD_V2_MAX_PART_BYTES'
+
+ it.each([
+ ['exported multiplication', `export const ${SYMBOL} = 16 * 1024 * 1024`],
+ ['non-exported multiplication', `const ${SYMBOL} = 16 * 1024 * 1024`],
+ ['numeric separators', `export const ${SYMBOL} = 16 * 1_024 * 1_024`],
+ ])('accepts %s', (_label, source) => {
+ expect(staticBytesFromSource(source, SYMBOL)).toBe(16 * 1024 * 1024)
+ })
+
+ it.each([
+ ['addition', `export const ${SYMBOL} = 16 * 1024 * 1024 + 1`],
+ ['division', `export const ${SYMBOL} = 16 * 1024 * 1024 / 2`],
+ ['identifier', `export const ${SYMBOL} = 16 * 1024 * SOME_IDENTIFIER`],
+ ['malformed trailing expression', `export const ${SYMBOL} = 16 * 1024 * 1024 nonsense`],
+ ['line-comment only', `// export const ${SYMBOL} = 16 * 1024 * 1024`],
+ ['block-comment only', `/* export const ${SYMBOL} = 16 * 1024 * 1024 */`],
+ ])('rejects %s', (_label, source) => {
+ expect(() => staticBytesFromSource(source, SYMBOL)).toThrow()
+ })
+
+ it('ignores a commented safe declaration before a live unsafe declaration', () => {
+ const source = [
+ `// export const ${SYMBOL} = 16 * 1024 * 1024`,
+ `export const ${SYMBOL} = 17 * 1024 * 1024`,
+ ].join('\n')
+ expect(staticBytesFromSource(source, SYMBOL)).toBe(17 * 1024 * 1024)
+ })
+
+ it('rejects a commented relay bound without a live producer', () => {
+ const source = [
+ '// gfsUploadMaxPartBytes: boundedIntegerFromEnv(',
+ "// 'CONTROL_API_GFS_UPLOAD_MAX_PART_BYTES',",
+ '// 16 * 1024 * 1024,',
+ '// 16 * 1024 * 1024',
+ '// ),',
+ ].join('\n')
+ expect(() =>
+ boundedEnvBytesFromSource(
+ source,
+ 'gfsUploadMaxPartBytes',
+ 'CONTROL_API_GFS_UPLOAD_MAX_PART_BYTES'
+ )
+ ).toThrow()
+ })
+
+ it('turns red when the effective writer part maximum exceeds a relay ceiling', () => {
+ expect(() =>
+ assertUploadV2TransportBounds(
+ (() => {
+ const unsafeWriterPart = staticBytesFromSource(
+ `export const ${SYMBOL} = 17 * 1024 * 1024`,
+ SYMBOL
+ )
+ return {
+ writerProtocol: 1024 * 1024 * 1024,
+ writerPart: unsafeWriterPart,
+ protocolMirrors: [1024 * 1024 * 1024],
+ partMirrors: [unsafeWriterPart],
+ relayBounds: [{ fallback: 16 * 1024 * 1024, ceiling: 16 * 1024 * 1024 }],
+ gatewayBytes: 24 * 1024 * 1024,
+ }
+ })()
+ )
+ ).toThrow(/writer part maximum|relay part maximum/)
+ })
+})
+
describe('network/gateway intent (manifest-level)', () => {
it('ships a GFS namespace default-deny policy while HCC owns gfsc allowlists', () => {
const gfsKustomization = read(`${BASE}/gfs/kustomization.yaml`)
@@ -347,40 +548,6 @@ describe('network/gateway intent (manifest-level)', () => {
// Upload v2 is indexed binary streaming: product policy is writer-advertised
// session metadata, while protocol and per-request safety remain compiled.
// Do not compare the runtime product policy with a single-request body cap.
- const evaluateStaticBytes = (expression: string, label: string): number => {
- const value = expression
- .split('*')
- .reduce((acc, factor) => acc * Number(factor.trim().replaceAll('_', '')), 1)
- expect(Number.isSafeInteger(value), `${label} must be a safe integer`).toBe(true)
- return value
- }
- const staticBytes = (relPath: string, symbol: string): number => {
- const source = read(relPath)
- const match = source.match(
- new RegExp(`(?:export\\s+)?const\\s+${symbol}\\s*=\\s*([0-9_*\\s]+)`)
- )
- expect(match, `${symbol} not found in ${relPath}`).not.toBeNull()
- return evaluateStaticBytes(match![1], `${relPath}:${symbol}`)
- }
- const boundedEnvBytes = (
- relPath: string,
- property: string,
- envName: string
- ): { fallback: number; ceiling: number } => {
- const source = read(relPath)
- const match = source.match(
- new RegExp(
- `${property}\\s*:\\s*boundedIntegerFromEnv\\(\\s*['"]${envName}['"]\\s*,` +
- `\\s*([0-9_*\\s]+?),\\s*([0-9_*\\s]+?)\\s*\\)`
- )
- )
- expect(match, `${envName} bounded config not found in ${relPath}`).not.toBeNull()
- return {
- fallback: evaluateStaticBytes(match![1], `${relPath}:${envName}:fallback`),
- ceiling: evaluateStaticBytes(match![2], `${relPath}:${envName}:ceiling`),
- }
- }
-
// The legacy JSON/base64 path still requires the HCC-owned gfsc body cap to
// agree with the profile funnel. It is not the Upload v2 product authority.
const hccDeployment = read(`${BASE}/control-plane/host-context-controller.yaml`)
@@ -411,10 +578,6 @@ describe('network/gateway intent (manifest-level)', () => {
'../../gfs-controller/src/upload/protocol.ts',
'GFS_UPLOAD_V2_MAX_PART_BYTES'
)
- expect(writerProtocol).toBe(1024 * 1024 * 1024)
- expect(writerPart).toBe(16 * 1024 * 1024)
- expect(writerPart).toBeLessThan(writerProtocol)
-
const protocolMirrors = [
staticBytes(
'../../control-ui/app/constants/gfsFileUpload.ts',
@@ -441,9 +604,6 @@ describe('network/gateway intent (manifest-level)', () => {
'GFS_UPLOAD_MAX_PART_BYTES'
),
]
- expect(protocolMirrors).toEqual(protocolMirrors.map(() => writerProtocol))
- expect(partMirrors).toEqual(partMirrors.map(() => writerPart))
-
// Every relay defaults to the writer part maximum and refuses configuration
// above it. A part must also fit through the profile gateway with headroom.
const controlApiRelay = boundedEnvBytes(
@@ -456,9 +616,14 @@ describe('network/gateway intent (manifest-level)', () => {
'gfsUploadMaxPartBytes',
'EXTERNAL_REST_API_GFS_UPLOAD_MAX_PART_BYTES'
)
- expect(controlApiRelay).toEqual({ fallback: writerPart, ceiling: writerPart })
- expect(externalRestRelay).toEqual({ fallback: writerPart, ceiling: writerPart })
- expect(writerPart).toBeLessThan(profileFunnelCap)
+ assertUploadV2TransportBounds({
+ writerProtocol,
+ writerPart,
+ protocolMirrors,
+ partMirrors,
+ relayBounds: [controlApiRelay, externalRestRelay],
+ gatewayBytes: profileFunnelCap,
+ })
})
it('keeps control-api memory at >=768Mi in base AND the minikube overlay (RC3 OOM guard)', () => {