# Mitigate XSS Attacks with Content Security Policy

**URL:** https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243
**Category:** Site Management
**Tags:** how-to, content-security-policy
**Created:** [December 14, 2018, 4:38pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243 "2018-12-14T16:38:20Z")
**Posts on this page:** 13
**Page:** 2

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [July 19, 2021, 7:12pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/38 "2021-07-19T19:12:44Z")

</div>

> [@Falco](#):
>
> This directive will be enabled by default in the next release cycle.

As promised, this feature was just enabled by default: [CSP Frame Ancestors enabled by default](https://meta.discourse.org/t/csp-frame-ancestors-enabled-by-default/197615)

---

<div class="post-metadata">

### Author: ![Fabrice](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fabrice/32/265087_2.png) [@Fabrice](https://meta.discourse.org/u/Fabrice)
#### Post date: [July 5, 2022, 8:06pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/40 "2022-07-05T20:06:19Z")

</div>

Hello,  
I am currently setting up Google Adsense on my website and seems I am missing the “Enable CSP violation report collection at /csp\_reports” settings.. I am currently self-hosting Discourse on version 2.9.0.beta6

> [@pmusaraj](#):
>
> ![image](https://global.discourse-cdn.com/meta/original/3X/3/5/35741fb9a1b5d9bd8abcb63e079894e97a25232f.png)

above, in quote, a screenshot of the setting I am missing

 ![image](https://global.discourse-cdn.com/meta/original/4X/a/2/c/a2c2e4cb0ad6013e211fac75c72aa1709e28fbb1.png)  
above, my setting page for CSP with the “Enable CSP violation report collection at /csp\_reports” missing.

Any help is appreciated ! And please tell me if I set-up the settings I have correctly for Adsense, I never used it before 👀 !

---

<div class="post-metadata">

### Author: ![pmusaraj](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pmusaraj/32/119489_2.png) [@pmusaraj](https://meta.discourse.org/u/pmusaraj)
#### Post date: [July 13, 2022, 3:55pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/41 "2022-07-13T15:55:31Z")

</div>

Apologies, my earlier replies here are outdated because as of ~ a month ago we made `content_security_policy_collect_reports` a hidden setting. You can still enable it but you need to do it via the Rails console, as in:

```plaintext
./launcher enter app
...
rails c 
...
SiteSetting.content_security_policy_collect_reports = true

```

Note that this is very noisy, I highly recommend not going down this route at all and simply enabling CSP and navigating the site with the browser console open using multiple browsers (Chrome, Firefox, Safari). You’ll find most issues that way. And with the configuration you have, you’re basically allowing almost all that CSP protects against anyway, so you shouldn’t have any need for the reports.

---

<div class="post-metadata">

### Author: ![ReenigneArcher](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/reenignearcher/32/278266_2.png) [@ReenigneArcher](https://meta.discourse.org/u/ReenigneArcher)
#### Post date: [October 25, 2022, 10:28pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/42 "2022-10-25T22:28:04Z")

</div>

Maybe I am missing something, but I do not see these settings in the settings UI.

- `content_security_policy`
- `content_security_policy_report_only`
- `content_security_policy_collect_reports` (I see that is hidden now)
- `content_security_policy_script_src`

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/0/3/d0318f0aff719551bc44d6f2b1f19f4112417ae3.png)

Are these options available to hosted instances? I didn’t see any mention of that being a limitation in the original post or comments.

_Edit:_ Also attempted to set the security policy through a theme.

Does not seem to be working as instructed in original post.

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/e/8/7e84e27a964017438c6b95f35ae5f04d1596aed6.png)

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/7/e/77e194c38c755a9d3aefcd243031cb9f3861e40e.png)

I’m assuming the hosted plan I’m on doesn’t allow this, even when done via a theme or theme component?

Or maybe I am just doing something totally wrong.

---

<div class="post-metadata">

### Author: ![jkfran](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jkfran/32/283020_2.png) [@jkfran](https://meta.discourse.org/u/jkfran)
#### Post date: [November 23, 2022, 7:16pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/47 "2022-11-23T19:16:15Z")

</div>

> Currently, Discourse ships a CSP Level 2 policy with the following directives by default:
> 
> - `base-uri` restricts the URLs for `<base>` element

Is there any option to override the default value for the `base-uri` header?

---

<div class="post-metadata">

### Author: ![MichaIng](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/michaing/32/251089_2.png) [@MichaIng](https://meta.discourse.org/u/MichaIng)
#### Post date: [January 28, 2023, 11:59am UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/48 "2023-01-28T11:59:36Z")

</div>

Where are `<base>` elements actually used in discourse?

---

<div class="post-metadata">

### Author: ![hello-smile6](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hello-smile6/32/304628_2.png) [@hello-smile6](https://meta.discourse.org/u/hello-smile6)
#### Post date: [April 28, 2023, 1:50am UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/49 "2023-04-28T01:50:36Z")

</div>

> [@adrelanos](#):
>
> push ‘none’;

Would that break Discourse push notifications?

---

<div class="post-metadata">

### Author: ![adrelanos](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/adrelanos/32/121409_2.png) [@adrelanos](https://meta.discourse.org/u/adrelanos)
#### Post date: [May 5, 2023, 10:23am UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/50 "2023-05-05T10:23:32Z")

</div>

Could be. Might need a more suitable setting.

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [June 12, 2023, 8:35am UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/51 "2023-06-12T08:35:01Z")

</div>

A post was split to a new topic: [What’s the suggested method to use secure images?](https://meta.discourse.org/t/what-s-the-suggested-method-to-use-secure-images/268010)

---

<div class="post-metadata">

### Author: ![Hyan](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hyan](https://meta.discourse.org/u/Hyan)
#### Post date: [June 12, 2023, 12:55pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/52 "2023-06-12T12:55:10Z")

</div>

Hi there,  
May I know how to bypass the issue below? I have added the script invoking a new Function() expression into content\_security\_policy\_script\_src, but my browser console still complains below error. Where I can exclude the unsafe-eval? Thanks!

```plaintext
Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive:

```

---

<div class="post-metadata">

### Author: ![Hyan](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hyan](https://meta.discourse.org/u/Hyan)
#### Post date: [June 12, 2023, 1:22pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/53 "2023-06-12T13:22:53Z")

</div>

Apologies. Please ignore my previous post if my below remarks are correct. Just know through above posts I can set ‘unsafe-eval’ directly to content\_security\_policy\_script\_src, and there’s no JS error reported in console any more.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 12, 2023, 2:03pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/54 "2023-06-12T14:03:06Z")

</div>

Maybe have a look at the `content security policy script src ` site setting?

---

<div class="post-metadata">

### Author: ![Hyan](https://avatars.discourse-cdn.com/v4/letter/h/5fc32e/32.png) [@Hyan](https://meta.discourse.org/u/Hyan)
#### Post date: [June 13, 2023, 10:50pm UTC](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243/55 "2023-06-13T22:50:33Z")

</div>

Yes. As said in previous replies ‘unsafe-eval’ can be added directly to content\_security\_policy\_script\_src, I thought only URL base or full URL allowed to add.

[Previous page](https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243.md?page=1)
