-
Notifications
You must be signed in to change notification settings - Fork 3
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
Configure Content Security Policy to remove risky allowed properties #279
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,12 @@ def self.build_policy(policy) | |
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src | ||
policy.default_src :self | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/base-uri | ||
policy.base_uri :none | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/img-src | ||
# Note: we purposely don't include `data:` here because it produces a security risk. | ||
policy.img_src :self, | ||
# This allows Base64 encoded images, but is a security | ||
# risk as it can embed third party resources. | ||
# As of December 2022, we intend to remove this prior | ||
# to making the CSP live. | ||
:data, | ||
*GOVUK_DOMAINS, | ||
*GOOGLE_ANALYTICS_DOMAINS, # Tracking pixels | ||
# Speedcurve real user monitoring (RUM) - as per: https://support.speedcurve.com/docs/add-rum-to-your-csp | ||
|
@@ -45,25 +44,28 @@ def self.build_policy(policy) | |
"https://img.youtube.com" | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src | ||
# Note: we purposely don't include `data:`, `unsafe-inline` or `unsafe-eval` because | ||
# they are security risks, if you need them for a legacy app please only apply them at | ||
# an app level. | ||
policy.script_src :self, | ||
*GOOGLE_ANALYTICS_DOMAINS, | ||
*GOOGLE_STATIC_DOMAINS, | ||
# Allow YouTube Embeds (Govspeak turns YouTube links into embeds) | ||
"*.ytimg.com", | ||
"www.youtube.com", | ||
"www.youtube-nocookie.com", | ||
# This allows inline scripts and thus is a XSS risk. | ||
# As of December 2022, we intend to work towards removing | ||
# this from apps that don't use jQuery 1.12 (which needs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also is it worth keeping this reference somewhere (e.g. the change note)? Might be a useful prompt for devs to check what version of jQuery they're running...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good shout. I thought this was a non-concern as I ensured the removal of jQuery 1.x on all frontend apps. I, however, had no idea that that in the 2020 Rails update work a bunch of admin applications had the Content Security Policy switched on (including oddly some API only apps): https://sourcegraph.com/search?q=context:global+repo:alphagov+GovukContentSecurityPolicy&patternType=standard&sm=0&groupBy=repo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should now be covered in the changelog not, but I've been and swept through apps already to have them ready. |
||
# this) once we've set up nonces. | ||
:unsafe_inline | ||
"www.youtube-nocookie.com" | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src | ||
# Note: we purposely don't include `data:` or `unsafe-eval` because | ||
# they are security risks, if you need them for a legacy app please only apply them at | ||
# an app level. | ||
policy.style_src :self, | ||
*GOOGLE_STATIC_DOMAINS, | ||
# This allows style="" attributes and style elements. | ||
# As of December 2022, we intend to remove this prior | ||
# to making the CSP live due to the security risks it has. | ||
# This allows `style=""` attributes and `<style>` elements. | ||
# As of January 2023 our intentions to remove this were scuppered | ||
# by Govspeak [1] using inline styles on tables. Until that | ||
# is resolved we'll keep unsafe_inline | ||
# [1]: https://github.com/alphagov/govspeak/blob/5642fcc4231f215d1c58ad7feb30ca42fb8cfb91/lib/govspeak/html_sanitizer.rb#L72-L73 | ||
:unsafe_inline | ||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/font-src | ||
|
@@ -91,6 +93,20 @@ def self.build_policy(policy) | |
def self.configure | ||
Rails.application.config.content_security_policy_report_only = ENV.include?("GOVUK_CSP_REPORT_ONLY") | ||
|
||
# Sets a nonce per request that can be set on script-src and style-src | ||
# directives depending on the value of Rails.application.config.content_security_policy_nonce_directives | ||
# | ||
# Note: if an application needs to set unsafe-inline they will need to | ||
# unset this generator (by setting this config option to nil in their application) | ||
Rails.application.config.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) } | ||
|
||
# This only applies the nonce generator to the script-src directive. We need this to | ||
# use unsafe-inline for style-src as a nonce will override it. | ||
# | ||
# When we want to apply it to style-src we can remove this line as the Rails default | ||
# is for both script-src and style-src | ||
Rails.application.config.content_security_policy_nonce_directives = %w[script-src] | ||
|
||
policy = Rails.application.config.content_security_policy(&method(:build_policy)) | ||
|
||
# # allow apps to customise the CSP by passing a block e.g: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module GovukAppConfig | ||
VERSION = "4.13.0".freeze | ||
VERSION = "5.0.0".freeze | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍