Skip to content

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

TypeScript

fix(control-api): block loopback and private ranges in models.dev egress guard (R1-M1)

Diego Zacariasf60828d4f2ba
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index bc7880f09..80584b3fb 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.446",
+ "version": "0.1.447",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.446",
+ "version": "0.1.447",
"dependencies": {
"@clerum/image-policy": "file:../packages/image-policy",
"@clerum/llm-providers": "file:../packages/llm-providers",
diff --git a/control-api/package.json b/control-api/package.json
index e78dc1749..e361dcf57 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.446",
+ "version": "0.1.447",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/services/modelsDevClient.ts b/control-api/src/services/modelsDevClient.ts
index 3d7edb978..c39e1a6f2 100644
--- a/control-api/src/services/modelsDevClient.ts
+++ b/control-api/src/services/modelsDevClient.ts
@@ -14,6 +14,7 @@
* parse-guard) as defense-in-depth. No auth header is sent (the catalog is
* public) and the response body is never logged.
*/
+import { isIP } from 'node:net'
import { type LlmProviderId, PROVIDER_IDS } from '@clerum/llm-providers'
import { VENDORED_MODELS_DEV_SNAPSHOT } from '../data/modelsDevSnapshot.js'
@@ -196,14 +197,89 @@ function isRawCatalog(value: unknown): value is RawModelsDevCatalog {
)
}
+/**
+ * Hostname suffixes that only ever resolve inside a cluster / private network.
+ * `.svc.cluster.local` ends with `.local` (and `.svc`), so the shorter suffixes
+ * already cover it — it is listed for documentation, not because it is needed.
+ */
+const INTERNAL_HOST_SUFFIXES = ['.internal', '.local', '.svc', '.svc.cluster.local'] as const
+
+/**
+ * Classify an IPv4 literal as a private / loopback / link-local / "this host"
+ * target that egress must never reach. Ranges (not a single address each):
+ * - 127.0.0.0/8 loopback (all of it, not just 127.0.0.1)
+ * - 0.0.0.0/8 "this host" / unspecified
+ * - 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 RFC1918 private
+ * - 169.254.0.0/16 link-local (incl. 169.254.169.254 cloud metadata)
+ */
+function isBlockedV4(ip: string): boolean {
+ const parts = ip.split('.').map(p => Number(p))
+ if (parts.length !== 4 || parts.some(p => !Number.isInteger(p) || p < 0 || p > 255)) {
+ // isIP() said v4, so this should not happen; fail closed (treat as blocked).
+ return true
+ }
+ const [a, b] = parts
+ if (a === 127 || a === 0) return true
+ if (a === 10) return true
+ if (a === 172 && b >= 16 && b <= 31) return true
+ if (a === 192 && b === 168) return true
+ if (a === 169 && b === 254) return true
+ return false
+}
+
+/**
+ * Classify an IPv6 literal (brackets already stripped) as loopback / unspecified
+ * / IPv4-mapped-private / link-local / unique-local. `URL.hostname` yields IPv6
+ * *with* brackets (e.g. `[::1]`), so callers strip them before this. The
+ * IPv4-mapped case is re-classified through isBlockedV4 so every private v4
+ * tunneled as v6 is caught too — in BOTH the dotted-quad tail (`::ffff:127.0.0.1`)
+ * and the hex form the WHATWG `URL` parser normalizes it to (`::ffff:7f00:1`).
+ */
+function isBlockedV6(ip: string): boolean {
+ const h = ip.toLowerCase()
+ if (h === '::1' || h === '::') return true
+ // IPv4-mapped tail written in dotted-quad form (`::ffff:127.0.0.1`).
+ const dotted = /:((?:\d{1,3}\.){3}\d{1,3})$/.exec(h)
+ if (dotted && isBlockedV4(dotted[1])) return true
+ // IPv4-mapped tail in the hex form `URL` normalizes to (`::ffff:7f00:1`): the
+ // last two 16-bit groups carry the v4 (high octets, low octets).
+ const hex = /:ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(h)
+ if (hex) {
+ const hi = parseInt(hex[1], 16)
+ const lo = parseInt(hex[2], 16)
+ const v4 = `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`
+ if (isBlockedV4(v4)) return true
+ }
+ if (/^fe[89ab]/.test(h)) return true // fe80::/10 link-local
+ if (/^f[cd]/.test(h)) return true // fc00::/7 unique-local
+ return false
+}
+
+/** Classify a (non-IP) hostname as an internal / bare cluster egress target. */
+function isBlockedHostname(host: string): boolean {
+ if (host === 'localhost') return true
+ // A bare, dot-less name only resolves inside a cluster's search domain
+ // (`kubernetes`, `myservice`), never on the public internet.
+ if (!host.includes('.')) return true
+ return INTERNAL_HOST_SUFFIXES.some(suffix => host.endsWith(suffix))
+}
+
/**
* Assert the resolved catalog URL is a safe egress target BEFORE any fetch. The
* module header promises an https-only guard; with the `MODELS_DEV_API_URL` env
* override in play, a misconfigured deploy could otherwise resolve to `http://`
- * or an internal/link-local host (cloud metadata at 169.254.169.254, a cluster
- * `.internal` Service). This is a deploy-time input, not operator/tenant-facing,
- * but the boundary is real — so a bad URL is a hard error, not a silent
- * vendored-fallback that would hide the misconfiguration forever.
+ * or an internal host — cloud metadata (169.254.169.254), loopback (127.0.0.1,
+ * [::1]), an RFC1918 address (10/8, 172.16/12, 192.168/16), or a cluster Service
+ * (`.internal`, `.svc`, a bare `kubernetes`). This is a deploy-time input, not
+ * operator/tenant-facing, but the boundary is real — so a bad URL is a hard
+ * error, not a silent vendored-fallback that would hide the misconfiguration
+ * forever.
+ *
+ * Approach: RANGE/suffix CLASSIFICATION, not a `models.dev`-only allowlist. The
+ * documented purpose of the override is to point a non-prod plane at a local
+ * stub (a host that is by definition NOT models.dev); a strict allowlist would
+ * nullify the override entirely, so classification is used and every block is
+ * driven by the target being internal, not by it not being models.dev.
*/
function assertSafeCatalogUrl(rawUrl: string): void {
let parsed: URL
@@ -215,15 +291,17 @@ function assertSafeCatalogUrl(rawUrl: string): void {
if (parsed.protocol !== 'https:') {
throw new Error(`models.dev catalog URL must be https:// (got "${parsed.protocol}//")`)
}
- const host = parsed.hostname.toLowerCase()
- const linkLocalOrMetadata =
- host === 'localhost' ||
- host === '::1' ||
- host === '169.254.169.254' ||
- host.startsWith('169.254.') || // IPv4 link-local range (incl. cloud metadata)
- host === 'metadata.google.internal' ||
- host.endsWith('.internal')
- if (linkLocalOrMetadata) {
+ // `URL.hostname` keeps IPv6 literals wrapped in brackets ("[::1]"); strip them
+ // so both the bracketed form and net.isIP() see the bare address.
+ const host = parsed.hostname.toLowerCase().replace(/^\[|\]$/g, '')
+ const ipVersion = isIP(host)
+ const blocked =
+ ipVersion === 4
+ ? isBlockedV4(host)
+ : ipVersion === 6
+ ? isBlockedV6(host)
+ : isBlockedHostname(host)
+ if (blocked) {
throw new Error(`models.dev catalog URL host is not an allowed egress target: ${host}`)
}
}
diff --git a/control-api/test/services.modelsDevClient.urlGuard.test.ts b/control-api/test/services.modelsDevClient.urlGuard.test.ts
index 5d29cdaa5..e39f24c6b 100644
--- a/control-api/test/services.modelsDevClient.urlGuard.test.ts
+++ b/control-api/test/services.modelsDevClient.urlGuard.test.ts
@@ -46,6 +46,30 @@ describe('models.dev catalog URL egress guard', () => {
expect(fetchImpl).not.toHaveBeenCalled()
})
+ // Regression (R1-M1): before the fix the guard blocked only by exact-string
+ // match, so these internal targets slipped through the string checks and were
+ // let out to fetch. They must now THROW before any fetch (T4: assert the
+ // observable outcome — the guard rejects and the network is never touched).
+ it.each([
+ ['loopback 127.0.0.1', 'https://127.0.0.1/api.json'],
+ ['loopback range 127.5.5.5', 'https://127.5.5.5/api.json'],
+ ['unspecified 0.0.0.0', 'https://0.0.0.0/api.json'],
+ ['RFC1918 10/8', 'https://10.0.0.5/api.json'],
+ ['RFC1918 172.16/12', 'https://172.16.9.9/api.json'],
+ ['RFC1918 192.168/16', 'https://192.168.1.1/api.json'],
+ ['IPv6 loopback [::1]', 'https://[::1]/api.json'],
+ ['IPv4-mapped [::ffff:127.0.0.1]', 'https://[::ffff:127.0.0.1]/api.json'],
+ ['bare service name', 'https://kubernetes/api.json'],
+ ['.svc cluster Service', 'https://kube-apiserver.default.svc/api.json'],
+ ])('rejects internal target (%s) and never fetches', async (_label, url) => {
+ const { loadModelsDevCatalog } = await loadWith(url)
+ const fetchImpl = vi.fn()
+ await expect(loadModelsDevCatalog({ fetchImpl: fetchImpl as never })).rejects.toThrow(
+ /egress|host/i
+ )
+ expect(fetchImpl).not.toHaveBeenCalled()
+ })
+
it('passes the guard for the default https host (fetch IS attempted)', async () => {
const { loadModelsDevCatalog } = await loadWith(undefined) // default https://models.dev/api.json
// A throwing fetch proves the guard let us THROUGH to the network; the loader