Skip to content

Commit

Permalink
Configure GovukContentSecurityPolicy for govuk_app_config changes
Browse files Browse the repository at this point in the history
This configures the content security policy in preparation for the
breaking changes coming from
alphagov/govuk_app_config#279. As this app uses
govuk_admin_template and that uses jQuery 1.x and inline script tags,
then this app needs unsafe_inline for script and the nonce generator
disabled.

As an aside, It was a surprise that this application had configured the
GovukContentSecurityPolicy as this had been initially done just in
Frontend apps and it looks like this made it through in some outsourced
Rails updates [1]. I'm leaving this config in so there is an example of
an app outside of frontend using it to build on and as a case study in
configuring an app.

[1]: 45a5a51
  • Loading branch information
kevindew committed Jan 24, 2023
1 parent 4d492b5 commit ddcf31f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion config/initializers/content_security_policy.rb
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
GovukContentSecurityPolicy.configure
GovukContentSecurityPolicy.configure do |policy|
# Ensures the ability to use inline JavaScript without protections. This is
# required for compatibility with govuk_admin_template which both uses script
# tags without nonces and uses jQuery 1.x which requires unsafe-inline in
# some browsers (Firefox is one)
script_policy_with_unsafe_inline = (policy.script_src + ["'unsafe-inline'"]).uniq
policy.script_src(*script_policy_with_unsafe_inline)
end

# Disable any configured nonce generators so that unsafe-inline directives
# can be used
Rails.application.config.content_security_policy_nonce_generator = nil

0 comments on commit ddcf31f

Please sign in to comment.