| 1 | package googleimport |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // Policy extends one app policy for its import page. It never relaxes form |
| 10 | // targets, framing protection, base URLs, inline scripts or eval. Multiple |
| 11 | // policies/duplicate directives are refused rather than ambiguously merged. |
| 12 | func Policy(base string) (string, error) { |
| 13 | if strings.ContainsAny(base, ",\r\n") { |
| 14 | return "", errors.New("googleimport: expected one content security policy") |
| 15 | } |
| 16 | order := []string{} |
| 17 | directives := map[string][]string{} |
| 18 | for _, part := range strings.Split(base, ";") { |
| 19 | fields := strings.Fields(part) |
| 20 | if len(fields) == 0 { |
| 21 | continue |
| 22 | } |
| 23 | name := strings.ToLower(fields[0]) |
| 24 | if _, exists := directives[name]; exists { |
| 25 | return "", errors.New("googleimport: duplicate policy directive") |
| 26 | } |
| 27 | order = append(order, name) |
| 28 | directives[name] = fields[1:] |
| 29 | } |
| 30 | if len(directives["default-src"]) == 0 { |
| 31 | return "", errors.New("googleimport: policy needs default-src") |
| 32 | } |
| 33 | add := func(name string, sources ...string) { |
| 34 | values, exists := directives[name] |
| 35 | if !exists { |
| 36 | order = append(order, name) |
| 37 | fallback := directives["default-src"] |
| 38 | if name == "frame-src" && len(directives["child-src"]) > 0 { |
| 39 | fallback = directives["child-src"] |
| 40 | } |
| 41 | values = append([]string(nil), fallback...) |
| 42 | } |
| 43 | out := []string{} |
| 44 | seen := map[string]bool{} |
| 45 | for _, source := range append(values, sources...) { |
| 46 | if source != "'none'" && !seen[source] { |
| 47 | seen[source] = true |
| 48 | out = append(out, source) |
| 49 | } |
| 50 | } |
| 51 | directives[name] = out |
| 52 | } |
| 53 | // The chooser module is served by the app itself, even if its original |
| 54 | // policy disallowed scripts entirely. |
| 55 | add("script-src", "'self'", "https://accounts.google.com", "https://apis.google.com") |
| 56 | if _, exists := directives["script-src-elem"]; exists { |
| 57 | add("script-src-elem", "'self'", "https://accounts.google.com", "https://apis.google.com") |
| 58 | } |
| 59 | add("connect-src", "https://accounts.google.com", "https://apis.google.com", "https://www.googleapis.com") |
| 60 | add("frame-src", "https://accounts.google.com", "https://docs.google.com") |
| 61 | parts := make([]string, 0, len(order)) |
| 62 | for _, name := range order { |
| 63 | parts = append(parts, strings.Join(append([]string{name}, directives[name]...), " ")) |
| 64 | } |
| 65 | return strings.Join(parts, "; "), nil |
| 66 | } |
| 67 | |
| 68 | // Headers must run before rendering the opted-in import page, including a |
| 69 | // refused upload re-render. base is the app's existing policy constant. |
| 70 | func Headers(w http.ResponseWriter, base string) error { |
| 71 | if len(w.Header().Values("Content-Security-Policy")) > 1 { |
| 72 | return errors.New("googleimport: multiple content security policies") |
| 73 | } |
| 74 | policy, err := Policy(base) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | w.Header().Set("Content-Security-Policy", policy) |
| 79 | w.Header().Set("Cache-Control", "no-store") |
| 80 | return nil |
| 81 | } |
| 82 | |