sweetalert icon indicating copy to clipboard operation
sweetalert copied to clipboard

Content Security Policy Compatibility

Open adhip94 opened this issue 6 years ago • 10 comments

In my project I have given the following meta tag (as shown in the screenshot followed):

<meta http-equiv="Content-Security-Policy" content="default-src http:">

image

When I load the page I get the following errors in my console:

image

Anyone faced this issue before?

Also BTW the name of my js file is sweetalert.min.js but the file content contains the unminified js!

adhip94 avatar Oct 11 '18 09:10 adhip94

You need to update the Content-Security-Policy value to accommodate the style-src directive which must contain 'unsafe-inline' (careful to quote around) to execute the inline styles injected.

maksbd19 avatar Oct 13 '18 05:10 maksbd19

It's definitely not ideal to force developers to use the unsafe-inline policy for CSS. We should maybe find a better way to handle this.

t4t5 avatar Oct 27 '18 16:10 t4t5

One way is to not to import the css file inside your code and make it available as a separate file that can be imported within the developer code. something like import 'sweetalert/sweetalert.css This way it would be the responsibility of developer to make sure it does not violate CSP

kikoanis avatar Dec 15 '18 03:12 kikoanis

It's got (almost) nothing to do with where the CSS file is (as long as the CSS resides on the same domain). Sweetalert injects inline CSS, which any CSP worth having will not allow unless there's a nonce/hash value. It's causing me no end of pain this morning; I may have to unwire it from my project, which is a shame, because I really like(d) it.

kvn1234 avatar Feb 14 '19 15:02 kvn1234

Have there been any updates on this? I am having the same issue; just wondering what the best way to get around this is?

sleepingMantis avatar May 01 '19 12:05 sleepingMantis

The workaround is to add the hashes to your Content-Security-Policy header.

Something like

style-src 'self' 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=' 'sha256-wTr/bct+DGhJCU0mVZOm9Z1v99oBZrIu4VCMYQJWdfI=';

Not ideal but it is better than unsafe-inline. Issue is that if the files change, the hashes will no longer match and the browser will reject them.

The ideal situation would be to extract the css into it's own file and host it on a CDN. That way we can white list the file.

trevor-hackett avatar Jul 08 '19 19:07 trevor-hackett

It's got (almost) nothing to do with where the CSS file is (as long as the CSS resides on the same domain). Sweetalert injects inline CSS, which any CSP worth having will not allow unless there's a nonce/hash value. It's causing me no end of pain this morning; I may have to unwire it from my project, which is a shame, because I really like(d) it.

same here.

rjcabrera158 avatar Oct 15 '19 07:10 rjcabrera158

I can get two inline style hashes from sweetalert.min.js in Chrome. When added to the CSP header, Chrome doesn't complain anymore. But Firefox and Safari still complain, and I am not able to find the proper hash. These browsers don't give any hints. Firefox: Content Security Policy: The page’s settings blocked the loading of a resource at inline (“style-src”) Safari: Refused to apply a stylesheet because its hash, its nonce, or 'unsafe-inline' does not appear in the style-src directive of the Content Security Policy.

Janaka-Steph avatar May 26 '20 13:05 Janaka-Steph

The workaround is to add the hashes to your Content-Security-Policy header.

Something like

style-src 'self' 'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=' 'sha256-wTr/bct+DGhJCU0mVZOm9Z1v99oBZrIu4VCMYQJWdfI=';

Not ideal but it is better than unsafe-inline. Issue is that if the files change, the hashes will no longer match and the browser will reject them.

The ideal situation would be to extract the css into it's file and host it on a CDN. That way we can white list the file.

Can you please elaborate on how to do this "Extract CSS into a file"?

aditiagarwal34550 avatar May 17 '21 23:05 aditiagarwal34550

The package has a js version without embedding. My example is for laravel. import sweetalert from "sweetalert2/dist/sweetalert2.min"; - not css. and app.scss @import '~sweetalert2/dist/sweetalert2.min.css'; Hope this helps someone

kuzhak avatar Nov 02 '21 16:11 kuzhak