Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): close review gaps in the auto-claim register handler
Jose Ramireza71c6edd7562
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 260500750..4ad633e4d 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.371",+ "version": "0.1.372","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.371",+ "version": "0.1.372","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.jsonindex e7215af75..6f0a43cce 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.371",+ "version": "0.1.372","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/registryConnect.ts b/control-api/src/routes/admin/registryConnect.tsindex cc9222f0e..1918537af 100644--- a/control-api/src/routes/admin/registryConnect.ts+++ b/control-api/src/routes/admin/registryConnect.ts@@ -298,11 +298,9 @@ export function createRegistryConnectRouter(): Router {const err = await readErrorCode(regRes)const retryAfter = regRes.headers.get('retry-after')if (retryAfter) res.set('Retry-After', retryAfter)- res- .status(429)- .json({- error: err === 'registration_capacity' ? 'registration_capacity' : 'rate_limited',- })+ res.status(429).json({+ error: err === 'registration_capacity' ? 'registration_capacity' : 'rate_limited',+ })return}if (!regRes.ok) {@@ -313,12 +311,10 @@ export function createRegistryConnectRouter(): Router {res.status(502).json({ error: 'registry_integration_error' })return}- const reg = (await regRes.json()) as {- deployment_id: string- key_id: string- claim_token?: unknown- }- const autoApproved = regRes.status === 201 && typeof reg.claim_token === 'string'+ // Checked BEFORE parsing the body: a 2xx status this route doesn't+ // expect (a proxy splash page, an empty 204) may carry a non-JSON+ // body, and parsing first would throw out of this handler and surface+ // as a bare 500 instead of the intended 502.if (regRes.status !== 201 && regRes.status !== 202) {rootLogger.error({ event: 'registry_connect_register_unexpected', status: regRes.status },@@ -327,6 +323,21 @@ export function createRegistryConnectRouter(): Router {res.status(502).json({ error: 'registry_integration_error' })return}+ const reg = (await regRes.json()) as {+ deployment_id: string+ key_id: string+ claim_token?: unknown+ }+ // 201 IS the registry's approval, independent of whether this+ // particular response body carries a usable token. Deriving the+ // persisted status from the claim_token's presence/type instead of+ // the HTTP status would write 'pending' for an already-approved+ // deployment whenever the token is missing or malformed (partial+ // rollout, response-rewriting proxy). Task 5's recovery endpoint+ // refuses a 'pending' row, so that deployment could never recover and+ // would squat its org name forever.+ const isApproved = regRes.status === 201+ const hasClaimToken = typeof reg.claim_token === 'string'// Persist BEFORE claiming. The keypair is the only artifact that can// recover this deployment (rotate is PoP-gated); claiming first and dying// before the write would burn the one-time token AND lose the key.@@ -338,14 +349,24 @@ export function createRegistryConnectRouter(): Router {requestedOrgName,contactEmail,registryUrl: config.registryUrl,- status: autoApproved ? 'approved' : 'pending',+ status: isApproved ? 'approved' : 'pending',})- if (!autoApproved) {+ if (!isApproved) {res.status(202).json({ state: 'pending', deploymentId: reg.deployment_id, requestedOrgName })return}+ if (!hasClaimToken) {+ rootLogger.error(+ { event: 'registry_connect_auto_claim_missing_token', status: 0 },+ '201 response carried no usable claim_token; recovery is available'+ )+ res+ .status(202)+ .json({ state: 'connecting', deploymentId: reg.deployment_id, requestedOrgName })+ return+ }const outcome = await redeemClaim({deploymentId: reg.deployment_id,keyId: reg.key_id,diff --git a/control-api/test/routes.registryConnect.test.ts b/control-api/test/routes.registryConnect.test.tsindex 07e1bb4ec..a72df5e93 100644--- a/control-api/test/routes.registryConnect.test.ts+++ b/control-api/test/routes.registryConnect.test.ts@@ -1,5 +1,5 @@// test/routes.registryConnect.test.ts-import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest'import express from 'express'import { generateKeyPairSync } from 'node:crypto'import request from 'supertest'@@ -405,7 +405,7 @@ describe('register — auto-claim (201)', () => {it('claims immediately and reports connected', async () => {connDb.getRegistryConnection.mockResolvedValue(null)connDb.markConnected.mockResolvedValue(true)- routeFetch({+ const spy = routeFetch({'/register': () =>json({@@ -425,6 +425,14 @@ describe('register — auto-claim (201)', () => {.expect(200)expect(res.body).toMatchObject({ state: 'connected', org: 'acme' })expect(res.body.authEnabled).toBe(true)+ // The claim call must actually carry the token the registry minted, and+ // be PoP-authenticated — otherwise every auto-approved self-hoster gets a+ // registry-side 401 and silently falls back to 202 connecting.+ const [claimUrl, claimInit] = spy.mock.calls[1]!+ expect(String(claimUrl)).toContain('/claim')+ expect((claimInit as RequestInit).headers).toHaveProperty('DPoP')+ const claimBody = JSON.parse(String((claimInit as RequestInit).body))+ expect(claimBody.claim_token).toBe('tok-abc')})// The mocks return undefined, so `await x` and `void x` produce IDENTICAL@@ -439,6 +447,13 @@ describe('register — auto-claim (201)', () => {await new Promise(r => setImmediate(r))events.push('persisted')})+ // vitest's restoreAllMocks() only restores vi.spyOn registrations and+ // clearAllMocks() only calls mockClear — neither undoes mockImplementation.+ // If the assertion below FAILS (the exact mutation this test exists to+ // catch), a bare mockReset() on the next line never runs and this+ // implementation leaks into every later test in the file. onTestFinished+ // runs regardless of pass/fail.+ onTestFinished(() => connDb.upsertPendingConnection.mockReset())vi.spyOn(globalThis, 'fetch').mockImplementation(async input => {const url = String(input)if (url.includes('/register')) {@@ -456,7 +471,6 @@ describe('register — auto-claim (201)', () => {.send({ requested_org_name: 'acme', contact_email: '[email protected]' }).expect(200)expect(events).toEqual(['register', 'persisted', 'claim'])- connDb.upsertPendingConnection.mockReset()})it("persists status 'approved' and reports connecting when the claim fails", async () => {@@ -506,8 +520,31 @@ describe('register — auto-claim (201)', () => {.post('/admin/registry/connect/request').send({ requested_org_name: 'acme', contact_email: '[email protected]' }).expect(202)- const body = JSON.parse(String((spy.mock.calls[0]![1] as RequestInit).body))+ const init = spy.mock.calls[0]![1] as RequestInit+ const body = JSON.parse(String(init.body))expect(body.deployment_info).toMatchObject({ auto_claim: true })+ // A hung registry must not pin this request indefinitely.+ expect(init.signal).toBeInstanceOf(AbortSignal)+ })++ // Registry truth (201 = approved, one-time token burned) must win over the+ // response body's shape. A partial rollout or a response-rewriting proxy+ // could return 201 without a usable claim_token; if that persisted+ // 'pending', Task 5's recovery endpoint would refuse the row forever and+ // the org name would be squatted permanently.+ it('201 without a usable claim_token still persists approved and reports connecting', async () => {+ connDb.getRegistryConnection.mockResolvedValue(null)+ routeFetch({+ '/register': () => json({ deployment_id: 'dep-1', key_id: 'kid-1', status: 'approved' }, 201),+ })+ const res = await request(app())+ .post('/admin/registry/connect/request')+ .send({ requested_org_name: 'acme', contact_email: '[email protected]' })+ .expect(202)+ expect(res.body).toMatchObject({ state: 'connecting', deploymentId: 'dep-1' })+ expect(connDb.upsertPendingConnection).toHaveBeenCalledWith(+ expect.objectContaining({ status: 'approved' })+ )})})@@ -540,9 +577,23 @@ describe('register — new registry rejections map to distinct codes', () => {['per-IP', 429, { error: 'RATE_LIMITED' }, 429, 'rate_limited'],['bad email', 400, { error: 'invalid_contact_email' }, 400, 'invalid_contact_email'],['blocklist', 400, { error: 'org_blocklisted' }, 400, 'org_blocklisted'],+ ['unknown 400', 400, { error: 'who_knows' }, 400, 'invalid_request'],+ // deployment_info is non-empty as of this task (auto_claim capability+ // declaration), so this registry rejection is newly reachable.+ [+ 'info too large',+ 400,+ { error: 'deployment_info_too_large' },+ 400,+ 'deployment_info_too_large',+ ],['bad pop', 401, { error: 'invalid_pop' }, 502, 'registry_integration_error'],['unknown 409', 409, { error: 'who_knows' }, 502, 'registry_integration_error'],['unparseable 429', 429, 'not json', 429, 'rate_limited'],+ // Pins the ordering fix: the status check must run BEFORE the body is+ // parsed, or a non-JSON body on an unexpected 2xx throws out of the+ // handler and surfaces as a bare 500 instead of the intended 502.+ ['unexpected 200', 200, 'not json', 502, 'registry_integration_error'],]for (const [name, regStatus, regBody, want, code] of cases) {it(`${name} → ${want} ${code}`, async () => {