d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { authMockFns, dbChainMock, dbChainMockFns, resetDbChainMock } from '@sim/testing'
|
|
import { NextRequest } from 'next/server'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
// Override global db mock with the configurable chain mock
|
|
vi.mock('@sim/db', () => dbChainMock)
|
|
|
|
const { mockValidateWorkflowAccess, mockGetWorkspaceBilledAccountUserId } = vi.hoisted(() => ({
|
|
mockValidateWorkflowAccess: vi.fn(),
|
|
mockGetWorkspaceBilledAccountUserId: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/app/api/workflows/middleware', () => ({
|
|
validateWorkflowAccess: mockValidateWorkflowAccess,
|
|
}))
|
|
|
|
vi.mock('@/lib/workspaces/utils', () => ({
|
|
getWorkspaceBilledAccountUserId: mockGetWorkspaceBilledAccountUserId,
|
|
}))
|
|
|
|
vi.mock('@/lib/logs/execution/logging-session', () => ({
|
|
LoggingSession: vi.fn().mockImplementation(() => ({
|
|
start: vi.fn().mockResolvedValue(undefined),
|
|
markAsFailed: vi.fn().mockResolvedValue(undefined),
|
|
safeCompleteWithError: vi.fn().mockResolvedValue(undefined),
|
|
safeComplete: vi.fn().mockResolvedValue(undefined),
|
|
})),
|
|
}))
|
|
|
|
vi.mock('@/lib/logs/execution/trace-spans/trace-spans', () => ({
|
|
buildTraceSpans: vi.fn().mockReturnValue([]),
|
|
}))
|
|
|
|
import { POST } from './route'
|
|
|
|
const makeRequest = (workflowId: string, body: unknown) =>
|
|
new NextRequest(`http://localhost/api/workflows/${workflowId}/log`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
const validResult = { success: true, output: { value: 42 } }
|
|
|
|
describe('POST /api/workflows/[id]/log cross-tenant guard', () => {
|
|
const OWNER_WORKFLOW_ID = 'wf-owner'
|
|
const ATTACKER_WORKFLOW_ID = 'wf-attacker'
|
|
const VICTIM_EXECUTION_ID = 'exec-victim-uuid'
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetDbChainMock()
|
|
authMockFns.mockGetSession.mockResolvedValue({ user: { id: 'user-1' } })
|
|
mockValidateWorkflowAccess.mockResolvedValue({ error: null })
|
|
mockGetWorkspaceBilledAccountUserId.mockResolvedValue('user-1')
|
|
// Default: no existing log (fresh execution)
|
|
dbChainMockFns.limit.mockResolvedValue([])
|
|
})
|
|
|
|
it('returns 404 when executionId belongs to a different workflow', async () => {
|
|
dbChainMockFns.limit.mockResolvedValueOnce([{ workflowId: OWNER_WORKFLOW_ID }])
|
|
|
|
const res = await POST(
|
|
makeRequest(ATTACKER_WORKFLOW_ID, {
|
|
executionId: VICTIM_EXECUTION_ID,
|
|
result: validResult,
|
|
}),
|
|
{ params: Promise.resolve({ id: ATTACKER_WORKFLOW_ID }) }
|
|
)
|
|
|
|
expect(res.status).toBe(404)
|
|
const body = await res.json()
|
|
expect(body.error).toBe('Execution not found')
|
|
})
|
|
|
|
it('proceeds when executionId belongs to the same workflow', async () => {
|
|
dbChainMockFns.limit.mockResolvedValueOnce([{ workflowId: OWNER_WORKFLOW_ID }])
|
|
|
|
const res = await POST(
|
|
makeRequest(OWNER_WORKFLOW_ID, {
|
|
executionId: VICTIM_EXECUTION_ID,
|
|
result: validResult,
|
|
}),
|
|
{ params: Promise.resolve({ id: OWNER_WORKFLOW_ID }) }
|
|
)
|
|
|
|
expect(res.status).not.toBe(404)
|
|
expect(res.status).not.toBe(403)
|
|
})
|
|
|
|
it('proceeds when executionId has no existing log row (fresh execution)', async () => {
|
|
dbChainMockFns.limit.mockResolvedValueOnce([])
|
|
|
|
const res = await POST(
|
|
makeRequest(OWNER_WORKFLOW_ID, {
|
|
executionId: 'brand-new-execution-id',
|
|
result: validResult,
|
|
}),
|
|
{ params: Promise.resolve({ id: OWNER_WORKFLOW_ID }) }
|
|
)
|
|
|
|
expect(res.status).not.toBe(404)
|
|
expect(res.status).not.toBe(403)
|
|
})
|
|
})
|