Skip to content

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

TypeScript

Merge remote-tracking branch 'origin/dev' into feat/mcp-server-oauth

Diego Zacarias9bb00e31947b
diff --cc control-api/package-lock.json
index 17b2019f2,94317002b..0ab6b5a06
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@@ -1,12 -1,12 +1,12 @@@
{
"name": "clerum-control-api",
- "version": "0.1.472",
- "version": "0.1.470",
++ "version": "0.1.473",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.472",
- "version": "0.1.470",
++ "version": "0.1.473",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/image-policy": "file:../packages/image-policy",
diff --cc control-api/package.json
index 4a5a57da1,32f95cd2f..1470b0b85
--- a/control-api/package.json
+++ b/control-api/package.json
@@@ -1,6 -1,6 +1,6 @@@
{
"name": "clerum-control-api",
- "version": "0.1.472",
- "version": "0.1.470",
++ "version": "0.1.473",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --cc control-ui/package-lock.json
index d3c00d762,d3c00d762..186b759a2
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@@ -1,12 -1,12 +1,12 @@@
{
"name": "clerum-control-ui",
-- "version": "0.1.434",
++ "version": "0.1.435",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
-- "version": "0.1.434",
++ "version": "0.1.435",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --cc control-ui/package.json
index e66af8377,e66af8377..414f82f1d
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@@ -1,6 -1,6 +1,6 @@@
{
"name": "clerum-control-ui",
-- "version": "0.1.434",
++ "version": "0.1.435",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",
diff --cc host-context-controller/package-lock.json
index bbe7bf588,bbe7bf588..84cf93bac
--- a/host-context-controller/package-lock.json
+++ b/host-context-controller/package-lock.json
@@@ -1,12 -1,12 +1,12 @@@
{
"name": "clerum-host-context-controller",
-- "version": "0.1.228",
++ "version": "0.1.229",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-host-context-controller",
-- "version": "0.1.228",
++ "version": "0.1.229",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@clerum/image-policy": "file:../packages/image-policy",
diff --cc host-context-controller/package.json
index 96e9a797d,96e9a797d..efe9ae9da
--- a/host-context-controller/package.json
+++ b/host-context-controller/package.json
@@@ -1,6 -1,6 +1,6 @@@
{
"name": "clerum-host-context-controller",
-- "version": "0.1.228",
++ "version": "0.1.229",
"description": "Host Context Controller - provides McpServer CRDs to mcp-host via REST API",
"main": "dist/main.js",
"engines": {
diff --cc mcp-host/src/core/guardrails/hooks/hookFetcher.ts
index 000000000,0100d2132..215bf49ec
mode 000000,100644..100644
--- a/mcp-host/src/core/guardrails/hooks/hookFetcher.ts
+++ b/mcp-host/src/core/guardrails/hooks/hookFetcher.ts
@@@ -1,0 -1,190 +1,192 @@@
+ /**
+ * Concrete `/v1` HTTP transport for installed hooks (spec §8.1 wire contract).
+ *
+ * POSTs JSON to `{endpoint}{path}/v1/{point}` with a short-lived bearer token
+ * (mcp-host's env-injected token via a `getAuthToken` closure — the same pattern
+ * as `credentialBrokerClient.ts`; no in-process signing). Enforces the timeout
+ * and body cap, and classifies the outcome per §8.1: `5xx` / timeout /
+ * connection error / malformed or oversized body → `unavailable` (→ the hook's
+ * fail-mode, §8.6). `4xx`/`2xx` are returned for the per-point mapping to judge.
+ *
+ * In-cluster (`image`/`service`) targets resolve to cluster-private IPs and are
+ * dialed directly. A `remote` (external) target — `descriptor.external` — is
+ * dialed through the SSRF-guarded transport (`../../net/ssrf`: reject
+ * private/loopback/link-local/metadata ranges, DNS-pin the connection), so a
+ * hosted provider (e.g. guardrails.aporia.com) works while an internal/metadata
+ * URL is refused (spec §8.3). An SSRF refusal maps to `unavailable` → the hook's
+ * fail-mode.
+ */
+ import { SsrfBlockedError, requestPinned, resolvePinnedPublicIp } from '../../net/ssrf'
+ import type { HookFetcher, LifecyclePoint } from './types'
+
+ /** Minimal fetch surface (injectable for tests + the in-cluster path). */
+ export type FetchLike = (
+ url: string,
+ init: { method: string; headers: Record<string, string>; body: string; signal: AbortSignal }
+ ) => Promise<{ status: number; text: () => Promise<string> }>
+
+ export interface HookFetcherDeps {
+ getAuthToken: () => string
+ timeoutMs?: number
+ /** Tight cap for non-rewrite responses (moderate/post_call/on_error/tool). Default 1 MiB. */
+ maxOutputBytes?: number
+ /** Generous cap for `pre_call` rewrite output (the whole conversation). Default 5 MiB (~1M tokens). */
+ maxRewriteBytes?: number
+ /** In-cluster transport. Defaults to the global `fetch`. */
+ fetchImpl?: FetchLike
+ /** External (remote) transport. Defaults to the SSRF-guarded impl. Injectable for tests. */
+ externalFetchImpl?: FetchLike
+ }
+
+ const POINT_PATH: Record<LifecyclePoint, string> = {
+ pre_call: 'pre_call',
+ moderate: 'moderate',
+ post_call: 'post_call',
+ on_error: 'on_error',
+ pre_tool_use: 'pre_tool_use',
+ post_tool_use: 'post_tool_use',
+ }
+
+ function sanitizeForLog(value: unknown): string {
- return String(value).replace(/[\r\n]+/g, ' ').replace(/[\u0000-\u001F\u007F]/g, '')
++ return String(value)
++ .replace(/[\r\n]+/g, ' ')
++ .replace(/[\u0000-\u001F\u007F]/g, '')
+ }
+
+ /** Build `{endpoint}{path}/v1/{point}`, normalizing slashes (spec §8.1). */
+ export function buildHookUrl(endpoint: string, path: string, point: LifecyclePoint): string {
+ const base = endpoint.replace(/\/+$/, '')
+ const mid = !path || path === '/' ? '' : '/' + path.replace(/^\/+|\/+$/g, '')
+ return `${base}${mid}/v1/${POINT_PATH[point]}`
+ }
+
+ /**
+ * SSRF-guarded transport for `remote` hook targets: requires https, validates
+ * the host resolves only to public IPs, and connects to a pinned IP. Adapts to
+ * the `FetchLike` shape so the outcome classification is shared with the
+ * in-cluster path. Throws `SsrfBlockedError` on a blocked/unverifiable host.
+ */
+ function makeSsrfGuardedFetch(timeoutMs: number, maxBytes: number): FetchLike {
+ return async (urlStr, init) => {
+ const url = new URL(urlStr)
+ if (url.protocol !== 'https:') {
+ throw new SsrfBlockedError(`remote hook URL must be https (got ${url.protocol})`)
+ }
+ const pinnedIp = await resolvePinnedPublicIp(url) // throws SsrfBlockedError on private/unresolvable
+ const { statusCode, body } = await requestPinned({
+ url,
+ method: init.method,
+ headers: init.headers,
+ body: init.body,
+ pinnedIp,
+ timeoutMs,
+ // Absolute deadline (idle-independent) + streaming byte cap: a trickling or
+ // flooding remote hook can no longer hang or OOM the pod (settles → §8.6).
+ signal: init.signal,
+ maxBytes,
+ })
+ return { status: statusCode, text: async () => body }
+ }
+ }
+
+ /**
+ * In-cluster transport (default): the global `fetch` honors the abort `signal`
+ * (absolute deadline) and the body is read WITH a streaming byte cap, so an
+ * oversized in-cluster response is never fully buffered either.
+ */
+ function makeInClusterFetch(maxBytes: number): FetchLike {
+ return async (urlStr, init) => {
+ const res = await (globalThis.fetch as unknown as (u: string, i: unknown) => Promise<Response>)(
+ urlStr,
+ init
+ )
+ return {
+ status: res.status,
+ text: async () => {
+ const stream = res.body as ReadableStream<Uint8Array> | null
+ if (!stream) return res.text()
+ const reader = stream.getReader()
+ const chunks: Buffer[] = []
+ let total = 0
+ for (;;) {
+ const { done, value } = await reader.read()
+ if (done) break
+ if (value) {
+ total += value.length
+ if (total > maxBytes) {
+ await reader.cancel()
+ throw new Error('response exceeded maxBytes') // → caught → unavailable
+ }
+ chunks.push(Buffer.from(value))
+ }
+ }
+ return Buffer.concat(chunks).toString('utf-8')
+ },
+ }
+ }
+ }
+
+ export function createHookFetcher(deps: HookFetcherDeps): HookFetcher {
+ const timeoutMs = deps.timeoutMs ?? 5000
+ // Point-aware response caps (§8.1). A `pre_call` `may_rewrite` output IS the whole
+ // (possibly compressed) conversation — legitimately large — whereas a `moderate`
+ // verdict, a `post_call` redaction, or an `on_error` recovery is small. A single
+ // tight cap for all silently discards rewrites (they blow past 64 KiB) and no-ops
+ // the hook. So: a generous cap for pre_call, a tight cap for everything else.
+ const rewriteCap = deps.maxRewriteBytes ?? 5 * 1024 * 1024 // pre_call: ~1M tokens
+ const responseCap = deps.maxOutputBytes ?? 1 * 1024 * 1024 // moderate / post_call / on_error / tool
+ // The streaming transports enforce only the LARGER ceiling — purely for memory
+ // safety (OOM/flood, §8.6). The per-point APPLICATION cap is checked after parse.
+ const memoryCeiling = Math.max(rewriteCap, responseCap)
+ const inClusterFetch: FetchLike = deps.fetchImpl ?? makeInClusterFetch(memoryCeiling)
+ const externalFetch: FetchLike =
+ deps.externalFetchImpl ?? makeSsrfGuardedFetch(timeoutMs, memoryCeiling)
+
+ return async ({ point, descriptor, body }) => {
+ const url = buildHookUrl(descriptor.endpoint, descriptor.path, point)
+ const doFetch = descriptor.external ? externalFetch : inClusterFetch
+ const applyCap = point === 'pre_call' ? rewriteCap : responseCap
+ try {
+ const res = await doFetch(url, {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Authorization: `Bearer ${deps.getAuthToken()}`,
+ },
+ body: JSON.stringify(body),
+ signal: AbortSignal.timeout(timeoutMs),
+ })
+ const text = await res.text()
+ if (text.length > applyCap) {
+ // Over the per-point cap → cannot be applied. NEVER silent (this used to
+ // no-op a rewrite invisibly), and flagged `oversized` so the §8.6 breaker
+ // does NOT count "response too big" as a hook-down failure (§12.4).
+ console.warn(
+ `[Guardrails] hook ${descriptor.id} ${point} response ${text.length}B exceeds the ` +
+ `${point === 'pre_call' ? 'rewrite' : 'response'} cap (${applyCap}B) — discarding as unavailable`
+ )
+ return { status: res.status, body: undefined, unavailable: true, oversized: true }
+ }
+ let parsed: unknown = {}
+ if (text) {
+ try {
+ parsed = JSON.parse(text)
+ } catch {
+ return { status: res.status, body: undefined, unavailable: true } // malformed → unavailable
+ }
+ }
+ // 5xx → unavailable; 2xx/4xx are returned for the mapping to classify.
+ return { status: res.status, body: parsed, unavailable: res.status >= 500 }
+ } catch (err) {
+ // timeout / connection error / SSRF refusal → unavailable (→ fail-mode, §8.6).
+ if (err instanceof SsrfBlockedError) {
+ const safeErrMessage = sanitizeForLog(err.message)
+ const safeDescriptorId = sanitizeForLog(descriptor.id)
+ console.warn(
+ `[Guardrails] remote hook ${safeDescriptorId} blocked by SSRF guard: ${safeErrMessage}`
+ )
+ }
+ return { status: 0, body: undefined, unavailable: true }
+ }
+ }
+ }