Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions apps/sim/lib/core/security/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ export function getWorkflowExecutionCSPPolicy(): string {
return "default-src * 'unsafe-inline' 'unsafe-eval'; connect-src *;"
}

/**
* CSP for embeddable chat pages
* Allows embedding in iframes from any origin while maintaining other security policies
*/
export function getChatEmbedCSPPolicy(): string {
const basePolicy = buildCSPString({
...buildTimeCSPDirectives,
'frame-ancestors': ['*'],
})
return basePolicy
}
Comment thread
waleedlatif1 marked this conversation as resolved.
Outdated

/**
* CSP for embeddable form pages
* Allows embedding in iframes from any origin while maintaining other security policies
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' },
],
},
Comment thread
waleedlatif1 marked this conversation as resolved.
// 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