Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(control-api): add the pinnedFetch pin unit net the docstring promised (R2-L1)
Diego Zacarias0fc5a0e29a65
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 50c00993a..f799c7af2 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.648",+ "version": "0.1.649","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.648",+ "version": "0.1.649","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.jsonindex 8b15eaf27..be4b5b6ca 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.648",+ "version": "0.1.649","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/test/http.pinnedFetch.test.ts b/control-api/test/http.pinnedFetch.test.tsnew file mode 100644index 000000000..b8114c663--- /dev/null+++ b/control-api/test/http.pinnedFetch.test.ts@@ -0,0 +1,99 @@+import { describe, expect, it } from 'vitest'+import type { LookupFunction } from 'node:net'+import { type PinnedTransport, pinnedFetch } from '../src/http/pinnedFetch.js'+import type { DnsResolver } from '../src/http/validateMcpServerSpec.js'++/**+ * R2-L1 — the pin's security guarantee gets the unit net the docstring already+ * promised: the socket connects to the exact IP the kernel validated+ * (connectedIP === validatedIP), DNS resolves ONCE per hop (the re-resolution the+ * pin exists to prevent never happens), and the host stays the hostname (never the+ * IP) so SNI/Host/TLS keep verifying against it.+ *+ * The validated addresses flow through the REAL resolveValidatedOAuthEndpoint →+ * pinnedLookup (T1): the test injects only the DNS seam and a transport that+ * invokes the pinned lookup to observe the IP the socket would reach — no real+ * network.+ */++const VALIDATED_IP = '93.184.216.34' // public, non-blocked++function countingResolver(ips: string[]): { resolveDns: DnsResolver; calls: string[] } {+ const calls: string[] = []+ return {+ calls,+ resolveDns: async (hostname: string) => {+ calls.push(hostname)+ return ips+ },+ }+}++/**+ * A transport that invokes the pinned lookup exactly as `node:https` would, to+ * record the IP the socket would connect to — with no real network.+ */+function observingTransport(): {+ transport: PinnedTransport+ seen: { url: string; connectedIps: string[]; invoked: boolean }+} {+ const seen = { url: '', connectedIps: [] as string[], invoked: false }+ const transport: PinnedTransport = input =>+ new Promise((resolve, reject) => {+ seen.invoked = true+ seen.url = input.url+ const hostname = new URL(input.url).hostname+ ;(input.lookup as LookupFunction)(hostname, { all: true }, (err, addrs) => {+ if (err) {+ reject(err)+ return+ }+ const list = Array.isArray(addrs) ? addrs.map(a => a.address) : [addrs as unknown as string]+ seen.connectedIps.push(...list)+ resolve({ status: 200, headers: { 'content-type': 'application/json' }, bodyText: '{}' })+ })+ })+ return { transport, seen }+}++describe('pinnedFetch — the socket pins to the validated IP (R2-L1)', () => {+ it('connectedIP === validatedIP and resolveDns runs exactly once per hop', async () => {+ const { resolveDns, calls } = countingResolver([VALIDATED_IP])+ const { transport, seen } = observingTransport()++ const result = await pinnedFetch(+ 'https://as.example.com/.well-known/oauth-authorization-server',+ 'test',+ {+ resolveDns,+ transport,+ }+ )++ expect(result.ok).toBe(true)+ if (!result.ok) return+ // The socket pinned to the exact IP the kernel validated.+ expect(seen.connectedIps).toEqual([VALIDATED_IP])+ expect(result.response.pinnedAddresses).toEqual([VALIDATED_IP])+ // DNS resolved exactly once — no second resolution to slip a private IP in.+ expect(calls).toEqual(['as.example.com'])+ // Host stays the hostname (never the IP) so SNI/Host/TLS verify against it.+ expect(new URL(seen.url).hostname).toBe('as.example.com')+ })++ it('a hostname resolving only to a blocked IP is never fetched (fail closed)', async () => {+ const { resolveDns } = countingResolver(['169.254.169.254']) // link-local metadata+ const { transport, seen } = observingTransport()++ const result = await pinnedFetch('https://evil.example.com/x', 'test', {+ resolveDns,+ transport,+ })++ expect(result.ok).toBe(false)+ if (result.ok) return+ expect(result.error.kind).toBe('kernel_rejected')+ // The transport was never reached — no connection attempt to a blocked IP.+ expect(seen.invoked).toBe(false)+ })+})