# Add CSP nonce-source support

**URL:** <https://meta.discourse.org/t/add-csp-nonce-source-support/93300>\
**Category:** Development\
**Created:** [July 27, 2018, 4:19pm UTC](https://meta.discourse.org/t/add-csp-nonce-source-support/93300 "2018-07-27T16:19:33Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![LeoMcA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leomca/32/87233_2.png) [@LeoMcA](https://meta.discourse.org/u/LeoMcA)\
**Post date:** [July 27, 2018, 4:19pm UTC](https://meta.discourse.org/t/add-csp-nonce-source-support/93300/1 "2018-07-27T16:19:33Z")

</div>

While [moving all inline scripts externally would take a tremendous amount of effort](https://meta.discourse.org/t/how-to-configure-content-security-policy/53308/14), as far as I can see, adding nonce-source support would be relatively trivial.

nonce-source works by adding a `nonce` attribute to any `<script>` tags you want to run, which is a base64 string randomly generated on each page load. This nonce is also added to the CSP header, and only scripts with the correct nonce set are allowed to run.

This means inline scripts can be whitelisted without the need to whitelist _all_ inline scripts with `unsafe-inline`, and external scripts can be included without the need to whitelist them explicitly in the header (which is useful for themes loading scripts from an external CDN).

More info:

- [CSP for the web we have - Mozilla Security Blog](https://blog.mozilla.org/security/2014/10/04/csp-for-the-web-we-have/)
- [Content-Security-Policy: script-src directive - HTTP | MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src)

To achieve this in Discourse we could generate the nonce on every page load in `ApplicationController`, add it to `request.env` so we can insert it where we want, and set the appropriate CSP header (or a custom header so CSP headers can be written in the nginx config).

I believe this addresses the problems identified before:

> [@sam](#):
>
> `_discourse_javascript.html.erb` partial is huge, it would need to be addressed somehow and it is very complex to address because it dynamically generates stuff

This, along with the other `.html.erb` partials containing inline scripts could simply include the `nonce` attribute within their script tags.

> [@sam](#):
>
> - It would involve extensive changes to the theme system

[`app/helpers/application_helper#theme_lookup`](https://github.com/discourse/discourse/blob/master/app/helpers/application_helper.rb#L376) seems to be the method used for inserting themes’ `head_tags`. It could be extended to parse the html to be inserted, and add the `nonce` attribute to any script tags.

> [@sam](#):
>
> - Breaking a large number of plugins

This could sit behind a site preference, so sites relying on plugins which don’t update to work with nonces don’t break them.

> [@sam](#):
>
> - Extensive changes to the application

From the previous CSP feedback, and my own grepping of places `<script>` tags appear in the codebase, I can’t see what else would have to be done to make Discourse compatible with nonce-source.

Is there anything I’ve missed? Would a PR be welcome to implement this?

I haven’t yet thought through how this could be tested to ensure `<script>` tags lacking a `nonce` aren’t added in future.

---

_[View the full topic](https://meta.discourse.org/t/add-csp-nonce-source-support/93300)._
