Skip to content

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

TypeScript

fix(control-api): anchor the digest pin on install-hook too (m4)

Andy Pavia626de7ae0c93
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index be2a61d83..971556d02 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.468",
+ "version": "0.1.469",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.468",
+ "version": "0.1.469",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/image-policy": "file:../packages/image-policy",
diff --git a/control-api/package.json b/control-api/package.json
index 6e03124d4..4d711e8cd 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.468",
+ "version": "0.1.469",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/routes/admin/__tests__/digestPinnedImageRef.test.ts b/control-api/src/routes/admin/__tests__/digestPinnedImageRef.test.ts
new file mode 100644
index 000000000..e624aed71
--- /dev/null
+++ b/control-api/src/routes/admin/__tests__/digestPinnedImageRef.test.ts
@@ -0,0 +1,51 @@
+import { describe, expect, it } from 'vitest'
+import { imageRefDigest, isDigestPinnedImageRef } from '../registry.js'
+
+const SHA = 'sha256:' + 'a'.repeat(64)
+
+/**
+ * install-hook and upgrade-hook both gate on this and both derive the Host-side
+ * digest pin from it. The regression it guards: `/@sha256:…$/` unanchored plus
+ * `split('@')[1]` accepted `repo@bar@sha256:…` and pinned `bar` into every
+ * Host's guardrails.hooks[].digest — the value mcp-host compares against
+ * status.observedDigest to detect drift and quarantine a hook.
+ */
+describe('isDigestPinnedImageRef', () => {
+ it('accepts a normally pinned ref', () => {
+ expect(isDigestPinnedImageRef(`registry.example.com/acme/hook@${SHA}`)).toBe(true)
+ })
+
+ it('rejects a ref with a second @ before the digest', () => {
+ expect(isDigestPinnedImageRef(`repo@bar@${SHA}`)).toBe(false)
+ })
+
+ it.each([
+ ['a tag instead of a digest', 'registry.example.com/acme/hook:1.2.3'],
+ ['no digest at all', 'registry.example.com/acme/hook'],
+ ['a truncated digest', 'registry.example.com/acme/hook@sha256:abc'],
+ ['a non-hex digest', `registry.example.com/acme/hook@sha256:${'z'.repeat(64)}`],
+ ['trailing content after the digest', `registry.example.com/acme/hook@${SHA} `],
+ ['an empty ref', ''],
+ ])('rejects %s', (_label, ref) => {
+ expect(isDigestPinnedImageRef(ref)).toBe(false)
+ })
+
+ it.each([undefined, null, 42, {}])('rejects the non-string %s', ref => {
+ expect(isDigestPinnedImageRef(ref)).toBe(false)
+ })
+})
+
+describe('imageRefDigest', () => {
+ it('returns the digest of a pinned ref', () => {
+ expect(imageRefDigest(`registry.example.com/acme/hook@${SHA}`)).toBe(SHA)
+ })
+
+ it('returns undefined for a double-@ ref rather than the middle segment', () => {
+ // The old split('@')[1] returned 'bar' here, which then became the Host pin.
+ expect(imageRefDigest(`repo@bar@${SHA}`)).toBeUndefined()
+ })
+
+ it('returns undefined when the ref is not pinned', () => {
+ expect(imageRefDigest('registry.example.com/acme/hook:1.2.3')).toBeUndefined()
+ })
+})
diff --git a/control-api/src/routes/admin/registry.ts b/control-api/src/routes/admin/registry.ts
index eae0f5adf..2d25b8b40 100644
--- a/control-api/src/routes/admin/registry.ts
+++ b/control-api/src/routes/admin/registry.ts
@@ -112,6 +112,29 @@ export function catalogAnnotations(entryName: string, version: string): Record<s
}
}
+/**
+ * A digest-pinned image ref: exactly one `@`, immediately followed by
+ * `sha256:<64 hex>` and nothing else.
+ *
+ * Anchoring at the front is what makes it exact. An unanchored `/@sha256:…$/`
+ * also accepts `repo@bar@sha256:…`, and the digest was then read with
+ * `split('@')[1]` — which yields `bar`. That value is written into every Host's
+ * `guardrails.hooks[].digest`, and the Host-side pin is exactly what mcp-host
+ * compares against `status.observedDigest` to detect drift and quarantine a
+ * hook, so a junk pin degrades the check that quarantine depends on.
+ *
+ * Shared by install-hook and upgrade-hook so the two cannot drift apart again —
+ * they did: upgrade was fixed while install kept both halves of the bug.
+ */
+export function isDigestPinnedImageRef(ref: unknown): boolean {
+ return typeof ref === 'string' && /^[^@]+@sha256:[0-9a-f]{64}$/.test(ref)
+}
+
+/** The `sha256:…` digest of a ref that passed `isDigestPinnedImageRef`. */
+export function imageRefDigest(ref: unknown): string | undefined {
+ return isDigestPinnedImageRef(ref) ? (ref as string).split('@').pop() : undefined
+}
+
const MAX_RECIPE_YAML_SIZE = 100 * 1024 // 100 KB (spec §7.3)
const DELETE_SETTLE_TIMEOUT_MS = 3_000
const DELETE_SETTLE_POLL_MS = 100
@@ -1907,7 +1930,7 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
// Step 7 — image preflight: digest-pinned (§8.2 resolution) + allowlist (§8.5).
const image = hookMeta.target.image
if (image) {
- if (!/@sha256:[0-9a-f]{64}$/.test(image.ref ?? '')) {
+ if (!isDigestPinnedImageRef(image.ref)) {
res.status(422).json({ error: 'image_ref_not_digest_pinned' })
return
}
@@ -2065,7 +2088,7 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
// Step 10 — reference the hook from Host.spec.guardrails.hooks[phase] as
// {id, digest}. If this fails the hook exists but no Host runs it, so roll
// the CR (+ Secret) back rather than return a false success.
- const digest = image?.ref?.includes('@') ? image.ref.split('@')[1] : undefined
+ const digest = imageRefDigest(image?.ref)
try {
// Atomic read→derive→write (shared with the uninstall/upgrade paths):
// re-reads the Host, derives the hooks map from THAT read, and sends its
@@ -2226,9 +2249,7 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
const newImage = hookMeta.target.image
let newDigest: string | undefined
if (newImage) {
- // Anchored, and take the LAST @-segment: an unanchored test plus
- // split('@')[1] pinned "bar" for a ref like `repo@bar@sha256:…`.
- if (!/^[^@]+@sha256:[0-9a-f]{64}$/.test(newImage.ref ?? '')) {
+ if (!isDigestPinnedImageRef(newImage.ref)) {
res.status(422).json({ error: 'image_ref_not_digest_pinned' })
return
}
@@ -2239,7 +2260,7 @@ export function createAdminRegistryRouter(gateway?: K8sGateway): Router {
res.status(422).json({ error: 'image_not_allowlisted', reason: newImage.ref })
return
}
- newDigest = newImage.ref?.includes('@') ? newImage.ref.split('@').pop() : undefined
+ newDigest = imageRefDigest(newImage.ref)
}
// Host-independent admissibility gates — upgrade-hook is the sanctioned