Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

ngSanitize triggers CSP alert/report in Firefox #16463

Closed
1 of 3 tasks
thesam opened this issue Feb 20, 2018 · 9 comments
Closed
1 of 3 tasks

ngSanitize triggers CSP alert/report in Firefox #16463

thesam opened this issue Feb 20, 2018 · 9 comments

Comments

@thesam
Copy link

thesam commented Feb 20, 2018

I'm submitting a ...

  • bug report
  • feature request
  • other

Current behavior:

If ngSanitize is added as a module dependency and a Content-Security-Policy is set that does not allow inline styles then Firefox shows the following message:

Content Security Policy: The page’s settings observed the loading of a resource at self (“default-src”). A CSP report is being sent.

Our CSP looks like this:

Content-Security-Policy-Report-Only: default-src 'self'; report-uri /foo

If ngSanitize is removed from the module dependencies then the CSP message disappears as well.

Expected / new behavior:

ngSanitize should work in Firefox without triggering CSP alerts, at least if the "ng-csp" mode is enabled.

Minimal reproduction of the problem with instructions:

  1. Set the Content-Security-Policy to: default-src: 'self'
  2. Add 'ngSanitize' as a module dependency.

AngularJS version: 1.6.9

Browser: Firefox 60.0a1 and 59.0b10

Anything else:
I guess the following code triggers the CSP alert, since it adds an inline <style> tag.
// Check for the Firefox bug - which prevents the inner img JS from being sanitized inertBodyElement.innerHTML = '<svg><p><style><img src="</style><img src=x onerror=alert(1)//">';
From: https://github.com/angular/angular.js/blob/master/src/ngSanitize/sanitize.js
Line 443-444

@petebacondarwin
Copy link
Contributor

@thesam do you have any suggestion for how we could test for this FF bug (which is a security vulnerability) without the inline style?

@thesam
Copy link
Author

thesam commented Mar 22, 2018

If I understand correctly this is how it works right now:

  1. If Safari bug exists: Use getInertBodyElement_XHR
  2. Else if Firefox bug exists: Use getInertBodyElement_DOMParser
  3. Else: Use getInertBodyElement_InertDocument

Maybe it could be done like this instead?

  1. If Safari bug exists: Use getInertBodyElement_XHR
  2. Else if DOMParser is available: Use getInertBodyElement_DOMParser
  3. Else if Firefox bug exists: fail/throw/abort!
  4. Else: Use getInertBodyElement_InertDocument

This way, the Firefox CSP alert will not be triggered if DOMParser is available.

I guess this would mean that most browsers would use DOMParser instead of InertDocument. Are there any negative side effects of using DOMParser instead of InertDocument?

@petebacondarwin
Copy link
Contributor

I think I remember that inert document approach is faster and more secure, so I would be wary of changing it like this. But... perhaps we could change it to this approach if ng-csp was set?

@oliversalzburg
Copy link
Contributor

This now also triggers an error in Chrome.

@peruukki
Copy link
Contributor

peruukki commented Apr 9, 2020

@thesam do you have any suggestion for how we could test for this FF bug (which is a security vulnerability) without the inline style?

I went through the details I could find on this:

It doesn't seem that the inline style tag is relevant; if I replace it with span, the bug is still detected in Firefox (and not in Chrome or Safari). I verified it by adding a console.log after if (inertBodyElement.querySelector('svg img')) { (in the Angular.js code) and if (this.inertBodyElement.querySelector && this.inertBodyElement.querySelector('svg img')) { (in the Angular code).

Also the related unit test description is "should not allow JavaScript hidden in badly formed HTML to get through sanitization (Firefox bug)", and none of the related discussions really mention the style tag. The bug is more related to the HTML structure than the actual tags if I understood correctly (though some tags like img may still be relevant).

Now that Angular.js is in Long Term Support, would it still be worthwhile to fix this? I guess this doesn't really count as a "security flaw".

@crhistianramirez
Copy link

I would argue that it does count as a security flaw because now my only course of action is to effectively cripple my content security policy by adding 'unsafe-inline'

peruukki added a commit to peruukki/angular.js that referenced this issue Apr 14, 2020
…ection

Inserting a `style` tag causes a CSP violation when using a strict CSP that
doesn't allow inline styles. The `style` tag doesn't seem relevant for the
Firefox bug detection, and can be replaced with a `span`, see issue angular#16463
for more details.

The related unit test in `sanitizeSpec.js`, "should not allow JavaScript
hidden in badly formed HTML to get through sanitization (Firefox bug)", is
left untouched and still uses `style` to assert that the behavior hasn't
changed in the original scenario.

Fixes angular#16463.
@peruukki
Copy link
Contributor

Thanks for your input @crhistianramirez, I went ahead and created a pull request, let's see how it goes. 🙂

peruukki added a commit to peruukki/angular.js that referenced this issue Apr 16, 2020
…ection

Default to using DOMParser if it is available and fall back to
createHTMLDocument if needed. This is the approach suggested in the
related pull request angular#17013 and used by DOMPurify too. It also safely
avoids using an inline style tag that causes CSP violation errors if inline
CSS is prohibited.

The related unit tests in `sanitizeSpec.js`, "should not allow JavaScript
execution when creating inert document" and "should not allow JavaScript
hidden in badly formed HTML to get through sanitization (Firefox bug)", are
left untouched to assert that the behavior hasn't changed in those scenarios.

Fixes angular#16463.
@Splaktar
Copy link
Member

Any plans to take this off of the Backlog - won't fix Milestone?

petebacondarwin pushed a commit that referenced this issue Jun 11, 2020
If `ngSanitize` is added as a module dependency and a Content-Security-Policy
is set that does not allow inline styles then Firefox and Chrome show the
following message:

> Content Security Policy: The page’s settings observed the loading of a
resource at self (“default-src”). A CSP report is being sent.

This message is caused because AngularJS is creating an inline style tag
to test for a browser bug that we use to decide what sanitization strategy
to use, which causes CSP violation errors if inline CSS is prohibited.

This test is no longer necessary, since the `DOMParser` is now safe to use
and the `style` based check is redundant.

In this fix, we default to using `DOMParser` if it is available and fall back
to `createHTMLDocument()` if needed. This is the approach used by DOMPurify
too.

The related unit tests in `sanitizeSpec.js`, "should not allow JavaScript
execution when creating inert document" and "should not allow JavaScript
hidden in badly formed HTML to get through sanitization (Firefox bug)", are
left untouched to assert that the behavior hasn't changed in those scenarios.

Fixes #16463.
@petebacondarwin
Copy link
Contributor

1.8.1 see https://github.com/angular/angular.js/blob/master/CHANGELOG.md#181-mutually-supporting-2020-09-30

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants