Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Security Policy Compatibility #869

Open
adhip94 opened this issue Oct 11, 2018 · 10 comments
Open

Content Security Policy Compatibility #869

adhip94 opened this issue Oct 11, 2018 · 10 comments

Comments

@adhip94
Copy link

adhip94 commented Oct 11, 2018

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!

@maksbd19
Copy link

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.

@t4t5
Copy link
Owner

t4t5 commented Oct 27, 2018

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.

@kikoanis
Copy link

kikoanis commented Dec 15, 2018

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

@kvn1234
Copy link

kvn1234 commented Feb 14, 2019

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.

@sleepingMantis
Copy link

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

@trevor-hackett
Copy link

trevor-hackett commented Jul 8, 2019

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.

@rbcabreraUPM
Copy link

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.

@Janaka-Steph
Copy link

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.

@aditiagarwal34550
Copy link

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"?

@kuzhak
Copy link

kuzhak commented Nov 2, 2021

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

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

No branches or pull requests