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

Cannot use nonce in nuxt-security:headers hook #407

Closed
jackpercy-acl opened this issue Mar 28, 2024 · 6 comments · Fixed by #408 or #399
Closed

Cannot use nonce in nuxt-security:headers hook #407

jackpercy-acl opened this issue Mar 28, 2024 · 6 comments · Fixed by #408 or #399
Labels
bug Something isn't working

Comments

@jackpercy-acl
Copy link

jackpercy-acl commented Mar 28, 2024

Version

nuxt-security: 1.2.2
nuxt: 3.11.1

Reproduction Link

https://stackblitz.com/edit/nuxt-security-missing-nonce?file=server%2Fplugins%2Fsecurity.ts

Steps to reproduce

  1. Configure the nuxt-security:headers hook to change CSP with Runtime Config as per docs
  2. Include a 'nonce-{{nonce}}' value in one of the CSP values
export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('nuxt-security:ready', async () => {
    await nitroApp.hooks.callHook('nuxt-security:headers', {
      route: '/**',
      headers: {
        contentSecurityPolicy: {
          'script-src': [
            "'self'",
            'https:',
            "'unsafe-inline'",
            "'strict-dynamic'",
            "'nonce-{{nonce}}'",
          ],
        },
      },
    });
  });
});

What is Expected?

The CSP header should be transformed to include the actual nonce value.

What is actually happening?

The CSP value contains the exact string 'nonce-{{nonce}}'

image

By adding some logging into the nuxt-security nitro plugins files, you can see that the custom CSP from the hook is being registered. However, when the plugin 99-cspSsrNonce.ts runs, the CSP value it resolves and replaces the nonces in, is the default/nuxt.config.ts CSP.

const csp = security.headers.contentSecurityPolicy

This is meaning we cannot do a combination of using runtime config in the CSP and nonces.

@jackpercy-acl jackpercy-acl added the bug Something isn't working label Mar 28, 2024
@Baroshem
Copy link
Owner

Hey, thanks for reporting this.

@vejja @huang-julien do you have some ideas about it? :)

@vejja
Copy link
Collaborator

vejja commented Mar 28, 2024

@jackpercy-acl : does it work if you don't use the runtime config and instead use the default nuxt.config.ts ?

@jackpercy-acl
Copy link
Author

@vejja yes, it works fine if configuring only in nuxt.config.ts

image

image

@vejja
Copy link
Collaborator

vejja commented Mar 28, 2024

Thanks
@huang-julien looks like the runtime config is not picked up here ?

@huang-julien
Copy link
Contributor

I'm not sure about that, I can't reproduce it
image

I may have to try reproduce it locally

@jackpercy-acl
Copy link
Author

@huang-julien if you check the headers on the returned doc rather than logging render:response you will see the unset nonce.

I assume this beforeResponse hook is run after render:response which is when the CSP from the nuxt-security:headers hook is set on the response.

nitroApp.hooks.hook('beforeResponse', (event) => {

@Baroshem Baroshem linked a pull request Apr 3, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants