Skip to content

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

TypeScript

fix(control-api): bound extraAuthorizeParams in the runtime CR reader (R1-L2)

Diego Zacariasc9dffbda0102
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 0a4fd7424..50c00993a 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.647",
+ "version": "0.1.648",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.647",
+ "version": "0.1.648",
"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 731cc8961..8b15eaf27 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.647",
+ "version": "0.1.648",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/oauth/genericKnobs.ts b/control-api/src/oauth/genericKnobs.ts
index 18710aa8f..35b8dd502 100644
--- a/control-api/src/oauth/genericKnobs.ts
+++ b/control-api/src/oauth/genericKnobs.ts
@@ -16,6 +16,16 @@ import type { GenericClientRouting } from './callback.js'
* booleans with `=== true` (absent ⇒ false). The install path writes the 10
* GENERIC-REQ knobs explicitly; the wizard is what fills defaults, never the server.
*/
+/**
+ * Bounds for `extraAuthorizeParams`, enforced at BOTH the install-side schema
+ * below AND the runtime CR reader (`extractExtraAuthorizeParams` in
+ * mcpServerOAuthSpec.ts) so a hand-written CR that bypasses admission cannot
+ * smuggle an unbounded map into the authorize URL. Shared so the two enforcement
+ * points can never drift.
+ */
+export const MAX_EXTRA_AUTHORIZE_PARAMS = 16
+export const MAX_EXTRA_AUTHORIZE_PARAM_VALUE_LEN = 1024
+
export const GenericOAuthKnobsSchema = z
.object({
authorizationEndpoint: z.string().min(1).max(2048),
@@ -30,8 +40,10 @@ export const GenericOAuthKnobsSchema = z
includeResponseType: z.boolean(),
supportsRefresh: z.boolean(),
extraAuthorizeParams: z
- .record(z.string(), z.string().max(1024))
- .refine(o => Object.keys(o).length <= 16, { message: 'at most 16 extra authorize params' })
+ .record(z.string(), z.string().max(MAX_EXTRA_AUTHORIZE_PARAM_VALUE_LEN))
+ .refine(o => Object.keys(o).length <= MAX_EXTRA_AUTHORIZE_PARAMS, {
+ message: `at most ${MAX_EXTRA_AUTHORIZE_PARAMS} extra authorize params`,
+ })
.optional(),
})
.strict()
diff --git a/control-api/src/oauth/mcpServerOAuthSpec.ts b/control-api/src/oauth/mcpServerOAuthSpec.ts
index 22da664d0..67dc697a4 100644
--- a/control-api/src/oauth/mcpServerOAuthSpec.ts
+++ b/control-api/src/oauth/mcpServerOAuthSpec.ts
@@ -17,6 +17,7 @@ import type {
RemoteClientRouting,
ServerOAuthSecretSource,
} from './callback.js'
+import { MAX_EXTRA_AUTHORIZE_PARAMS, MAX_EXTRA_AUTHORIZE_PARAM_VALUE_LEN } from './genericKnobs.js'
import type { OAuthGrantKey } from './store.js'
export interface McpServerOAuthDecl {
@@ -212,12 +213,20 @@ function resolveRemoteSecretSource(
return clientMode === 'confidential' ? { kind: 'dcr-store' } : { kind: 'public' }
}
-/** Read the optional `extraAuthorizeParams` map, keeping only string-valued keys. */
+/**
+ * Read the optional `extraAuthorizeParams` map, keeping only string-valued keys and
+ * enforcing the same bounds as the install-side schema (genericKnobs). A CR can be
+ * written straight to the apiserver, bypassing admission, so the runtime reader is
+ * the last gate before these land in the authorize URL: over-long values are dropped
+ * (never truncated or forwarded) and the map is capped at MAX_EXTRA_AUTHORIZE_PARAMS.
+ */
function extractExtraAuthorizeParams(raw: unknown): Record<string, string> | undefined {
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return undefined
const out: Record<string, string> = {}
for (const [k, v] of Object.entries(raw as Record<string, unknown>)) {
- if (typeof v === 'string') out[k] = v
+ if (typeof v !== 'string' || v.length > MAX_EXTRA_AUTHORIZE_PARAM_VALUE_LEN) continue
+ out[k] = v
+ if (Object.keys(out).length >= MAX_EXTRA_AUTHORIZE_PARAMS) break
}
return Object.keys(out).length > 0 ? out : undefined
}
diff --git a/control-api/test/oauth.mcpServerOAuthSpec.generic.test.ts b/control-api/test/oauth.mcpServerOAuthSpec.generic.test.ts
index 637fd744d..ad960946a 100644
--- a/control-api/test/oauth.mcpServerOAuthSpec.generic.test.ts
+++ b/control-api/test/oauth.mcpServerOAuthSpec.generic.test.ts
@@ -96,6 +96,28 @@ describe('resolveServerOAuthSubject — generic lane (DEC-28)', () => {
expect(r?.decl.generic?.extraAuthorizeParams).toEqual({ audience: 'aud-1' })
})
+ it('caps extraAuthorizeParams key count at the install-side max (defence in depth)', () => {
+ // A CR written straight to the apiserver bypasses the install-side schema, so
+ // the runtime reader must apply the same key cap (genericKnobs: 16).
+ const many: Record<string, string> = {}
+ for (let i = 0; i < 20; i++) many[`k${String(i).padStart(2, '0')}`] = `v${i}`
+ const r = resolveServerOAuthSubject(serverFrom({ ...KNOBS, extraAuthorizeParams: many }))
+ expect(Object.keys(r?.decl.generic?.extraAuthorizeParams ?? {}).length).toBe(16)
+ })
+
+ it('drops an extraAuthorizeParams value longer than the install-side max', () => {
+ const r = resolveServerOAuthSubject(
+ serverFrom({
+ ...KNOBS,
+ extraAuthorizeParams: { ok: 'short', tooLong: 'x'.repeat(1025) },
+ })
+ )
+ const params = r?.decl.generic?.extraAuthorizeParams ?? {}
+ // The over-long value is dropped, not truncated or forwarded.
+ expect(params.ok).toBe('short')
+ expect(params.tooLong).toBeUndefined()
+ })
+
it('half-declared refs (only one) fail closed to null', () => {
expect(
resolveServerOAuthSubject(serverFrom({ ...KNOBS, clientIdRef: { name: 'x', key: 'y' } }))