Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Add an HTTP Content-Security-Policy #444

Merged
merged 1 commit into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<%= yield :meta_tags %>
</head>
<body class="govuk-template__body">
<script>
<%= javascript_tag nonce: true do %>
document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');
</script>
<% end %>
<%= render "govuk_publishing_components/components/cookie_banner", {
title: t("cookies.banner.title"),
text: t("cookies.banner.text"),
Expand Down Expand Up @@ -75,6 +75,6 @@
]
}
} %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag "application", nonce: true %>
</body>
</html>
32 changes: 18 additions & 14 deletions config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

# Define an application-wide content security policy
# For further information see the following documentation
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/
# Content-Security-Policy

# Rails.application.config.content_security_policy do |policy|
# policy.default_src :self, :https
# policy.font_src :self, :https, :data
# policy.img_src :self, :https, :data
# policy.object_src :none
# policy.script_src :self, :https
# policy.style_src :self, :https
Rails.application.config.content_security_policy do |policy|
policy.default_src :self, :https
policy.font_src :self, :https, :data
policy.img_src :self, :https, :data
policy.object_src :none
policy.script_src :self, :https, :data, "https://www.google-analytics.com"
policy.style_src :self, :https
policy.media_src :none
policy.child_src :self
policy.form_action :self
policy.frame_ancestors :self
policy.connect_src :none

# # Specify URI for violation reports
# # policy.report_uri "/csp-violation-report-endpoint"
# end
# # Specify URI for violation reports
# # policy.report_uri "/csp-violation-report-endpoint"
end

# If you are using UJS then enable automatic nonce generation
# Rails.application.config.content_security_policy_nonce_generator = ->
# request { SecureRandom.base64(16) }
Rails.application.config.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) }

# Set the nonce only to specific directives
# Rails.application.config.content_security_policy_nonce_directives =
# %w(script-src)
Rails.application.config.content_security_policy_nonce_directives = %w[script-src]

# Report CSP violations to a specified URI
# For further information see the following documentation:
Expand Down