|
1 |
| -module CSP |
2 |
| - # Generate a Content Security Policy (CSP) directive. |
3 |
| - # |
4 |
| - # This code should eventually be moved to https://github.com/alphagov/govuk_app_config |
5 |
| - # |
6 |
| - # |
7 |
| - # Extracted in a separate module to allow comments. |
8 |
| - # |
9 |
| - # See https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP for more CSP info. |
10 |
| - # |
11 |
| - # The resulting policy should be checked with: |
12 |
| - # |
13 |
| - # - https://csp-evaluator.withgoogle.com |
14 |
| - # - https://cspvalidator.org |
15 |
| - |
16 |
| - GOVUK_DOMAINS = "'self' *.publishing.service.gov.uk localhost".freeze |
17 |
| - |
18 |
| - GOOGLE_ANALYTICS_DOMAINS = "www.google-analytics.com ssl.google-analytics.com".freeze |
19 |
| - |
20 |
| - def self.build |
21 |
| - policies = [] |
22 |
| - |
23 |
| - # By default, only allow HTTPS connections, and allow loading things from |
24 |
| - # the publishing domain |
25 |
| - # |
26 |
| - # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src |
27 |
| - policies << [ |
28 |
| - "default-src https", |
29 |
| - GOVUK_DOMAINS |
30 |
| - ] |
31 |
| - |
32 |
| - # Allow images from the current domain, Google Analytics (the tracking pixel), |
33 |
| - # and publishing domains. |
34 |
| - # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/img-src |
35 |
| - policies << [ |
36 |
| - "img-src", |
37 |
| - |
38 |
| - # Allow `data:` images for Base64-encoded images in CSS like: |
39 |
| - # |
40 |
| - # https://github.com/alphagov/service-manual-frontend/blob/1db99ed48de0dfc794b9686a98e6c62f8435ae80/app/assets/stylesheets/modules/_search.scss#L106 |
41 |
| - "data:", |
42 |
| - |
43 |
| - GOVUK_DOMAINS, |
44 |
| - GOOGLE_ANALYTICS_DOMAINS, |
45 |
| - |
46 |
| - # Some content still links to an old domain we used to use |
47 |
| - "assets.digital.cabinet-office.gov.uk", |
48 |
| - ] |
49 |
| - |
50 |
| - # script-src determines the scripts that the browser can load |
51 |
| - # |
52 |
| - # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src |
53 |
| - policies << [ |
54 |
| - # Allow scripts from publishing domains |
55 |
| - "script-src", |
56 |
| - GOVUK_DOMAINS, |
57 |
| - GOOGLE_ANALYTICS_DOMAINS, |
58 |
| - |
59 |
| - # Allow JSONP call to Verify to check whether the user is logged in |
60 |
| - # https://www.staging.publishing.service.gov.uk/log-in-file-self-assessment-tax-return/sign-in/prove-identity |
61 |
| - # https://github.com/alphagov/government-frontend/blob/71aca4df9b74366618a5a93acdb5cd2715f94f49/app/assets/javascripts/modules/track-radio-group.js |
62 |
| - "www.signin.service.gov.uk", |
63 |
| - |
64 |
| - # Allow YouTube Embeds (Govspeak turns YouTube links into embeds) |
65 |
| - "*.ytimg.com", |
66 |
| - "www.youtube.com", |
67 |
| - |
68 |
| - # Allow all inline scripts until we can conclusively document all the inline scripts we use, |
69 |
| - # and there's a better way to filter out junk reports |
70 |
| - "'unsafe-inline'" |
71 |
| - ] |
72 |
| - |
73 |
| - # Allow styles from own domain and publishing domains. |
74 |
| - # |
75 |
| - # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src |
76 |
| - policies << [ |
77 |
| - "style-src", |
78 |
| - GOVUK_DOMAINS, |
79 |
| - |
80 |
| - # Also allow "unsafe-inline" styles, because we use the `style=""` attribute on some HTML elements |
81 |
| - "'unsafe-inline'" |
82 |
| - ] |
83 |
| - |
84 |
| - # Allow fonts to be loaded from data-uri's (this is the old way of doing things) |
85 |
| - # or from the publishing asset domains. |
86 |
| - # |
87 |
| - # https://www.staging.publishing.service.gov.uk/apply-for-a-licence/test-licence/westminster/apply-1 |
88 |
| - # |
89 |
| - # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/font-src |
90 |
| - policies << [ |
91 |
| - "font-src data:", |
92 |
| - GOVUK_DOMAINS |
93 |
| - ] |
94 |
| - |
95 |
| - # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/connect-src |
96 |
| - policies << [ |
97 |
| - # Scripts can only load data using Ajax from Google Analytics and the publishing domains |
98 |
| - "connect-src", |
99 |
| - GOVUK_DOMAINS, |
100 |
| - GOOGLE_ANALYTICS_DOMAINS, |
101 |
| - |
102 |
| - # Allow connecting to web chat from HMRC contact pages like |
103 |
| - # https://www.staging.publishing.service.gov.uk/government/organisations/hm-revenue-customs/contact/child-benefit |
104 |
| - "www.tax.service.gov.uk", |
105 |
| - |
106 |
| - # Allow connecting to Verify to check whether the user is logged in |
107 |
| - # https://github.com/alphagov/government-frontend/blob/71aca4df9b74366618a5a93acdb5cd2715f94f49/app/assets/javascripts/modules/track-radio-group.js |
108 |
| - # https://www.staging.publishing.service.gov.uk/log-in-file-self-assessment-tax-return/sign-in/prove-identity |
109 |
| - "www.signin.service.gov.uk", |
110 |
| - ] |
111 |
| - |
112 |
| - # Disallow all <object>, <embed>, and <applet> elements |
113 |
| - # |
114 |
| - # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/object-src |
115 |
| - policies << [ |
116 |
| - "object-src 'none'" |
117 |
| - ] |
118 |
| - |
119 |
| - policies << [ |
120 |
| - "frame-src", |
121 |
| - |
122 |
| - # Allow YouTube embeds |
123 |
| - "www.youtube.com", |
124 |
| - ] |
125 |
| - |
126 |
| - policies.map { |str| str.join(" ") }.join("; ") + ";" |
127 |
| - end |
128 |
| -end |
129 |
| - |
130 |
| -# In test and development, use CSP for real to find issues. In production we only |
131 |
| -# report violations to Sentry (https://sentry.io/govuk/govuk-frontend-csp) via an |
132 |
| -# AWS Lambda function that filters out junk reports. |
133 |
| -if Rails.env.production? |
134 |
| - reporting = "report-uri https://jhpno0hk6b.execute-api.eu-west-2.amazonaws.com/production" |
135 |
| - Rails.application.config.action_dispatch.default_headers['Content-Security-Policy-Report-Only'] = CSP.build + " " + reporting |
136 |
| -else |
137 |
| - Rails.application.config.action_dispatch.default_headers['Content-Security-Policy'] = CSP.build |
138 |
| -end |
| 1 | +GovukContentSecurityPolicy.configure |
0 commit comments