Skip to content
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

Allow the CSP nonce attribute to be set on the inline script in the page template #2245

Merged
merged 1 commit into from
Jun 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Allow nonce attribute to be set on inline script
Not all services are able to follow the currently recommended approach of using
hashes to allow specific inline scripts as part of their Content Security
Policy.

An alternative approach is to use a nonce which requires the attribute to be set
on the script itself.

Introduce a new Nunjucks variable `cspNonce` for the page template
to allow users to do this.
  • Loading branch information
nataliecarey committed Jun 10, 2021
commit 2e40d744af6e6e4213ebc47644982d4eb94422d4
2 changes: 1 addition & 1 deletion src/govuk/template.njk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<meta property="og:image" content="{{ assetUrl | default('/assets') }}/images/govuk-opengraph-image.png">
</head>
<body class="govuk-template__body {{ bodyClasses }}" {%- for attribute, value in bodyAttributes %} {{attribute}}="{{value}}"{% endfor %}>
<script>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
<script{% if cspNonce %} nonce="{{ cspNonce }}"{% endif %}>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
{% block bodyStart %}{% endblock %}

{% block skipLink %}
Expand Down
12 changes: 12 additions & 0 deletions src/govuk/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ describe('Template', () => {
// updating the hash published in https://frontend.design-system.service.gov.uk/importing-css-assets-and-javascript/#if-your-javascript-isn-t-working-properly
expect('sha256-' + hash).toEqual('sha256-+6WnXIl4mbFTCARd8N3COQmT3bJJmo32N8q8ZSQAIcU=')
})
it('should not have a nonce attribute by default', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests – these look great 🙌🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good. It's important :)

const $ = renderTemplate()
const scriptTag = $('body > script').first()

expect(scriptTag.attr('nonce')).toEqual(undefined)
})
it('should have a nonce attribute when nonce is provided', () => {
const $ = renderTemplate({ cspNonce: 'abcdef' })
const scriptTag = $('body > script').first()

expect(scriptTag.attr('nonce')).toEqual('abcdef')
})
})

describe('skip link', () => {
Expand Down