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

autoCSP and media="print" onload="this.media='all'" #29603

Closed
1 task
rgant opened this issue Feb 10, 2025 · 4 comments · Fixed by #29638
Closed
1 task

autoCSP and media="print" onload="this.media='all'" #29603

rgant opened this issue Feb 10, 2025 · 4 comments · Fixed by #29638

Comments

@rgant
Copy link

rgant commented Feb 10, 2025

Command

build

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

Setting "autoCsp": true in security configuration of angular.json breaks the stylesheet because it has JS onload to change the media type. Unsure why the media type change is happening, but I feel like I could do without it.

<meta http-equiv="Content-Security-Policy" content="script-src 'strict-dynamic' 'sha256-lxruP/GUT5SHo3Hi18U7zp9MDVNM5Zn9NDVhbptQFwA=' https: 'unsafe-inline';object-src 'none';base-uri 'self';style-src 'self' 'unsafe-inline';">

<link rel="stylesheet" href="styles-T5VOQUP7.css" media="print" onload="this.media='all'">

Minimal Reproduction

Example app reproduction is here: https://brainfry-app.web.app/login?next=%2Fprofile (For now, I will probably need to disable this setting shortly.)

https://github.com/rgant/brainfry/blob/c7ba05dec689d748363fc0c4febe4a1eec7109e3/angular.json#L37

Exception or Error

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'strict-dynamic' 'sha256-lxruP/GUT5SHo3Hi18U7zp9MDVNM5Zn9NDVhbptQFwA=' https: 'unsafe-inline'". Note that 'unsafe-inline' is ignored if either a hash or nonce value is present in the source list.`

I believe that this error message is just because Chrome is complaining about a fallback setup for the CSP policy and is not related to this issue, but included for completeness.

Your Environment

$ ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 19.1.6
Node: 20.18.1
Package Manager: npm 11.0.0
OS: darwin arm64

Angular: 19.1.5
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router
... service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1901.6
@angular-devkit/build-angular   19.1.6
@angular-devkit/core            19.1.6
@angular-devkit/schematics      19.1.6
@angular/cli                    19.1.6
@angular/fire                   19.0.0
@schematics/angular             19.1.6
rxjs                            7.8.1
typescript                      5.7.3
zone.js                         0.15.0

Anything else relevant?

No response

@rgant
Copy link
Author

rgant commented Feb 10, 2025

I guess this is related to optimization.styles.inlineCritical defaulting to true.

https://stackoverflow.com/a/67582075/41908

@dgp1130
Copy link
Collaborator

dgp1130 commented Feb 10, 2025

/cc @aaronshim

@aaronshim
Copy link
Contributor

@dgp1130 I vaguely remember that we may have had this discussion a long time ago, when we were first planning auto-CSP.

There might have been interest in deprecating the this.media = ‘all’, is this still of interest? If not, we have to also hash the inline event handlers and throw that hash in to every generated CSP.

@alan-agius4
Copy link
Collaborator

@aaronshim, yes, we discussed this before. We need to update https://github.com/angular/angular-cli/blob/main/packages/angular/build/src/utils/index-file/inline-critical-css.ts to use the external script instead of the inline handlers. Alternatively, hashing the handlers could also work.

Let me know if you need any help with this.

alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update enhances the `autoCsp` functionality to properly handle inlined critical CSS, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could cause CSP violations in certain configurations. With this fix, the mechanism correctly accounts for and integrates critical CSS while maintaining security.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update enhances the `autoCsp` functionality to properly handle inlined critical CSS, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could cause CSP violations in certain configurations. With this fix, the mechanism correctly accounts for and integrates critical CSS while maintaining security.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update enhances the `autoCsp` functionality to properly handle inlined critical CSS, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could cause CSP violations in certain configurations. With this fix, the mechanism correctly accounts for and integrates critical CSS while maintaining security.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update enhances the `autoCsp` functionality to properly handle inlined critical CSS, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could cause CSP violations in certain configurations. With this fix, the mechanism correctly accounts for and integrates critical CSS while maintaining security.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update enhances the `autoCsp` functionality to properly handle inlined critical CSS, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could cause CSP violations in certain configurations. With this fix, the mechanism correctly accounts for and integrates critical CSS while maintaining security.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 13, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 14, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 14, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 14, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 14, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 14, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue Feb 14, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes angular#29603
dgp1130 pushed a commit that referenced this issue Feb 14, 2025
This update improves the handling of inlined critical CSS to align with `autoCsp`, ensuring compliance with Content Security Policy (CSP) directives. Previously, inlined styles could trigger CSP violations in certain configurations. With this fix, critical CSS is inlined in a way that maintains security while supporting `autoCsp`.

Closes #29603

(cherry picked from commit e6deb82)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants