Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v15] refactor: export getContentSecurityPolicyString and CSPMap #44817

Merged
merged 6 commits into from
Aug 7, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions lib/httplib/httpheaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ func newCSPCache() *cspCache {
}
}

type cspMap map[string][]string
// CSPMap holds a map of Content Security Policy.
type CSPMap map[string][]string

var defaultContentSecurityPolicy = cspMap{
var defaultContentSecurityPolicy = CSPMap{
"default-src": {"'self'"},
"script-src": {"'self'"},
// specify CSP directives not covered by `default-src`
Expand All @@ -77,24 +78,24 @@ var defaultContentSecurityPolicy = cspMap{
"style-src": {"'self'", "'unsafe-inline'"},
}

var defaultFontSrc = cspMap{"font-src": {"'self'", "data:"}}
var defaultConnectSrc = cspMap{"connect-src": {"'self'", "wss:"}}
var defaultFontSrc = CSPMap{"font-src": {"'self'", "data:"}}
var defaultConnectSrc = CSPMap{"connect-src": {"'self'", "wss:"}}

var stripeSecurityPolicy = cspMap{
var stripeSecurityPolicy = CSPMap{
// auto-pay plans in Cloud use stripe.com to manage billing information
"script-src": {"https://js.stripe.com"},
"frame-src": {"https://js.stripe.com"},
}

var wasmSecurityPolicy = cspMap{
var wasmSecurityPolicy = CSPMap{
"script-src": {"'self'", "'wasm-unsafe-eval'"},
}

// combineCSPMaps combines multiple CSP maps into a single map.
// When multiple of the input cspMaps have the same key, their
// When multiple of the input CSPMap have the same key, their
// respective lists are concatenated.
func combineCSPMaps(cspMaps ...cspMap) cspMap {
combinedMap := make(cspMap)
func combineCSPMaps(cspMaps ...CSPMap) CSPMap {
combinedMap := make(CSPMap)

for _, cspMap := range cspMaps {
for key, value := range cspMap {
Expand All @@ -106,11 +107,11 @@ func combineCSPMaps(cspMaps ...cspMap) cspMap {
return combinedMap
}

// getContentSecurityPolicyString combines multiple CSP maps into a single
// GetContentSecurityPolicyString combines multiple CSP maps into a single
// CSP string, alphabetically sorted by the directive key.
// When multiple of the input cspMaps have the same key, their
// respective lists are concatenated.
func getContentSecurityPolicyString(cspMaps ...cspMap) string {
func GetContentSecurityPolicyString(cspMaps ...CSPMap) string {
combined := combineCSPMaps(cspMaps...)

keys := make([]string, 0, len(combined))
Expand Down Expand Up @@ -175,8 +176,8 @@ func SetDefaultSecurityHeaders(h http.Header) {
h.Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
}

func getIndexContentSecurityPolicy(withStripe, withWasm bool) cspMap {
cspMaps := []cspMap{defaultContentSecurityPolicy, defaultFontSrc, defaultConnectSrc}
func getIndexContentSecurityPolicy(withStripe, withWasm bool) CSPMap {
cspMaps := []CSPMap{defaultContentSecurityPolicy, defaultFontSrc, defaultConnectSrc}

if withStripe {
cspMaps = append(cspMaps, stripeSecurityPolicy)
Expand Down Expand Up @@ -209,7 +210,7 @@ func getIndexContentSecurityPolicyString(cfg proto.Features, urlPath string) str

// Nothing found in cache, calculate regex and result
withWasm := desktopSessionRe.MatchString(urlPath) || recordingRe.MatchString(urlPath)
cspString := getContentSecurityPolicyString(
cspString := GetContentSecurityPolicyString(
getIndexContentSecurityPolicy(withStripe, withWasm),
)
// Add result to cache
Expand Down Expand Up @@ -260,9 +261,9 @@ func getRedirectPageContentSecurityPolicyString(scriptSrc string) string {
return cspString
}

cspString := getContentSecurityPolicyString(
cspString := GetContentSecurityPolicyString(
defaultContentSecurityPolicy,
cspMap{
CSPMap{
"script-src": {"'" + scriptSrc + "'"},
},
)
Expand Down
Loading