mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-28 03:00:29 -04:00
* feat(audit-log): add persistent audit log system with comprehensive route instrumentation
* fix(audit-log): address PR review — nullable workspaceId, enum usage, remove redundant queries
- Make audit_log.workspace_id nullable with ON DELETE SET NULL (logs survive workspace/user deletion)
- Make audit_log.actor_id nullable with ON DELETE SET NULL
- Replace all 53 routes' string literal action/resourceType with AuditAction.X and AuditResourceType.X enums
- Fix empty workspaceId ('') → null for OAuth, form, and org routes to avoid FK violations
- Remove redundant DB queries in chat manage route (use checkChatAccess return data)
- Fix organization routes to pass workspaceId: null instead of organizationId
* fix(audit-log): replace remaining workspaceId '' fallbacks with null
* fix(audit-log): credential-set org IDs, workspace deletion FK, actorId fallback, string literal action
* reran migrations
* fix(mcp,audit): tighten env var domain bypass, add post-resolution check, form workspaceId
- Only bypass MCP domain check when env var is in hostname/authority, not path/query
- Add post-resolution validateMcpDomain call in test-connection endpoint
- Match client-side isDomainAllowed to same hostname-only bypass logic
- Return workspaceId from checkFormAccess, use in form audit logs
- Add 49 comprehensive domain-check tests covering all edge cases
* fix(mcp): stateful regex lastIndex bug, RFC 3986 authority parsing
- Remove /g flag from module-level ENV_VAR_PATTERN to avoid lastIndex state
- Create fresh regex instances per call in server-side hasEnvVarInHostname
- Fix authority extraction to terminate at /, ?, or # per RFC 3986
- Prevents bypass via https://evil.com?token={{SECRET}} (no path)
- Add test cases for query-only and fragment-only env var URLs (53 total)
* fix(audit-log): try/catch for never-throw contract, accept null actorName/Email, fix misleading action
- Wrap recordAudit body in try/catch so nanoid() or header extraction can't throw
- Accept string | null for actorName and actorEmail (session.user.name can be null)
- Normalize null -> undefined before insert to match DB column types
- Fix org members route: ORG_MEMBER_ADDED -> ORG_INVITATION_CREATED (sends invite, not adds member)
* improvement(audit-log): add resource names and specific invitation actions
* fix(audit-log): use validated chat record, add mock sync tests
94 lines
3.1 KiB
TypeScript
94 lines
3.1 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log'
|
|
import { getSession } from '@/lib/auth'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { duplicateWorkspace } from '@/lib/workspaces/duplicate'
|
|
|
|
const logger = createLogger('WorkspaceDuplicateAPI')
|
|
|
|
const DuplicateRequestSchema = z.object({
|
|
name: z.string().min(1, 'Name is required'),
|
|
})
|
|
|
|
// POST /api/workspaces/[id]/duplicate - Duplicate a workspace with all its workflows
|
|
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
|
|
const { id: sourceWorkspaceId } = await params
|
|
const requestId = generateRequestId()
|
|
const startTime = Date.now()
|
|
|
|
const session = await getSession()
|
|
if (!session?.user?.id) {
|
|
logger.warn(
|
|
`[${requestId}] Unauthorized workspace duplication attempt for ${sourceWorkspaceId}`
|
|
)
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
try {
|
|
const body = await req.json()
|
|
const { name } = DuplicateRequestSchema.parse(body)
|
|
|
|
logger.info(
|
|
`[${requestId}] Duplicating workspace ${sourceWorkspaceId} for user ${session.user.id}`
|
|
)
|
|
|
|
const result = await duplicateWorkspace({
|
|
sourceWorkspaceId,
|
|
userId: session.user.id,
|
|
name,
|
|
requestId,
|
|
})
|
|
|
|
const elapsed = Date.now() - startTime
|
|
logger.info(
|
|
`[${requestId}] Successfully duplicated workspace ${sourceWorkspaceId} to ${result.id} in ${elapsed}ms`
|
|
)
|
|
|
|
recordAudit({
|
|
workspaceId: sourceWorkspaceId,
|
|
actorId: session.user.id,
|
|
actorName: session.user.name,
|
|
actorEmail: session.user.email,
|
|
action: AuditAction.WORKSPACE_DUPLICATED,
|
|
resourceType: AuditResourceType.WORKSPACE,
|
|
resourceId: result.id,
|
|
resourceName: name,
|
|
description: `Duplicated workspace to "${name}"`,
|
|
request: req,
|
|
})
|
|
|
|
return NextResponse.json(result, { status: 201 })
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
if (error.message === 'Source workspace not found') {
|
|
logger.warn(`[${requestId}] Source workspace ${sourceWorkspaceId} not found`)
|
|
return NextResponse.json({ error: 'Source workspace not found' }, { status: 404 })
|
|
}
|
|
|
|
if (error.message === 'Source workspace not found or access denied') {
|
|
logger.warn(
|
|
`[${requestId}] User ${session.user.id} denied access to source workspace ${sourceWorkspaceId}`
|
|
)
|
|
return NextResponse.json({ error: 'Access denied' }, { status: 403 })
|
|
}
|
|
}
|
|
|
|
if (error instanceof z.ZodError) {
|
|
logger.warn(`[${requestId}] Invalid duplication request data`, { errors: error.errors })
|
|
return NextResponse.json(
|
|
{ error: 'Invalid request data', details: error.errors },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const elapsed = Date.now() - startTime
|
|
logger.error(
|
|
`[${requestId}] Error duplicating workspace ${sourceWorkspaceId} after ${elapsed}ms:`,
|
|
error
|
|
)
|
|
return NextResponse.json({ error: 'Failed to duplicate workspace' }, { status: 500 })
|
|
}
|
|
}
|