{{- /* The Content-Security-Policy of a page, "" for none: */ -}} {{- /* 1. security.contentSecurityPolicy of the front matter replaces the policy of the site; */ -}} {{- /* 2. else the directives of params.security.csp, each extended by the sources of the same */ -}} {{- /* directive in the security.csp front matter of the page; */ -}} {{- /* 3. else params.security.contentSecurityPolicy. */ -}} {{- $site := .Site.Params.security -}} {{- $site = cond (reflect.IsMap $site) $site dict -}} {{- $page := .Params.security -}} {{- $page = cond (reflect.IsMap $page) $page dict -}} {{- $policy := "" -}} {{- with $page.contentSecurityPolicy -}} {{- $policy = printf "%v" . | strings.TrimSpace -}} {{- else -}} {{- with $site.csp -}} {{- $directives := newScratch -}} {{- if reflect.IsMap . -}} {{- range $name, $value := . -}} {{- $result := partial "function/csp-sources.html" (dict "key" "params.security.csp" "name" $name "value" $value) -}} {{- if and $result.valid (not $result.remove) -}} {{- $directives.SetInMap "csp" (lower $name) $result.sources -}} {{- end -}} {{- end -}} {{- else -}} {{- warnidf "loveit-csp-site" "params.security.csp must be a map of Content-Security-Policy directives, such as script-src = [\"'self'\"]. It is ignored." -}} {{- end -}} {{- with $page.csp -}} {{- if reflect.IsMap . -}} {{- range $name, $value := . -}} {{- $result := partial "function/csp-sources.html" (dict "key" "security.csp" "name" $name "value" $value) -}} {{- $name = lower $name -}} {{- if $result.remove -}} {{- $directives.DeleteInMap "csp" $name -}} {{- else if $result.valid -}} {{- $sources := index ($directives.Get "csp" | default dict) $name | default slice -}} {{- $directives.SetInMap "csp" $name (union $sources $result.sources) -}} {{- end -}} {{- end -}} {{- else -}} {{- warnidf "loveit-csp-page" "The security.csp front matter of %q must be a map of Content-Security-Policy directives, such as connect-src: [\"https://api.example.com\"]. It is ignored." $.Path -}} {{- end -}} {{- end -}} {{- $csp := $directives.Get "csp" | default dict -}} {{- /* hugo server reloads the page over a WebSocket to its own host, which 'self' matches only in browsers that follow CSP Level 3. */ -}} {{- if hugo.IsServer -}} {{- $url := urls.Parse $.Site.BaseURL -}} {{- $target := cond (isset $csp "connect-src") "connect-src" (cond (isset $csp "default-src") "default-src" "") -}} {{- if and $target $url.Host -}} {{- $socket := printf "%s://%s" (cond (eq $url.Scheme "https") "wss" "ws") $url.Host -}} {{- $directives.SetInMap "csp" $target (union (index $csp $target) (slice $socket)) -}} {{- $csp = $directives.Get "csp" -}} {{- end -}} {{- end -}} {{- $parts := slice -}} {{- range $name, $sources := $csp -}} {{- $parts = $parts | append (delimit (slice $name | append $sources) " ") -}} {{- end -}} {{- $policy = delimit $parts "; " -}} {{- else -}} {{- with $page.csp -}} {{- warnidf "loveit-csp-page-without-site" "The security.csp front matter of %q is ignored: it extends the directives of params.security.csp, which the site does not set. Set params.security.csp, or replace the whole policy of the page with security.contentSecurityPolicy." $.Path -}} {{- end -}} {{- with $site.contentSecurityPolicy -}} {{- $policy = printf "%v" . | strings.TrimSpace -}} {{- end -}} {{- end -}} {{- end -}} {{- return $policy -}}