Skip to content

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

TypeScript

fix(guardrails): security hardening from review

Andy Paviac927bcddc135
diff --git a/deploy/base/llm-hooks/rbac.yaml b/deploy/base/llm-hooks/rbac.yaml
index 84d96b97b..f755bfc5e 100644
--- a/deploy/base/llm-hooks/rbac.yaml
+++ b/deploy/base/llm-hooks/rbac.yaml
@@ -99,9 +99,12 @@ metadata:
name: mcp-host-hook-reader
namespace: llm-hooks
rules:
+ # `get` only — mcp-host resolves each referenced hook by name (getLlmHook); it
+ # never lists/watches. Withholding list/watch prevents a host SA from
+ # enumerating other tenants' hooks (it must already know the name).
- apiGroups: ["clerum.io"]
resources: ["llmhooks"]
- verbs: ["get", "list", "watch"]
+ verbs: ["get"]
- apiGroups: ["clerum.io"]
resources: ["llmhooks/status"]
verbs: ["get"]
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 557b9c78e..20030b14a 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.249",
+ "version": "0.1.250",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.249",
+ "version": "0.1.250",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 82817857a..61cb5a33f 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.249",
+ "version": "0.1.250",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/__tests__/guardrailHookResolver.test.ts b/mcp-host/src/__tests__/guardrailHookResolver.test.ts
index 75cca40a2..c258dce34 100644
--- a/mcp-host/src/__tests__/guardrailHookResolver.test.ts
+++ b/mcp-host/src/__tests__/guardrailHookResolver.test.ts
@@ -78,19 +78,21 @@ describe('resolveGuardrailHookDescriptors', () => {
expect(out[0].lifecyclePoints).toEqual(['pre_call'])
})
- it('resolves a remote-target hook to its baseUrl', async () => {
+ it('does NOT resolve a remote-target hook (disabled pending SSRF-guarded transport, §8.3)', async () => {
const cr: LlmHookCR = {
metadata: { name: 'r' },
spec: {
target: { remote: { baseUrl: 'https://hooks.example.com' } },
lifecyclePoints: ['preCall'],
+ capabilities: ['may_deny'],
},
}
const out = await resolveGuardrailHookDescriptors(
guardrails({ preCall: [{ id: 'r' }] }),
deps({ r: cr })
)
- expect(out[0].endpoint).toBe('https://hooks.example.com')
+ // Fail-closed: the external dial is not enabled until the SSRF guard lands.
+ expect(out).toEqual([])
})
it('skips a dangling reference (CR not found)', async () => {
diff --git a/mcp-host/src/core/adapters/hookedLlmPort.ts b/mcp-host/src/core/adapters/hookedLlmPort.ts
index 68c14d40a..fe3726f5e 100644
--- a/mcp-host/src/core/adapters/hookedLlmPort.ts
+++ b/mcp-host/src/core/adapters/hookedLlmPort.ts
@@ -23,7 +23,7 @@
* Response-capability enforcement (F4), system-prompt immutability (N4), and
* subtractive actions (N5) are enforced inside `RemoteLlmHook`.
*/
-import { aggregateDecision, pickPresentationReason } from '../guardrails'
+import { aggregateDecision, guardrailDecisionsTotal, pickPresentationReason } from '../guardrails'
import type { Contributor, GuardrailsConfig } from '../guardrails'
import { type RequestShaper, buildLlmBuiltinChain } from '../guardrails/llm/builtinChain'
import {
@@ -58,6 +58,25 @@ const REFUSAL_BY_REASON: Record<string, string> = {
}
const DEFAULT_REFUSAL = "I can't help with that request."
+/** Bounded set for the metric `reason_code` label — the hook's `body.code` is
+ * attacker-influenced, so unknown codes collapse to `hook_deny` to keep the
+ * label a fixed enum (no cardinality blow-up / label injection, spec §9). */
+const KNOWN_REASON_CODES = new Set([
+ 'moderation_blocked',
+ 'jailbreak_blocked',
+ 'content_filtered',
+ 'hook_unavailable',
+])
+function boundedReason(reason?: string): string {
+ return reason && KNOWN_REASON_CODES.has(reason) ? reason : 'hook_deny'
+}
+/** Sanitize an untrusted reason for a single-line log (strip CR/LF/TAB, cap length). */
+function logSafeReason(reason?: string): string {
+ return String(reason ?? 'unknown')
+ .replace(/[\r\n\t]+/g, ' ')
+ .slice(0, 80)
+}
+
/**
* A denied model call renders as a GRACEFUL assistant refusal — `finish_reason:
* Stop` + refusal text — not a content-filter error. The turn then flows through
@@ -98,6 +117,30 @@ export class HookedLlmPort implements LlmPort {
return this.inner.complete(request)
}
+ /**
+ * A guardrail deny → graceful refusal. Records the block on
+ * `guardrailDecisionsTotal` (the LLM lane bypasses the boundary's own
+ * recordDecision, so without this a deny would be invisible to telemetry) and
+ * logs it, then returns the first-party refusal completion.
+ */
+ private denyRefusal(
+ contribs: ReadonlyArray<Contributor<ToolCompletionRequest, ToolCompletionResponse>>,
+ point: 'pre_call' | 'moderate'
+ ): ToolCompletionResponse {
+ const reason = pickPresentationReason(contribs, 'deny')
+ guardrailDecisionsTotal.inc({
+ lane: 'llm',
+ decision: 'deny',
+ source: 'hook',
+ execution_mode: point === 'moderate' ? 'concurrent' : 'blocking',
+ phase: 'pre',
+ outcome: 'denied',
+ reason_code: boundedReason(reason),
+ })
+ console.warn(`[HookedLlmPort] ${point} deny — refusing turn (reason=${logSafeReason(reason)})`)
+ return refusalResponse(reason)
+ }
+
/** Main lane (spec §7): built-ins → pre_call → dispatch (moderation concurrent with the call). */
async completeWithTools(request: ToolCompletionRequest): Promise<ToolCompletionResponse> {
let req = this.shapeMainLane(request)
@@ -112,11 +155,7 @@ export class HookedLlmPort implements LlmPort {
if (c.rewrite) req = c.rewrite // already system-role-stripped + capability-gated (N4/F4)
}
}
- if (aggregateDecision(preContribs) === 'deny') {
- const reason = pickPresentationReason(preContribs, 'deny')
- console.warn(`[HookedLlmPort] pre_call deny — refusing turn (reason=${reason ?? 'unknown'})`)
- return refusalResponse(reason)
- }
+ if (aggregateDecision(preContribs) === 'deny') return this.denyRefusal(preContribs, 'pre_call')
return this.hooks.moderate.length === 0
? this.dispatch(req)
@@ -163,11 +202,7 @@ export class HookedLlmPort implements LlmPort {
const contribs = await moderationContribs
if (aggregateDecision(contribs) === 'deny') {
controller.abort() // cancel the in-flight call (§7.1)
- const reason = pickPresentationReason(contribs, 'deny')
- console.warn(
- `[HookedLlmPort] moderation deny — refusing turn (reason=${reason ?? 'unknown'})`
- )
- return refusalResponse(reason)
+ return this.denyRefusal(contribs, 'moderate')
}
const outcome = await modelOutcome
diff --git a/mcp-host/src/guardrailHookResolver.ts b/mcp-host/src/guardrailHookResolver.ts
index 70de38dda..2f0227419 100644
--- a/mcp-host/src/guardrailHookResolver.ts
+++ b/mcp-host/src/guardrailHookResolver.ts
@@ -141,6 +141,19 @@ export async function resolveGuardrailHookDescriptors(
continue
}
+ // SSRF posture (§8.3): the hook fetcher has no private-range/metadata-IP
+ // guard yet and the base mcp-host egress permits 0.0.0.0/0:443, so dialing an
+ // admin-authored external URL would ship the full conversation to an arbitrary
+ // host (incl. 169.254.169.254). Remote targets are therefore NOT resolved
+ // until the SSRF-guarded transport lands — fail-closed (the hook does not run)
+ // rather than enabling an unvalidated external dial.
+ if (ep.kind === 'remote') {
+ console.warn(
+ `[Guardrails] LlmHook ${id} is a remote target — disabled pending SSRF-guarded transport (§8.3); skipping`
+ )
+ continue
+ }
+
const points = (cr.spec?.lifecyclePoints ?? [])
.map(p => PHASE_TO_POINT[p])
.filter((p): p is LifecyclePoint => !!p)
@@ -150,7 +163,10 @@ export async function resolveGuardrailHookDescriptors(
}
// Digest binding (§8.2): an image hook whose reconciled digest no longer
- // matches the admin-pinned reference is not trusted — skip (fail-closed).
+ // matches the admin-pinned reference is not the reviewed image — skip it.
+ // NOTE: skipping drops the hook from the lane, which is fail-OPEN for that
+ // phase; turning a mismatch into an authoritative deny for fail-closed hooks
+ // is a tracked follow-up (resolution fail-posture).
if (
ep.kind === 'image' &&
refDigest &&