nuxt-security: Current cspSsrNonce plugin can result in security problem in some cases

Environment

- Operating System: Linux
- Node Version:     v22.11.0
- Nuxt Version:     3.14.1592
- CLI Version:      3.17.2
- Nitro Version:    2.10.4
- Package Manager:  yarn@1.22.22
- Builder:          -
- User Config:      default
- Runtime Modules:  nuxt-security@2.1.5
- Build Modules:    -

Nuxt Security Version

v2.1.5

Default setup used?

Yes, the bug happens even if the security option is not customized

Security options


Reproduction

https://github.com/maple3142/nuxt-security-nonce-bug-repro

  1. yarn install && yarn build
  2. node .output/server/index.mjs
  3. Visit http://localhost:3000/?statusMessage=1&html=%3Cdiv+id=%22%3Cscript%3E%3Cscript%3Ealert(origin)%3C/script%3E%22%3E%3C/div%3E from browser

Description

The browser would pop an alert when visiting that url (so XSS happens here), despite https://github.com/maple3142/nuxt-security-nonce-bug-repro/blob/master/pages/index.vue does sanitize the incoming html using DOMPurify.

The main problem is that nuxt security incorrectly add nonces to script tags using simple string replacement here: https://github.com/nuxt-modules/security/blob/70cd67a4690b53e9167c210e3e020faed901355d/src/runtime/nitro/plugins/40-cspSsrNonce.ts#L53-L73 . And v-html can result in unescaped <script> to appear in rendered html in SSR mode.

I think nuxt-security should not process html in from v-html, and should not naively use string replacement to do that because it may accidentally replace fake script tags inside element attributes.

Additional context

No response

Logs


About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 16 (5 by maintainers)

Most upvoted comments

However it would still be nice to modify our regex, would it be extremely difficult ?

This would depend, I’d have to look into it, because it can be in the form of

<div
id="<script></script>"
/>

As well as

<div id="<script></script>" />

It definitely complicates the parsing process to a degree (which will in turn increase resource demand on constrained environments), but I’d have to figure out to which degree that would be exactly.