| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // SecurityPolicy is the policy every response starts with. It preserves |
| 10 | // framework defaults and names, in form-action, the directory and nothing |
| 11 | // else on the credential origin; the two pages whose form goes to a consumer |
| 12 | // (the assertion handoff and the "Finish signing out" page for a preview) |
| 13 | // name that one consumer per response, through securityPolicy. That is |
| 14 | // what lets the consumer list outgrow the header (team-provisioning- |
| 15 | // amendment.md §7). The credential origin renders no account-authored |
| 16 | // markup. |
| 17 | func (a *App) SecurityPolicy() string { return a.securityPolicy("") } |
| 18 | |
| 19 | // setConsumerPolicy is the one place a handler replaces the policy, and it |
| 20 | // can only add one registered consumer to form-action: the rest is |
| 21 | // SecurityPolicy's, byte for byte (TestConsumerPolicyOnlyWidensFormAction). |
| 22 | // The assertion handoff and the preview "Finish signing out" page call it. |
| 23 | func (a *App) setConsumerPolicy(w http.ResponseWriter, consumer string) { |
| 24 | if !registeredOrigin(a.consumers(), consumer) { |
| 25 | return |
| 26 | } |
| 27 | w.Header().Set("Content-Security-Policy", a.securityPolicy(consumer)) |
| 28 | } |
| 29 | |
| 30 | func (a *App) securityPolicy(consumer string) string { |
| 31 | origins := map[string]bool{a.origins.Directory: true} |
| 32 | if consumer != "" { |
| 33 | origins[consumer] = true |
| 34 | } |
| 35 | // form-action governs a form's redirects too, so the directory's |
| 36 | // signed-out page posts a second form to the credential origin and is |
| 37 | // blocked in the browser without this. See signoutPath. |
| 38 | if a.runtime != nil && a.runtime.role == "directory" { |
| 39 | origins[a.origins.Credential] = true |
| 40 | } |
| 41 | list := make([]string, 0, len(origins)) |
| 42 | for origin := range origins { |
| 43 | list = append(list, origin) |
| 44 | } |
| 45 | sort.Strings(list) |
| 46 | // rastrillo's default policy (serve.go's defaultCSP) byte for byte but |
| 47 | // for form-action; TestSecurityPolicyPreservesFrameworkDefaults holds |
| 48 | // it there. Since rastrillo v0.27.0 style-src allows no inline style, |
| 49 | // only the framework's two hashed exceptions. |
| 50 | return "default-src 'self'; " + |
| 51 | "style-src 'self' 'unsafe-hashes' " + |
| 52 | "'sha256-yJxAE4rjdcckohdlnvecSporPcqS9xOaA4hJxi87LMc=' " + |
| 53 | "'sha256-81gZUb3tp60YCqhJ0dItBPh5hRQiGbfst4gpwCNPTfI='; " + |
| 54 | "img-src 'self' data:; frame-ancestors 'none'; base-uri 'self'; form-action 'self' " + strings.Join(list, " ") |
| 55 | } |
| 56 | |