Skip to content

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

Closed

Description

@rgant

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

Activity

  1. rgant commented on Feb 10, 2025

    @rgant
    Author

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

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

  2. dgp1130 commented on Feb 10, 2025

    @dgp1130
    Collaborator
  3. aaronshim commented on Feb 11, 2025

    @aaronshim
    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.

  4. alan-agius4 commented on Feb 11, 2025

    @alan-agius4
    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.

  5. added 14 commits that reference this issue on Feb 13, 2025
    bd0c2fc
    e8b3b5b
    ae31443
    bffc752
    4dbb4de
    84366d7
    c2e79f9
    90810ef
    29c6689
    68e78ec
    0fde9a5
    83883d9
    d0e3f12
    4bac5d8
  6. added a commit that references this issue on Feb 14, 2025
    b8f7952
  7. added a commit that references this issue on Feb 14, 2025
    e6deb82
  8. angular-automatic-lock-bot commented on Mar 17, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    angular/build:applicationarea: @angular/buildfreq1: lowseverity3: brokentype: bug/fix

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions