Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use angular-material with CSP?

Our Content Security Policy does not allow unsafe-inline for styles.

When I try to use angular-material in our application, it won't work right (missing styles) and I get a lot of errors logged in the DevTools. From what I can tell, this is due to dynamically generated CSS being injected into the page.

I'm already including ngCsp, but it has no noticeably effect on this issue.

like image 798
Oliver Salzburg Avatar asked Nov 20 '25 21:11

Oliver Salzburg


1 Answers

According to the documentation at AngularJS Material the solution must be:

angular
.module("app", ['ngMaterial'])
.config(['$mdThemingProvider', function (themeProv) {
    themeProv.setNonce('randomString')
}])

The nonce to be added as an attribute to the theme style tags. Setting a value allows the use of CSP policy without using the unsafe-inline directive.

like image 175
Sandman Avatar answered Nov 24 '25 06:11

Sandman