Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-api): hide Slack targets whose Secret has no signing secret
Jose Ramirez3cea4d0c391f
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 31b8ca7a5..9b40bbd85 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.436",+ "version": "0.1.437","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.436",+ "version": "0.1.437","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 c73ab25e9..513de4d55 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.436",+ "version": "0.1.437","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/workflowApprovalMediumSlackVerificationService.ts b/control-api/src/services/workflowApprovalMediumSlackVerificationService.tsindex 6a3a94af8..d69add4a7 100644--- a/control-api/src/services/workflowApprovalMediumSlackVerificationService.ts+++ b/control-api/src/services/workflowApprovalMediumSlackVerificationService.ts@@ -1,6 +1,7 @@import type { K8sGateway } from '../k8s.js'import { decodeSlackTargetId, encodeSlackTargetId } from '../utils/slackTargetId.js'import { getTeamAgents, getUserAgents, listTeams } from './directory/index.js'+import { secretKeyNames } from './secretKeyNames.js'import type { VerifiedMediumAccount } from './workflowApprovalMediumIdentityService.js'type CommunicationChannelResource = {@@ -200,12 +201,44 @@ export async function listSlackApprovalTargets(params: {'communicationchannels','*')) as CommunicationChannelResource[]- const items = channels- .flatMap(channel => {- const target = projectTarget(channel)- if (!target || !userCanAccessChannel(channel, params.userId, access)) return []- return [target]+ // Two phases, and the order is a security property rather than a style+ // choice. This lists communicationchannels across ALL namespaces and the+ // route is gated only by a valid external session, so reading Secrets before+ // the access check would let any authenticated profile user drive one request+ // into reading the credentials Secret of every channel in the cluster. Project+ // and access-check first; read keys only for what survived, which bounds the+ // reads to the caller's own channels.+ const candidates = channels.flatMap(channel => {+ const target = projectTarget(channel)+ if (!target || !userCanAccessChannel(channel, params.userId, access)) return []+ return [{ channel, target }]+ })++ const keysBySecret = new Map<string, Promise<string[]>>()+ const readable = await Promise.all(+ candidates.map(async ({ channel, target }) => {+ const secretName = optionalString(channel.spec?.credentialsSecretRef?.name)+ if (!secretName) return null+ const cacheKey = `${target.channelNamespace}/${secretName}`+ if (!keysBySecret.has(cacheKey)) {+ // Fails OPEN, unlike the write-path validator, and deliberately so: this+ // is a read-only listing, and one channel with a broken or unreadable+ // Secret must not blank the user's entire verification picker.+ keysBySecret.set(+ cacheKey,+ secretKeyNames(params.gateway, secretName, target.channelNamespace).catch(() => [])+ )+ }+ const keys = await keysBySecret.get(cacheKey)!+ // A channel can carry a Slack App Name while its Secret only ever held+ // another provider's credentials. It projects as ready and then 409s on+ // use, so it must not be offered as a target at all.+ return keys.includes('slack-signing-secret') ? target : null})+ )++ const items = readable+ .filter((target): target is SlackApprovalTarget => target !== null).sort((a, b) =>a.agentName.localeCompare(b.agentName) ||diff --git a/control-api/test/services.slackTargetFilter.test.ts b/control-api/test/services.slackTargetFilter.test.tsnew file mode 100644index 000000000..cc77f0f5d--- /dev/null+++ b/control-api/test/services.slackTargetFilter.test.ts@@ -0,0 +1,171 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import { getTeamAgents, getUserAgents, listTeams } from '../src/services/directory/index.js'+import { secretKeyNames } from '../src/services/secretKeyNames.js'+import { listSlackApprovalTargets } from '../src/services/workflowApprovalMediumSlackVerificationService.js'+import { MockGateway } from './mockGateway.js'++vi.mock('../src/services/directory/index.js', () => ({+ getTeamAgents: vi.fn(),+ getUserAgents: vi.fn(),+ listTeams: vi.fn(),+}))++vi.mock('../src/services/secretKeyNames.js', () => ({+ secretKeyNames: vi.fn(),+}))++const mockedGetUserAgents = vi.mocked(getUserAgents)+const mockedGetTeamAgents = vi.mocked(getTeamAgents)+const mockedListTeams = vi.mocked(listTeams)+const mockedSecretKeyNames = vi.mocked(secretKeyNames)++/**+ * Keys the fake cluster holds per `namespace/secretName`. The mocked+ * `secretKeyNames` answers from this map, so a test declares Secret contents+ * the same way it declares channels.+ */+const secretKeys = new Map<string, string[]>()++function seedSecretKeys(namespace: string, name: string, keys: string[]): void {+ secretKeys.set(`${namespace}/${name}`, keys)+}++async function seedSlackChannel(+ gateway: MockGateway,+ options: {+ name: string+ namespace?: string+ hostRef?: string+ users?: string[]+ secretName?: string+ }+): Promise<void> {+ const namespace = options.namespace ?? 'channels'+ await gateway.createResource(+ 'communicationchannels',+ {+ metadata: { name: options.name },+ spec: {+ hostRef: options.hostRef ?? 'agent-a',+ access: { users: options.users ?? ['user-1'], teams: [] },+ credentialsSecretRef: { name: options.secretName ?? `${options.name}-credentials` },+ slackSettings: { botHandle: 'Evenfire-jos' },+ },+ },+ namespace+ )+}++/** Every `namespace/secretName` the implementation actually read. */+function readSecretRefs(): string[] {+ return mockedSecretKeyNames.mock.calls.map(([, name, namespace]) => `${namespace}/${name}`)+}++describe('listSlackApprovalTargets Secret filtering', () => {+ beforeEach(() => {+ vi.clearAllMocks()+ secretKeys.clear()+ mockedGetUserAgents.mockResolvedValue({ userId: 'user-1', agentNames: ['agent-a'] })+ mockedGetTeamAgents.mockResolvedValue({ teamId: 'team-1', agentNames: [] })+ mockedListTeams.mockResolvedValue({ currentTeamId: '', items: [] })+ mockedSecretKeyNames.mockImplementation(async (_gateway, name, namespace) => {+ const keys = secretKeys.get(`${namespace}/${name}`)+ if (!keys) {+ const error = new Error(`secrets "${name}" unreadable`) as Error & { statusCode: number }+ error.statusCode = 403+ throw error+ }+ return [...keys].sort((a, b) => a.localeCompare(b))+ })+ })++ it('lists a channel whose Secret holds a Slack signing secret', async () => {+ const gateway = new MockGateway('channels')+ await seedSlackChannel(gateway, { name: 'agent-a-slack' })+ seedSecretKeys('channels', 'agent-a-slack-credentials', [+ 'slack-bot-token',+ 'slack-signing-secret',+ ])++ const targets = await listSlackApprovalTargets({ gateway: gateway as never, userId: 'user-1' })++ expect(targets.items).toHaveLength(1)+ expect(targets.items[0]).toMatchObject({+ agentName: 'agent-a',+ channelName: 'agent-a-slack',+ channelNamespace: 'channels',+ medium: 'slack',+ })+ })++ it('hides a channel whose Secret holds no Slack keys even though it names a Slack app', async () => {+ // The reported duplicate "Evenfire-jos": a Telegram channel that had a Slack+ // App Name typed into it. It projects as a ready Slack target, but its+ // Secret only ever held Telegram credentials, so picking it 409s.+ const gateway = new MockGateway('channels')+ await seedSlackChannel(gateway, { name: 'agent-a-slack' })+ await seedSlackChannel(gateway, { name: 'agent-a-impostor' })+ seedSecretKeys('channels', 'agent-a-slack-credentials', ['slack-signing-secret'])+ seedSecretKeys('channels', 'agent-a-impostor-credentials', ['telegram-bot-token'])++ const targets = await listSlackApprovalTargets({ gateway: gateway as never, userId: 'user-1' })++ expect(targets.items.map(target => target.channelName)).toEqual(['agent-a-slack'])+ })++ it('never reads the Secret of a channel the caller cannot access', async () => {+ // The ordering property. This endpoint lists communicationchannels across+ // ALL namespaces and is gated only by a valid external session, so reading+ // keys before the access check would let any authenticated profile user+ // drive control-api into reading every credentials Secret in the cluster.+ // Asserting only on `items` would not catch that: the foreign channel is+ // absent from the result either way.+ const gateway = new MockGateway('channels')+ await seedSlackChannel(gateway, { name: 'agent-a-slack' })+ await seedSlackChannel(gateway, {+ name: 'agent-b-slack',+ namespace: 'other-tenant',+ hostRef: 'agent-b',+ users: ['user-2'],+ })+ seedSecretKeys('channels', 'agent-a-slack-credentials', ['slack-signing-secret'])+ seedSecretKeys('other-tenant', 'agent-b-slack-credentials', ['slack-signing-secret'])++ const targets = await listSlackApprovalTargets({ gateway: gateway as never, userId: 'user-1' })++ expect(targets.items.map(target => target.channelName)).toEqual(['agent-a-slack'])+ expect(mockedSecretKeyNames).not.toHaveBeenCalledWith(+ expect.anything(),+ 'agent-b-slack-credentials',+ expect.anything()+ )+ expect(readSecretRefs()).toEqual(['channels/agent-a-slack-credentials'])+ })++ it('reads a shared Secret once when two accessible channels reference it', async () => {+ const gateway = new MockGateway('channels')+ await seedSlackChannel(gateway, { name: 'agent-a-slack-one', secretName: 'shared-credentials' })+ await seedSlackChannel(gateway, { name: 'agent-a-slack-two', secretName: 'shared-credentials' })+ seedSecretKeys('channels', 'shared-credentials', ['slack-signing-secret'])++ const targets = await listSlackApprovalTargets({ gateway: gateway as never, userId: 'user-1' })++ expect(targets.items).toHaveLength(2)+ expect(readSecretRefs()).toEqual(['channels/shared-credentials'])+ })++ it('drops only the offending target when a Secret cannot be read', async () => {+ // Fails OPEN, deliberately, and unlike the write-path validator: this is a+ // read-only listing, and one broken channel must not blank the whole picker.+ const gateway = new MockGateway('channels')+ await seedSlackChannel(gateway, { name: 'agent-a-slack' })+ await seedSlackChannel(gateway, { name: 'agent-a-unreadable' })+ seedSecretKeys('channels', 'agent-a-slack-credentials', ['slack-signing-secret'])+ // agent-a-unreadable-credentials is intentionally unseeded: the mock throws+ // a 403 for it, the same shape secretKeyNames rethrows on an RBAC denial.++ const targets = await listSlackApprovalTargets({ gateway: gateway as never, userId: 'user-1' })++ expect(targets.items.map(target => target.channelName)).toEqual(['agent-a-slack'])+ })+})