ATTENTION! Answer updated
UPDATED ANSWER (July 16th 2021)
The original solution posted below breaks Safari (MacOS) so we had to rollback and disable optimization.styles.inlineCritical.
Also, as commented by masterfloda below, the choice should be security over performance in most if not all cases.
My original thinking was that it would be difficult to exploit (unsafe-hashes), but I have not done a thorough InfoSec risk scoring on that.
There is a ticket open for this on the Angular repo - #20864.
I suggest you disable optimization.styles.inlineCritical for now and like and subscribe to that issue.
ORIGINAL ANSWER (June 9th 2021)
Yes, this issue was introduced by Angular v12's Styles optimization options (inlineCritical: true) which adds an onload event handler to the link tag for the main stylesheet as follows:
<link rel="stylesheet" href="styles.<hash>.css" media="all" onload="this.media='all'">
It can be solved without disabling inlineCritical or enabling 'unsafe-inline' in the CSP, as follows:
- Hash the contents of the onloadhandler:this.media='all'
- Report URI has a handy tool for that: Script And Style Hasher
- Add the hash to the script-srcdirective in the CSP
- For Chrome (but not for FF), you also need to add 'unsafe-hashes'to thescript-srcdirective, otherwise it blocks with the following log in the console: Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present