4
4
"fmt"
5
5
"net/http"
6
6
"strings"
7
+
8
+ "github.com/coder/coder/v2/codersdk"
7
9
)
8
10
9
11
// cspDirectives is a map of all csp fetch directives to their values.
@@ -37,6 +39,7 @@ const (
37
39
CSPDirectiveFormAction CSPFetchDirective = "form-action"
38
40
CSPDirectiveMediaSrc CSPFetchDirective = "media-src"
39
41
CSPFrameAncestors CSPFetchDirective = "frame-ancestors"
42
+ CSPFrameSource CSPFetchDirective = "frame-src"
40
43
CSPDirectiveWorkerSrc CSPFetchDirective = "worker-src"
41
44
)
42
45
@@ -55,7 +58,7 @@ const (
55
58
// Example: https://github.com/coder/coder/issues/15118
56
59
//
57
60
//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 {
59
62
return func (next http.Handler ) http.Handler {
60
63
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
61
64
// 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
88
91
CSPDirectiveMediaSrc : {"'self'" },
89
92
// Report all violations back to the server to log
90
93
CSPDirectiveReportURI : {"/api/v2/csp/reports" },
91
- CSPFrameAncestors : {"'none'" },
92
94
93
95
// Only scripts can manipulate the dom. This prevents someone from
94
96
// naming themselves something like '<svg onload="alert(/cross-site-scripting/)" />'.
95
97
// "require-trusted-types-for" : []string{"'script'"},
96
98
}
97
99
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
+
98
109
if telemetry {
99
110
// If telemetry is enabled, we report to coder.com.
100
111
cspSrcs .Append (CSPDirectiveConnectSrc , "https://coder.com" )
0 commit comments