Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): reject oauth-broker SDK grants on recipes without that broker agent
Jose Gabriel Ramirez Encinas594384247c1b
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 0f85948c4..08289b3e8 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.577",+ "version": "0.1.578","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.577",+ "version": "0.1.578","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 f35e0ce4c..90f6258e2 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.577",+ "version": "0.1.578","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/pluginWorkloadSdk.ts b/control-api/src/routes/admin/pluginWorkloadSdk.tsindex 7b29df4b7..fc9745194 100644--- a/control-api/src/routes/admin/pluginWorkloadSdk.ts+++ b/control-api/src/routes/admin/pluginWorkloadSdk.ts@@ -434,7 +434,17 @@ async function publishPromptBridgeGrantIdentity(input: {)return { error: { status: 503, error: 'recipe_annotation_publish_failed' } }}- if (input.provider && recipeBrokers.length > 0 && !recipeBrokers.includes(input.provider)) {+ // A broker SDK target is spendable only when the recipe's agent / step agents+ // name that same broker: authorize attests live targets from agent/steps, and+ // the promptBridge default target must match the bootstrap agent. A static-+ // agent recipe (RP-009) therefore fails closed here instead of publishing an+ // identity no SDK call can ever redeem. An unassigned target on such a recipe+ // only clears the identity, so legacy grant re-saves keep working.+ if (+ input.provider &&+ !recipeBrokers.includes(input.provider) &&+ (recipeBrokers.length > 0 || input.nextRef !== '')+ ) {if (input.onProviderConflict === 'skip') return {}return { error: { status: 400, error: 'oauth_broker_provider_conflict' } }}diff --git a/control-api/src/services/recipeCodexGrantIdentity.ts b/control-api/src/services/recipeCodexGrantIdentity.tsindex 8a388e7b9..2f6b23270 100644--- a/control-api/src/services/recipeCodexGrantIdentity.ts+++ b/control-api/src/services/recipeCodexGrantIdentity.ts@@ -110,9 +110,11 @@ export async function publishRecipeGrantIdentity(input: {const provider = grok ? 'grok-subscription' : 'codex-subscription'// The recipe's agent / step agents own the grant annotations when they name// an oauth-broker. Publishing another broker's identity would rewrite the- // agent grant (e.g. a Grok SDK key read back as the Codex agent's key).+ // agent grant (e.g. a Grok SDK key read back as the Codex agent's key), and a+ // named key on a recipe with no matching broker agent can never authorize+ // (live-target attestation reads agent/steps only). Clearing is still allowed.const agentBrokers = collectRecipeOauthBrokerProviders(asRecord(current.spec) ?? {})- if (agentBrokers.length > 0 && !agentBrokers.includes(provider)) {+ if ((agentBrokers.length > 0 || next !== unassignedKey) && !agentBrokers.includes(provider)) {throw new RecipeCodexGrantIdentityError(409,'oauth_broker_provider_conflict',diff --git a/control-api/test/routes.adminPluginWorkloadSdk.test.ts b/control-api/test/routes.adminPluginWorkloadSdk.test.tsindex 76c29e31a..6f66c9648 100644--- a/control-api/test/routes.adminPluginWorkloadSdk.test.ts+++ b/control-api/test/routes.adminPluginWorkloadSdk.test.ts@@ -605,6 +605,17 @@ describe('routes/admin/pluginWorkloadSdk — grants', () => {// ── Claim 1b: Codex (oauth-broker) promptBridge targets ─────────────────────describe('codex-subscription promptTargets', () => {+ // A broker SDK target is publishable only on a recipe whose agent names the+ // same broker (RP-009); default these suites to that realistic recipe shape.+ beforeEach(() => {+ getResource.mockResolvedValue({+ spec: {+ agent: { provider: 'codex-subscription', model: 'gpt-5.3-codex' },+ pluginWorkloadSdk: { promptBridge: {} },+ },+ })+ })+const codexGrantBody = {...validGrantBody,provider: 'codex-subscription',@@ -741,6 +752,8 @@ describe('routes/admin/pluginWorkloadSdk — grants', () => {})it('accepts a stored Codex target without connectionRef as unassigned', async () => {+ // Legacy re-save on a recipe with no broker agent: clearing stays allowed.+ getResource.mockResolvedValue({ spec: { pluginWorkloadSdk: { promptBridge: {} } } })vi.mocked(sdkDb.upsertGrant).mockResolvedValue({ id: 'g-legacy' } as never)const { connectionRef: _omitted, ...targetWithoutConnection } =codexGrantBody.promptTargets[0]!@@ -760,6 +773,8 @@ describe('routes/admin/pluginWorkloadSdk — grants', () => {})it('accepts the unassigned sentinel as a stored Codex target', async () => {+ // Legacy re-save on a recipe with no broker agent: clearing stays allowed.+ getResource.mockResolvedValue({ spec: { pluginWorkloadSdk: { promptBridge: {} } } })vi.mocked(sdkDb.upsertGrant).mockResolvedValue({ id: 'g-legacy' } as never)const res = await request(buildApp()).post('/admin/plugin-workload-sdk/grants')@@ -847,6 +862,17 @@ describe('routes/admin/pluginWorkloadSdk — grants', () => {})describe('grok-subscription promptTargets', () => {+ // A broker SDK target is publishable only on a recipe whose agent names the+ // same broker (RP-009); default these suites to that realistic recipe shape.+ beforeEach(() => {+ getResource.mockResolvedValue({+ spec: {+ agent: { provider: 'grok-subscription', model: 'grok-4.6' },+ pluginWorkloadSdk: { promptBridge: {} },+ },+ })+ })+const grokGrantBody = {...validGrantBody,provider: 'grok-subscription',@@ -886,37 +912,47 @@ describe('routes/admin/pluginWorkloadSdk — grants', () => {})it.each([- ['spec.agent', { agent: { provider: 'codex-subscription', model: 'gpt-5.3-codex' } }],[- 'a step agent',+ 'spec.agent is Codex',+ { agent: { provider: 'codex-subscription', model: 'gpt-5.3-codex' } },+ ],+ [+ 'a step agent is Codex',{agent: { provider: 'openai', model: 'gpt-5.1' },steps: [{ id: 's', agent: { provider: 'codex-subscription', model: 'gpt-5.3-codex' } }],},],- ])(- 'rejects a Grok target when %s is Codex (oauth_broker_provider_conflict)',- async (_label, spec) => {- vi.mocked(isGrokAssignmentAllowed).mockResolvedValue(true)- getResource.mockResolvedValue({- metadata: {- annotations: {- 'clerum.io/codex-connection-ref': 'team-plus',- 'clerum.io/subscription-connection-ref': 'team-plus',- },+ // RP-009: a static-agent recipe has no live oauth-broker target, so a Grok+ // SDK grant could publish but never authorize. Fail closed at publish.+ [+ 'the only agent is static (openai)',+ {+ agent: { provider: 'openai', model: 'gpt-5.1' },+ pluginWorkloadSdk: { promptBridge: {} },+ },+ ],+ ['the recipe declares no agent at all', { pluginWorkloadSdk: { promptBridge: {} } }],+ ])('rejects a Grok target when %s (oauth_broker_provider_conflict)', async (_label, spec) => {+ vi.mocked(isGrokAssignmentAllowed).mockResolvedValue(true)+ getResource.mockResolvedValue({+ metadata: {+ annotations: {+ 'clerum.io/codex-connection-ref': 'team-plus',+ 'clerum.io/subscription-connection-ref': 'team-plus',},- spec,- })- const res = await request(buildApp())- .post('/admin/plugin-workload-sdk/grants')- .send(grokGrantBody)- expect(res.status).toBe(400)- expect(res.body).toEqual({ error: 'oauth_broker_provider_conflict' })- expect(publishRecipeGrantIdentity).not.toHaveBeenCalled()- expect(updateResource).not.toHaveBeenCalled()- expect(sdkDb.upsertGrant).not.toHaveBeenCalled()- }- )+ },+ spec,+ })+ const res = await request(buildApp())+ .post('/admin/plugin-workload-sdk/grants')+ .send(grokGrantBody)+ expect(res.status).toBe(400)+ expect(res.body).toEqual({ error: 'oauth_broker_provider_conflict' })+ expect(publishRecipeGrantIdentity).not.toHaveBeenCalled()+ expect(updateResource).not.toHaveBeenCalled()+ expect(sdkDb.upsertGrant).not.toHaveBeenCalled()+ })it('accepts a Grok target on a Grok-agent recipe', async () => {vi.mocked(sdkDb.upsertGrant).mockResolvedValue({ id: 'g-grok' } as never)diff --git a/control-api/test/services.recipeCodexGrantIdentity.test.ts b/control-api/test/services.recipeCodexGrantIdentity.test.tsindex 3392549dc..190fe66fd 100644--- a/control-api/test/services.recipeCodexGrantIdentity.test.ts+++ b/control-api/test/services.recipeCodexGrantIdentity.test.ts@@ -149,7 +149,7 @@ describe('recipe Codex grant identity', () => {resourceVersion: '3',annotations: {},},- spec: {},+ spec: { agent: { provider: 'codex-subscription' } },})updateResource.mockRejectedValueOnce(new K8sConflictError('resource changed'))await expect(@@ -253,7 +253,9 @@ describe('recipe Codex grant identity', () => {expect(updateResource).not.toHaveBeenCalled()})- it('publishes a Grok identity on an SDK-only recipe with a static agent', async () => {+ it('refuses (409) to publish a named Grok identity on a recipe whose only agent is static', async () => {+ // RP-009: authorize attests live oauth-broker targets from agent/steps only,+ // so a static-agent recipe can never spend a published Grok key.getResource.mockResolvedValue({metadata: { resourceVersion: '7', annotations: {} },spec: { agent: { provider: 'openai' }, pluginWorkloadSdk: { family: 'promptBridge' } },@@ -266,7 +268,30 @@ describe('recipe Codex grant identity', () => {next: 'team-grok',provider: 'grok-subscription',})- ).resolves.toMatchObject({ published: 'team-grok', noop: false })+ ).rejects.toMatchObject({ status: 409, error: 'oauth_broker_provider_conflict' })+ expect(updateResource).not.toHaveBeenCalled()+ })++ it('still clears (unassigns) a stale broker identity on a static-agent recipe', async () => {+ getResource.mockResolvedValue({+ metadata: {+ resourceVersion: '7',+ annotations: {+ 'clerum.io/codex-connection-ref': '',+ 'clerum.io/subscription-connection-ref': 'team-grok',+ },+ },+ spec: { agent: { provider: 'openai' }, pluginWorkloadSdk: { family: 'promptBridge' } },+ })+ await expect(+ publishRecipeGrantIdentity({+ gateway: { getResource, updateResource },+ namespace: 'sandbox-recipes',+ name: 'sdk-recipe',+ next: 'unassigned',+ provider: 'grok-subscription',+ })+ ).resolves.toMatchObject({ noop: false })expect(updateResource).toHaveBeenCalledTimes(1)})