Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions apps/sim/lib/core/security/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,28 @@ export function getWorkflowExecutionCSPPolicy(): string {
}

/**
* CSP for embeddable form pages
* Shared CSP for embeddable pages (chat, forms)
* Allows embedding in iframes from any origin while maintaining other security policies
*/
export function getFormEmbedCSPPolicy(): string {
const basePolicy = buildCSPString({
function getEmbedCSPPolicy(): string {
return buildCSPString({
...buildTimeCSPDirectives,
'frame-ancestors': ['*'],
})
return basePolicy
}

/**
* CSP for embeddable chat pages
*/
export function getChatEmbedCSPPolicy(): string {
return getEmbedCSPPolicy()
}

/**
* CSP for embeddable form pages
*/
export function getFormEmbedCSPPolicy(): string {
return getEmbedCSPPolicy()
}

/**
Expand Down
25 changes: 22 additions & 3 deletions apps/sim/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NextConfig } from 'next'
import { env, getEnv, isTruthy } from './lib/core/config/env'
import { isDev } from './lib/core/config/feature-flags'
import {
getChatEmbedCSPPolicy,
getFormEmbedCSPPolicy,
getMainCSPPolicy,
getWorkflowExecutionCSPPolicy,
Expand Down Expand Up @@ -255,6 +256,24 @@ const nextConfig: NextConfig = {
},
],
},
// Chat pages - allow iframe embedding from any origin
{
source: '/chat/:path*',
headers: [
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
// No X-Frame-Options to allow iframe embedding
{
key: 'Content-Security-Policy',
value: getChatEmbedCSPPolicy(),
},
// Permissive CORS for chat requests from embedded chats
{ key: 'Cross-Origin-Embedder-Policy', value: 'unsafe-none' },
{ key: 'Cross-Origin-Opener-Policy', value: 'unsafe-none' },
],
},
// Form pages - allow iframe embedding from any origin
{
source: '/form/:path*',
Expand Down Expand Up @@ -284,10 +303,10 @@ const nextConfig: NextConfig = {
],
},
// Apply security headers to routes not handled by middleware runtime CSP
// Middleware handles: /, /workspace/*, /chat/*
// Exclude form routes which have their own permissive headers
// Middleware handles: /, /workspace/*
// Exclude chat and form routes which have their own permissive embed headers
{
source: '/((?!workspace|chat$|form).*)',
source: '/((?!workspace|chat|form).*)',
headers: [
{
key: 'X-Content-Type-Options',
Expand Down
7 changes: 2 additions & 5 deletions apps/sim/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export async function proxy(request: NextRequest) {
return response
}

// Chat pages are publicly accessible embeds — CSP is set in next.config.ts headers
if (url.pathname.startsWith('/chat/')) {
return NextResponse.next()
}
Expand Down Expand Up @@ -188,11 +189,7 @@ export async function proxy(request: NextRequest) {
const response = NextResponse.next()
response.headers.set('Vary', 'User-Agent')

if (
url.pathname.startsWith('/workspace') ||
url.pathname.startsWith('/chat') ||
url.pathname === '/'
) {
if (url.pathname.startsWith('/workspace') || url.pathname === '/') {
response.headers.set('Content-Security-Policy', generateRuntimeCSP())
}

Expand Down
Loading