Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds SRI integrity attribute to template
How to test ----------- In the application using govuk_template, you will have to set `config.assets.debug = false` in config/environments/development.rb or run the application in production mode. This is because sprockets-rails checks for this flag when deciding if it should calculate the integrity attribute: https://github.com/rails/sprockets-rails/blob/10bc1bd096b39a3dd632571dd517788314657056/lib/sprockets/rails/helper.rb#L168 What is SRI ----------- SRI will add an integrity attribute on your script tags: <script src="https://example.com/example.css" integrity_no="sha384oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8w" crossorigin="anonymous"></script> The example above is generated automatically by sprockets-rails in your project if you do this: <%= stylesheet_script_tag 'example', integrity: true %> The way the digest value for the integrity attribute is calculated is by applying SHA256 on the contents of the file. In that way, the browser can double check that the right file is being received and hasn’t been tampered with (which would mean the contents would have changed and the resulting digest would be incorrect). The problem with using this in govuk_template --------------------------------------------- The css files it provided by govuk_template (for example: to the static project), are still css.erb files. In which case the contents of those files will change when they are processed in static (specifically there’s multiple <%= asset_path(...)%>s in there). So here’s the problem illustrated: 1. The govuk_template gem compiles a file called govuk_template.css.erb which contains multiple asset_path lines, e.g. background-image: url(<%= asset_path 'images/govuk-crest.png' %>); 2. static receives this file and replaces all the asset_path lines with it’s own path, e.g. background-image: url(**http://static.dev.gov.uk/static**/images/govuk-crest-2x.png); Notice that this url will vary depending on the app that uses the gem => this means the contents of the resulting govuk_template.css will vary depending on which app is using the gem so the digest will also vary. This means you can’t calculate the digest inside the gem, and will have to be left to the apps to do it individually 3. You cannot use sprockets-rails in govuk_template directly because the assets it serves are also compiled into django, play, liquid, mustache and other crazy formats. So you can’t do this: <%= stylesheet_link_tag ‘govuk-template-print’, integrity: true%> and then leave it up to sprockets-rails to calculate the digest for you, because this will not work for django and all the other formats. They don’t know what stylesheet_tag is. Instead, this is what govuk_template does so it can be compiled into multiple languages: <link href="<%= asset_path "govuk-template-print.css" %>" media="print" rel="stylesheet" /> 4. You cannot calculate the integrity digest by hand in the gem, because of point 2 -> the content will change Proposed solution ----------------- We use stylesheet_link_tag and set integrity: true, but we leave it up to the apps using the gem to actually calculate the integrity digest. For the other languages (django, jijna, liquid, mustache) we have defined a stylesheet_include_tag method in their respective processors that will translate that tag into something they can understand: <link href="<%= asset_path "govuk-template-print.css" %>" media="print" rel="stylesheet" />
- Loading branch information