- Notifications
You must be signed in to change notification settings - Fork914
feat: add csp headers for embedded apps#18374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
code-asher wants to merge2 commits intomainChoose a base branch fromasher/frame-src
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+180 −57
Open
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
27 changes: 20 additions & 7 deletionscoderd/coderd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
41 changes: 20 additions & 21 deletionscoderd/httpmw/csp.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5,6 +5,7 @@ import ( | ||
"net/http" | ||
"strings" | ||
"github.com/coder/coder/v2/coderd/proxyhealth" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
@@ -47,18 +48,18 @@ const ( | ||
// for coderd. | ||
// | ||
// Arguments: | ||
// -proxyHosts: a function that returns a list of supportedproxyhosts | ||
//(including the primary).This is to support the terminal connecting to a | ||
//workspace proxy and for embedding apps in an iframe. The originof the | ||
//requests do not match the url of the proxy, so the CSP list of allowed | ||
//hosts must be dynamic and match the currentavailable proxy urls. | ||
// - staticAdditions: a map of CSP directives to append to the default CSP headers. | ||
// Used to allow specific static additions to the CSP headers. Allows some niche | ||
// use cases, such as embedding Coder in an iframe. | ||
// Example: https://github.com/coder/coder/issues/15118 | ||
// | ||
//nolint:revive | ||
func CSPHeaders(experiments codersdk.Experiments, telemetry bool,proxyHosts func() []*proxyhealth.ProxyHost, staticAdditions map[CSPFetchDirective][]string) func(next http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Content-Security-Policy disables loading certain content types and can prevent XSS injections. | ||
@@ -97,15 +98,6 @@ func CSPHeaders(experiments codersdk.Experiments, telemetry bool, websocketHosts | ||
// "require-trusted-types-for" : []string{"'script'"}, | ||
} | ||
if telemetry { | ||
// If telemetry is enabled, we report to coder.com. | ||
cspSrcs.Append(CSPDirectiveConnectSrc, "https://coder.com") | ||
@@ -126,19 +118,26 @@ func CSPHeaders(experiments codersdk.Experiments, telemetry bool, websocketHosts | ||
cspSrcs.Append(CSPDirectiveConnectSrc, fmt.Sprintf("wss://%[1]s ws://%[1]s", host)) | ||
} | ||
// The terminaland iframed apps can useworkspaceproxies (which includes | ||
//the primary).Make sure we allowconnections to healthy proxies. | ||
extraConnect :=proxyHosts() | ||
if len(extraConnect) > 0 { | ||
for _, extraHost := range extraConnect { | ||
// Allow embedding the app host. | ||
if experiments.Enabled(codersdk.ExperimentAITasks) { | ||
cspSrcs.Append(CSPDirectiveFrameSrc, extraHost.AppHost) | ||
} | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if extraHost.Host == "*" { | ||
// '*' means all | ||
cspSrcs.Append(CSPDirectiveConnectSrc, "*") | ||
continue | ||
} | ||
// Avoid double-adding r.Host. | ||
if extraHost.Host != r.Host { | ||
cspSrcs.Append(CSPDirectiveConnectSrc, fmt.Sprintf("wss://%[1]s ws://%[1]s", extraHost.Host)) | ||
} | ||
// We also require this to make http/https requests to the workspace proxy for latency checking. | ||
cspSrcs.Append(CSPDirectiveConnectSrc, fmt.Sprintf("https://%[1]s http://%[1]s", extraHost.Host)) | ||
} | ||
} | ||
47 changes: 37 additions & 10 deletionscoderd/httpmw/csp_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletionscoderd/proxyhealth/proxyhealth.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package proxyhealth | ||
code-asher marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
type ProxyHost struct { | ||
// Host is the root host of the proxy. | ||
Host string | ||
// AppHost is the wildcard host where apps are hosted. | ||
AppHost string | ||
} |
20 changes: 20 additions & 0 deletionscoderd/workspaceapps/appurl/appurl.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletionscoderd/workspaceapps/appurl/appurl_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
34 changes: 19 additions & 15 deletionsenterprise/coderd/proxyhealth/proxyhealth.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.