What is Content Security Policy?
Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft to site defacement to distribution of malware.
– Content Security Policy (CSP) - HTTP | MDN
XSS is still one of the most common web vulnerability – if someone else can run scripts on your site, it is not your site anymore.
Discourse mitigates XSS attacks with CSP, by allowing scripts only from trusted sources to load and execute. Discourse’s default policy employs a strict URL whitelist, and only allows sources that Discourse needs.
Currently, Discourse ships a CSP Level 2 policy with the following directives by default:
worker-srcspecifies valid sources for ServiceWorker scripts
object-srcblocks the execution of plug-ins (Flash, Java, etc)
base-urirestricts the URLs for
The default policy will be expanded in future updates to include more directives.
content_security_policy: Turn on CSP (default off for existing sites, on for new instances)
content_security_policy_report_only: Turn on CSP Report-Only mode (default off)
CSP Report Only mode checks and reports the site’s violations, without blocking them. This can be used to experiment with different policies, and monitor their effects.
content_security_policy_collect_reports: Log CSP violations reports in
content_security_policy_script_src: Extend the default
script-srcby defining additional whitelisted script sources
How to Turn on CSP?
content_security_policy site setting, and viola, CSP is on!
However, since CSP will start blocking any non-whitelisted script sources immediately, I recommend the following steps to make sure that CSP is not blocking your site’s customizations:
- Turn on CSP Report-Only mode, and check for CSP violations.
- Extend the default CSP as needed
- Turn on Full CSP.
Are My Customizations CSP-Compliant?
<script type="text/discourse-plugin" version="0.8"> api.replaceIcon('heart', 'thumbs-up'); </script>
on Event Handlers
<a href="#" onclick="doSomething()">Foo</a>
on event handlers (e.g
Here is an example of refactoring the above code:
<a href="#" id="bar">Foo</a> <script> // if the element is already in the DOM document.getElementById('bar').addEventListener('click', doSomething); // otherwise, try event propagation $('#main').on('click', '#bar', doSomething); </script>
<script> Tags Linking to External Resources
- Copy and paste the script content into your theme / component, or
- Extend the default CSP by whitelisting the source.
4. Third-Party Script / Service Integration
Different integrations will have different requirements, but can be addressed similarly.
You could look up the integration’s recommended CSP whitelist, and extend the default CSP accordingly. I suggest simply turn on CSP Report Only mode in Discourse, and watch your console to determine which resources you’ll need to whitelist to make your integrations work.
This is especially important when using third-party script bundlers like Google Tag Manager or Segment, because these bundlers might load many third-party or inline scripts. (You might even end up adding
'unsafe-inline' when using Segment or GTM, even though this definition should be avoided as much as possible.)
Extending the Default CSP
1. Via Site Setting
Admins can extend their site’s CSP by defining additional script sources to be whitelisted in the
content_security_policy_script_src site setting.
You can extend
script-src with all valid source definitions, including hash-sources, and
'unsafe-inline' if you must.
2. In Plugins
plugin.rb with the
# plugin.rb extend_content_security_policy( script_src: ['https://domain.com/script.js', 'https://your-cdn.com/'], object_src: ['https://domain.com/flash-content'] )
3. In Themes and Components
list-type theme setting named
# settings.yml extend_content_security_policy: type: list default: "script_src: https://script-cdn.com/some/script.js|script_src: https://site1.com"
And this is what it will look like in the UI:
I can no longer access my site due to a bad CSP setting, what should I do?
You will need SSH access to your site, and open the Rails console with:
cd /var/discourse ./launcher enter app rails c
SiteSetting.content_security_policy_script_src = ""
This will reset your CSP setup back to default, and you should be able to get back to your site.