Skip to content

Content Security Policy 'NONCE' value is not applied to all scripts (when using 'ngCspNonce' approach) #27765

Closed as not planned
@jlfrances

Description

@jlfrances

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

About Content Security Policy in Angular docs

In the Angular official docs (under section Content Security Policy) it says how to implement it in the Angular application.
The first approach is using an attribute called 'ngCspNonce':

Set the ngCspNonce attribute on the root application element as . Use > this approach if you have access to server-side templating that can add the nonce both to the header and the index.html when > constructing the response.
https://angular.dev/best-practices/security#content-security-policy

Trying 'nonce' with a hardcoded value

To test this, I set a hardcoded nonce (fakeNonce) to simulate what would be the auto-generated one.

So first I set it up in the HTTP Response header for the angular.json file (serve > options > headers):
image

☝️ Note that I'm using Content-Security-Policy-Report-Only instead of Content-Security-Policy so my app is not crashing when the policy is not complied. More info here: https://content-security-policy.com/report-only/

Then I added the nonce in the index.html :
image

Errors

The issue comes when I run the application: there are two <script> tags in the index.html that are added on runtime, but the nonce="fakeNonce" attribute-value is missing:
image

In the DevTools console I can see two errors that I understand are related to that:
image

I know all other inline styles and scripts work because the HTTP header is being returned:
image

Please provide the exception or error you saw

[Report Only] Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' 'nonce-fakeNonce'". Either the 'unsafe-inline' keyword, a hash ('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='), or a nonce ('nonce-...') is required to enable inline execution.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 17.3.7
Node: 20.5.1
Package Manager: npm 9.8.0
OS: win32 x64

Angular: 17.3.8
... animations, cdk, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1703.7
@angular-devkit/build-angular   17.3.7
@angular-devkit/core            17.3.7
@angular-devkit/schematics      17.3.7
@angular/cli                    17.3.7
@schematics/angular             17.3.7
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions