-
Notifications
You must be signed in to change notification settings - Fork 12k
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
inlineCritical CSS asynchronous loading method breaks with CSP #20864
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Found how to disable the inlineCritical styles "optimization": true put "optimization": {
"scripts": true,
"styles": {
"minify": true,
"inlineCritical": false
},
"fonts": true
}, |
We can probably solve this by changing the preload strategy of critters from Let me reach out to the Chrome team so see if there are any drawbacks of doing so. |
@alan-agius4 I think the "body" mode could work - it injects styles at the |
@developit, thanks for the input. I had a chat with @janicklas-ralph, and mentioned that the default is idea although in some cases it can break CSS because of different CSS ordering. I’ll investigate a bit more. |
@alan-agius4 another option would be to have the JS bundle itself flip those preloads to stylesheets (as a backup). It could scan for the preload/disabled [].forEach.call(document.querySelectorAll('link[rel="stylesheet"][media="print"]'), n => {
n.onload = () => n.media='';
n.href += ''; // onload if not already loading
}); |
An interesting article with an overview of this concept : |
I tried @tiberiuzuld's solution but didn't get it working. Any other workarounds? |
Hi @pacocom, it looks like the same problem. Maybe you need to apply the fix to other config areas of you angular config. Look for other |
It works now. |
@alan-agius4 and maybe others.. What is the status of this issue? What i understand is that we have to disable the inline css feature, OR make our CSP more insecure. Both dont sound like great solutions. Will there be a good fix to not require us to either sacrifice perf or security? If not, will there be documentation for angular with a CSP guide on how to create a Security performance balance? |
Just disable inline CSS. External stylesheets worked for dial-up internet, IDK why people insist on complicating the most simple things when speeds are 100x what they used to be. EDIT: Enjoy your bugs. |
@sander1095 One work-around is of course to add |
Can somebody point to the logic which extracts the 'critical css' and then renders it as inline? I mean maybe it's possible to customize that to include CSP nonces you have defined somewhere. |
I am trying to apply fix in Angular 13. Please post angular.json with fix. I changed it as suggested. But, I still have |
@rickz21 try putting the optimizations in the build > options portion of the JSON e.g.
|
@jpduckwo thank you trying to help me. I did paste in the lines you posted within build.options but it didn't work for me. Angular still inserted a style attribute into my page's body tag. |
I read |
@jdavidhermoso I tried that configuration. It didn't stop Angular from adding inline styling to my body tag. |
Hey @rickz21 the style tag on the body isn't related to this issue. This one is about angular automatically inlining critical css. It sounds like your styles on the body could be coming from something else. Maybe it's another dependency? Might be worth posting a new issue if you can create minimal reproduction |
@jpduckwo Angular is the only dependency. I will do as you suggest. I will try to create a minimal demonstration. |
Hello All, In my case, my project uses Angular 13 latest; and this issue was happening only on PROD build. Not on Dev/Staging builds. I searched angular documentation; no where it is mentioned why only production grade build gets affected? Any clarity would enlighten me on this. Thanks |
@ZenwalkerD check your angular.json, your settings for dev/staging will have a different optimization setting than prod. That's my guess :) |
Thank's @tiberiuzuld 🎉 I used your solution #20864 (comment) for a Chrome extension created with [email protected]. I also needed the code generated by the I put the following "configurations": {
"production": {
"...": "...",
"optimization": {
"scripts": true,
"styles": {
"minify": true,
"inlineCritical": false
},
"fonts": true
}
},
"development": {
"...": "..."
}
}, |
The changes to angular.json are to disable inline critical CSS, which breaks without "script-src 'unsafe-inline'" (which we obviously don't want to have). See angular/angular-cli#20864 for details.
Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. See: #24903 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Bug Report
Affected Package
@angular/cli
Is this a regression?
Nay
Description
The method used for inlining critical css and asynchronously loading it, breaks and doesn't load the external stylesheet when you have a content security policy that doesn't include
script-src 'unsafe-inline'
. As the name suggests this isn't a very secure way of operating for various reasons such as script injection.You can fix by disabling inlineCritical in the optimizations - however maybe there is a better way to load the styles, maybe in a JS file?
<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">
It's the
onload="this.media='all'"
that breaks itMinimal Reproduction
https://github.com/jpduckwo/ng12-csp-issue
run:
ng serve
Exception or Error
Refused to execute inline event handler because it violates the following Content Security Policy directive: "default-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.
Your Environment
Angular Version:
The text was updated successfully, but these errors were encountered: