- Notifications
You must be signed in to change notification settings - Fork1k
chore: add deployment config option to append custom csp directives#15596
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5,6 +5,7 @@ import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"database/sql" | ||
"errors" | ||
"expvar" | ||
"flag" | ||
"fmt" | ||
@@ -1378,6 +1379,26 @@ func New(options *Options) *API { | ||
r.Get("/swagger/*", swaggerDisabled) | ||
} | ||
additionalCSPHeaders := make(map[httpmw.CSPFetchDirective][]string, len(api.DeploymentValues.AdditionalCSPPolicy)) | ||
var cspParseErrors error | ||
for _, v := range api.DeploymentValues.AdditionalCSPPolicy { | ||
// Format is "<directive> <value> <value> ..." | ||
v = strings.TrimSpace(v) | ||
parts := strings.Split(v, " ") | ||
if len(parts) < 2 { | ||
cspParseErrors = errors.Join(cspParseErrors, xerrors.Errorf("invalid CSP header %q, not enough parts to be valid", v)) | ||
continue | ||
} | ||
additionalCSPHeaders[httpmw.CSPFetchDirective(strings.ToLower(parts[0]))] = parts[1:] | ||
} | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if cspParseErrors != nil { | ||
// Do not fail Coder deployment startup because of this. Just log an error | ||
// and continue | ||
api.Logger.Error(context.Background(), | ||
"parsing additional CSP headers", slog.Error(cspParseErrors)) | ||
} | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// Add CSP headers to all static assets and pages. CSP headers only affect | ||
// browsers, so these don't make sense on api routes. | ||
cspMW := httpmw.CSPHeaders(options.Telemetry.Enabled(), func() []string { | ||
@@ -1390,7 +1411,7 @@ func New(options *Options) *API { | ||
} | ||
// By default we do not add extra websocket connections to the CSP | ||
return []string{} | ||
}, additionalCSPHeaders) | ||
// Static file handler must be wrapped with HSTS handler if the | ||
// StrictTransportSecurityAge is set. We only need to set this header on | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -391,6 +391,7 @@ type DeploymentValues struct { | ||
CLIUpgradeMessage serpent.String `json:"cli_upgrade_message,omitempty" typescript:",notnull"` | ||
TermsOfServiceURL serpent.String `json:"terms_of_service_url,omitempty" typescript:",notnull"` | ||
Notifications NotificationsConfig `json:"notifications,omitempty" typescript:",notnull"` | ||
AdditionalCSPPolicy serpent.StringArray `json:"additional_csp_policy,omitempty" typescript:",notnull"` | ||
Config serpent.YAMLConfigPath `json:"config,omitempty" typescript:",notnull"` | ||
WriteConfig serpent.Bool `json:"write_config,omitempty" typescript:",notnull"` | ||
@@ -2147,6 +2148,18 @@ when required by your organization's security policy.`, | ||
Group: &deploymentGroupIntrospectionLogging, | ||
YAML: "enableTerraformDebugMode", | ||
}, | ||
{ | ||
Name: "Additional CSP Policy", | ||
Description: "Coder configures a Content Security Policy (CSP) to protect against XSS attacks. " + | ||
"This setting allows you to add additional CSP directives, which can open the attack surface of the deployment. " + | ||
"Format matches the CSP directive format, e.g. --additional-csp-policy=\"script-src https://example.com\".", | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Flag: "additional-csp-policy", | ||
Env: "CODER_ADDITIONAL_CSP_POLICY", | ||
YAML: "additionalCSPPolicy", | ||
Value: &c.AdditionalCSPPolicy, | ||
Group: &deploymentGroupNetworkingHTTP, | ||
}, | ||
// ☢️ Dangerous settings | ||
{ | ||
Name: "DANGEROUS: Allow all CORS requests", | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.