44"fmt"
55"net/http"
66"strings"
7+
8+ "github.com/coder/coder/v2/codersdk"
79)
810
911// cspDirectives is a map of all csp fetch directives to their values.
@@ -37,6 +39,7 @@ const (
3739CSPDirectiveFormAction CSPFetchDirective = "form-action"
3840CSPDirectiveMediaSrc CSPFetchDirective = "media-src"
3941CSPFrameAncestors CSPFetchDirective = "frame-ancestors"
42+ CSPFrameSource CSPFetchDirective = "frame-src"
4043CSPDirectiveWorkerSrc CSPFetchDirective = "worker-src"
4144)
4245
@@ -55,7 +58,7 @@ const (
5558// Example: https://github.com/coder/coder/issues/15118
5659//
5760//nolint:revive
58- func CSPHeaders (telemetry bool ,websocketHosts func () []string ,staticAdditions map [CSPFetchDirective ][]string )func (next http.Handler ) http.Handler {
61+ func CSPHeaders (experiments codersdk. Experiments , telemetry bool ,websocketHosts func () []string ,staticAdditions map [CSPFetchDirective ][]string )func (next http.Handler ) http.Handler {
5962return func (next http.Handler ) http.Handler {
6063return http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) {
6164// Content-Security-Policy disables loading certain content types and can prevent XSS injections.
@@ -88,13 +91,21 @@ func CSPHeaders(telemetry bool, websocketHosts func() []string, staticAdditions
8891CSPDirectiveMediaSrc : {"'self'" },
8992// Report all violations back to the server to log
9093CSPDirectiveReportURI : {"/api/v2/csp/reports" },
91- CSPFrameAncestors : {"'none'" },
9294
9395// Only scripts can manipulate the dom. This prevents someone from
9496// naming themselves something like '<svg onload="alert(/cross-site-scripting/)" />'.
9597// "require-trusted-types-for" : []string{"'script'"},
9698}
9799
100+ if experiments .Enabled (codersdk .ExperimentAITasks ) {
101+ // AI tasks use iframe embeds of local apps.
102+ // TODO: Handle region domains too, not just path based apps
103+ cspSrcs .Append (CSPFrameAncestors ,`'self'` )
104+ cspSrcs .Append (CSPFrameSource ,`'self'` )
105+ }else {
106+ cspSrcs .Append (CSPFrameAncestors ,`'none'` )
107+ }
108+
98109if telemetry {
99110// If telemetry is enabled, we report to coder.com.
100111cspSrcs .Append (CSPDirectiveConnectSrc ,"https://coder.com" )