Skip to content

Commit

Permalink
server: Enforce CSP with --app.strict_csp_enabled (#7717)
Browse files Browse the repository at this point in the history
There haven't been any CSP reports in dev since the latest fix. The flag
is only enabled in dev, with violations upgraded to warnings.

Also enables the flag in webdriver tests.

<!-- Optional: Provide additional context (beyond the PR title). -->

<!-- Optional: link a GitHub issue.
Example: "Fixes #123" will auto-close #123 when the PR is merged. -->

**Related issues**: buildbuddy-io/buildbuddy-internal#3911
  • Loading branch information
fmeum authored Nov 6, 2024
1 parent aac225f commit 1e9bbb8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func SetupWebTarget(t *testing.T, localArgs ...string) WebTarget {
args := append([]string{
"--cache.detailed_stats_enabled=true",
"--app.user_owned_keys_enabled=true",
"--app.strict_csp_enabled=true",
}, localArgs...)
return Run(t, args...)
case "remote":
Expand Down
2 changes: 1 addition & 1 deletion server/http/csp/csp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ReportingEndpoint = "/csp-report"

var ReportingHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
for _, report := range extractReports(r.Body) {
log.CtxDebug(r.Context(), report)
log.CtxWarning(r.Context(), report)
}
})

Expand Down
5 changes: 2 additions & 3 deletions server/http/interceptors/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (

var (
upgradeInsecure = flag.Bool("ssl.upgrade_insecure", false, "True if http requests should be redirected to https. Assumes http traffic is served on port 80 and https traffic is served on port 443 (typically via an ingress / load balancer).")
strictCspEnabled = flag.Bool("app.strict_csp_enabled", false, "If set, enable a strict CSP header in report only mode.")
strictCspEnabled = flag.Bool("app.strict_csp_enabled", false, "If set, set a strict CSP header. Violations are logged at warning level.")
)

const contentSecurityPolicyReportingEndpointName = "csp-endpoint"
Expand Down Expand Up @@ -94,8 +94,7 @@ func setContentSecurityPolicy(h http.Header) string {
panic(fmt.Sprintf("Failed to generate nonce: %s", err))
}
nonce := base64.StdEncoding.EncodeToString(nonceBytes)
// TODO: Enable this by dropping the "-Report-Only" suffix.
h.Set("Content-Security-Policy-Report-Only", getContentSecurityPolicyHeaderValue(nonce))
h.Set("Content-Security-Policy", getContentSecurityPolicyHeaderValue(nonce))
return nonce
}

Expand Down

0 comments on commit 1e9bbb8

Please sign in to comment.