fix(chat): fail closed when embed gate cannot resolve workspace (#5046)

This commit is contained in:
Waleed
2026-06-14 18:53:05 -07:00
committed by GitHub
parent 7a33508af1
commit 3ada4a34af
2 changed files with 24 additions and 1 deletions
+16
View File
@@ -4,6 +4,8 @@
* @vitest-environment node
*/
import {
dbChainMock,
dbChainMockFns,
encryptionMock,
encryptionMockFns,
loggingSessionMock,
@@ -34,6 +36,8 @@ const {
flagState: { isBillingEnabled: false, isFreeApiDeploymentGateEnabled: true },
}))
vi.mock('@sim/db', () => dbChainMock)
vi.mock('@/lib/billing/core/api-access', () => ({
isWorkspaceApiExecutionEntitled: mockIsWorkspaceApiExecutionEntitled,
}))
@@ -480,6 +484,7 @@ describe('assertChatEmbedAllowed', () => {
flagState.isBillingEnabled = true
flagState.isFreeApiDeploymentGateEnabled = true
mockIsWorkspaceApiExecutionEntitled.mockResolvedValue(true)
dbChainMockFns.limit.mockResolvedValue([{ workspaceId: 'ws-1' }])
})
it('returns 403 for a cross-site origin when the owner is on the free plan', async () => {
@@ -501,6 +506,17 @@ describe('assertChatEmbedAllowed', () => {
expect(res).toBeNull()
})
it('returns 403 for a cross-site origin when the workflow has no active workspace', async () => {
dbChainMockFns.limit.mockResolvedValueOnce([])
const res = await assertChatEmbedAllowed(
chatRequest('https://evil.example.com'),
'wf-1',
'req-1'
)
expect(res?.status).toBe(403)
expect(mockIsWorkspaceApiExecutionEntitled).not.toHaveBeenCalled()
})
it('allows a first-party *.sim.ai origin without gating', async () => {
const res = await assertChatEmbedAllowed(chatRequest('https://chat.sim.ai'), 'wf-1', 'req-1')
expect(res).toBeNull()
+8 -1
View File
@@ -81,7 +81,14 @@ export async function assertChatEmbedAllowed(
.where(and(eq(workflow.id, workflowId), isNull(workflow.archivedAt)))
.limit(1)
if (!(await isWorkspaceApiExecutionEntitled(wf?.workspaceId ?? undefined))) {
if (!wf?.workspaceId) {
logger.warn(
`[${requestId}] Chat embed blocked: no active workspace for workflow ${workflowId}, origin=${origin}`
)
return createErrorResponse('This chat is currently unavailable', 403)
}
if (!(await isWorkspaceApiExecutionEntitled(wf.workspaceId))) {
logger.warn(`[${requestId}] Chat embed blocked: workspace on free plan, origin=${origin}`)
return createErrorResponse('Embedding this chat on external sites requires a paid plan', 403)
}