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

Failed to implement CSP Policy's for styles with CSP_NONCE token #54689

Open
KostiantynBorzik opened this issue Mar 4, 2024 · 5 comments
Open
Labels
area: core Issues related to the framework runtime area: security Issues related to built-in security features, such as HTML sanitation core: stylesheets cross-cutting: CSP
Milestone

Comments

@KostiantynBorzik
Copy link

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

core

Is this a regression?

No

Description

When implementing the CSP policy for styles, we decided to go with the CSP_NONCE token because we are caching index.html.
In order to implement this token we decided to use APP_INITIALIZER to receive a nonce token from the server before the application is loaded and use it afterwards.
For this, we wrote a simple nonce service that is responsible for getting and saving the token
image
After that we added this configuration to app.module.ts :

export function receiveNonce(nonceService: NonceService) {
  return () => {
    console.log('APP_INITIALIZER useFactory function start working');
    return nonceService.receiveNonce();
  }
}


Providers: [
NonceService,
      {
        provide: APP_INITIALIZER,
        useFactory: receiveNonce,
        deps: [NonceService, HttpClient],
        multi: true
      },
      {
        provide: CSP_NONCE,
        useFactory: (nonceService: NonceService) => {
          console.log('CSP_NONCE useFactory function start working');
          return nonceService.getNonce();
        },
        deps: [NonceService, HttpClient],
        multi: true
      }
]

image
Example of execution order.

And as a result we get incorrect behavior our СSP_NONCE is executed before APP_INITIALIZER for some reason. Because of this we can not get nonce token from the server, also useFactory does not work with asynchronous operations not anywhere except APP_INITIALIZER that's why we decided to use it.
Expected Behavior: CSP_NONCE token should work fine with factory approach

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

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

Node version - 18.16.0
Angular version - 16.2.12

Anything else?

No response

@JoostK
Copy link
Member

JoostK commented Mar 4, 2024

This is working as expected; a nonce must be available synchronously during startup.

What's the reasoning for fetching a nonce via the server anyway? Why not generate it locally in the client? That can't normally be done for scripts (as it's a catch 22) but if you have the ability to execute code anyway, then I don't see why you'd need a server to generate a nonce...

@pkozlowski-opensource pkozlowski-opensource added area: core Issues related to the framework runtime area: security Issues related to built-in security features, such as HTML sanitation core: stylesheets cross-cutting: CSP labels Mar 5, 2024
@ngbot ngbot bot added this to the needsTriage milestone Mar 5, 2024
@Peter-Zakharevich
Copy link

Peter-Zakharevich commented Mar 5, 2024

This is working as expected; a nonce must be available synchronously during startup.

What's the reasoning for fetching a nonce via the server anyway? Why not generate it locally in the client? That can't normally be done for scripts (as it's a catch 22) but if you have the ability to execute code anyway, then I don't see why you'd need a server to generate a nonce...

The reason why we're fetching a nonce it's because API side have a control over CSP policies (e.g. alowed chases, nonces e.t.c), Client side receive CSP policies via HTTP headers.
Are you suggesting to implement CSP policies for the styles on the Client side via meta tag ?
In that case, can we use injection of csp_nonce token for meta tag or we have to write some custom logic to populate csp policies there?

@Peter-Zakharevich
Copy link

Hi guys, any updates on this ?

1 similar comment
@Peter-Zakharevich
Copy link

Hi guys, any updates on this ?

@JoostK
Copy link
Member

JoostK commented Apr 4, 2024

If conceiving the nonce in the client is not an option for you, you can fetch it before bootstrapping Angular to account for the fact that CSP_NONCE has to be provided synchronously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime area: security Issues related to built-in security features, such as HTML sanitation core: stylesheets cross-cutting: CSP
Projects
None yet
Development

No branches or pull requests

4 participants