Skip to content

Commit 070f8d0

Browse files
fix(workflows): scope deployment api-key auth
1 parent 6282fc2 commit 070f8d0

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

apps/sim/app/api/workflows/middleware.test.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,8 @@ describe('validateWorkflowAccess', () => {
293293
},
294294
})
295295
expect(mockCheckHybridAuth).not.toHaveBeenCalled()
296-
expect(mockAuthenticateApiKeyFromHeader).toHaveBeenNthCalledWith(1, 'valid-key', {
297-
keyTypes: ['workspace', 'personal'],
298-
})
296+
expect(mockGetActiveWorkflowRecord).toHaveBeenCalledWith(WORKFLOW_ID)
297+
expect(mockAuthenticateApiKeyFromHeader).not.toHaveBeenCalled()
299298
})
300299

301300
it('returns 401 before deployed workflow lookup when api key is missing', async () => {
@@ -334,11 +333,12 @@ describe('validateWorkflowAccess', () => {
334333
status: 401,
335334
},
336335
})
337-
expect(mockGetActiveWorkflowRecord).not.toHaveBeenCalled()
338-
expect(mockGetWorkflowById).not.toHaveBeenCalled()
336+
expect(mockGetActiveWorkflowRecord).toHaveBeenCalledWith(WORKFLOW_ID)
339337
expect(mockAuthenticateApiKeyFromHeader).toHaveBeenCalledWith('invalid-key', {
338+
workspaceId: WORKSPACE_ID,
340339
keyTypes: ['workspace', 'personal'],
341340
})
341+
expect(mockAuthenticateApiKeyFromHeader).toHaveBeenCalledTimes(1)
342342
})
343343

344344
it('returns 403 for deployed access when authenticated workflow has no workspace', async () => {
@@ -368,9 +368,7 @@ describe('validateWorkflowAccess', () => {
368368
},
369369
})
370370
expect(mockCheckHybridAuth).not.toHaveBeenCalled()
371-
expect(mockAuthenticateApiKeyFromHeader).toHaveBeenNthCalledWith(1, 'valid-key', {
372-
keyTypes: ['workspace', 'personal'],
373-
})
371+
expect(mockAuthenticateApiKeyFromHeader).not.toHaveBeenCalled()
374372
})
375373

376374
it('returns 404 for deployed access when authenticated workflow workspace is archived', async () => {
@@ -400,9 +398,7 @@ describe('validateWorkflowAccess', () => {
400398
})
401399
expect(mockGetWorkflowById).toHaveBeenCalledWith(WORKFLOW_ID)
402400
expect(mockCheckHybridAuth).not.toHaveBeenCalled()
403-
expect(mockAuthenticateApiKeyFromHeader).toHaveBeenNthCalledWith(1, 'valid-key', {
404-
keyTypes: ['workspace', 'personal'],
405-
})
401+
expect(mockAuthenticateApiKeyFromHeader).not.toHaveBeenCalled()
406402
})
407403

408404
it('returns 403 for deployed access when authenticated workflow is not deployed', async () => {
@@ -430,8 +426,10 @@ describe('validateWorkflowAccess', () => {
430426
},
431427
})
432428
expect(mockAuthenticateApiKeyFromHeader).toHaveBeenCalledWith('valid-key', {
429+
workspaceId: WORKSPACE_ID,
433430
keyTypes: ['workspace', 'personal'],
434431
})
432+
expect(mockAuthenticateApiKeyFromHeader).toHaveBeenCalledTimes(1)
435433
expect(mockUpdateApiKeyLastUsed).not.toHaveBeenCalled()
436434
})
437435
})

apps/sim/app/api/workflows/middleware.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,6 @@ export async function validateWorkflowAccess(
141141
},
142142
}
143143
}
144-
145-
const preflightResult = await authenticateApiKeyFromHeader(apiKeyHeader, {
146-
keyTypes: ['workspace', 'personal'],
147-
})
148-
149-
if (!preflightResult.success) {
150-
return {
151-
error: {
152-
message: 'Unauthorized: Invalid API key',
153-
status: 401,
154-
},
155-
}
156-
}
157144
}
158145

159146
const workflowResult = await getValidatedWorkflow(workflowId)
@@ -162,16 +149,16 @@ export async function validateWorkflowAccess(
162149
}
163150
const workflow = workflowResult.workflow
164151

165-
if (!workflow.isDeployed) {
166-
return {
167-
error: {
168-
message: 'Workflow is not deployed',
169-
status: 403,
170-
},
152+
if (hasValidInternalSecret) {
153+
if (!workflow.isDeployed) {
154+
return {
155+
error: {
156+
message: 'Workflow is not deployed',
157+
status: 403,
158+
},
159+
}
171160
}
172-
}
173161

174-
if (hasValidInternalSecret) {
175162
return { workflow }
176163
}
177164

@@ -195,6 +182,15 @@ export async function validateWorkflowAccess(
195182
}
196183
}
197184

185+
if (!workflow.isDeployed) {
186+
return {
187+
error: {
188+
message: 'Workflow is not deployed',
189+
status: 403,
190+
},
191+
}
192+
}
193+
198194
if (validResult.keyId) {
199195
await updateApiKeyLastUsed(validResult.keyId)
200196
}

0 commit comments

Comments
 (0)