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

Add nonce property to script tags #1645

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

branberry
Copy link

@branberry branberry commented May 15, 2024

Resolves #1554

@branberry branberry mentioned this pull request May 15, 2024
@brillout
Copy link
Member

👍

  • The crypto module should only be loaded if needed, and by using @brillout/import to avoid crypto from being bundled by bundlers.
  • What's wrong with using Math.random() instead? I wonder how Math.random() can be a security hazard.

@branberry
Copy link
Author

Thanks for the feedback! I'll update the import to not include crypto as a part of the bundle. Based on the MDN docs, that the nonce value needs to be cryptographically secure:

Allowing all inline scripts is considered a security risk, so it's recommended to use a nonce-source or a hash-source instead. To allow inline scripts and styles with a nonce-source, you need to generate a random nonce value (using a cryptographically secure random token generator) and include it in the policy. It is important to note, this nonce value needs to be dynamically generated as it has to be unique for each HTTP request:

Based on the MDN docs for Math.random, it does not generate cryptographically secure values.

@branberry branberry marked this pull request as ready for review May 16, 2024 00:39
@brillout
Copy link
Member

brillout commented May 16, 2024

  • 👍 Good to know, how about adding a comment about this in the code?
  • I think we need to add the nonce to all other <script> tags?
  • How about we set the generated random nonce value to pageContext.nonce? Is there a use case for that? E.g. if the user wants to inject a script to the HTML?
  • Maybe we can also let the user set a pageContext.none value (Vike wouldn't use crypto then but take that user-defined value instead), but I don't think there is a use case for that?
  • How about we load the crypto module only if strictly necessary , e.g. only ifpageContext.nonce is set?
  • Up for adding docs? Maybe creating a new page vike.dev/CSP?
  • I'm thinking: should we maybe always generate a nonce by default? How much value does the nonce add? Is that worth it?

@branberry
Copy link
Author

Thank you for the feedback! I don't have answers for you at the moment about whether or not there is value in generating a nonce by default. I'll also work on adding the nonce to the other script tags as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CSP nonce support
2 participants