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

Start adding Nonce documentation #1833

Merged
merged 3 commits into from
Feb 25, 2018

Conversation

EugeneHlushko
Copy link
Member

Add basic nonce documentation.

Fixes #1802

@quantizor
Copy link

Looking good! A few notes to add to the docs:

  1. The nonce should always be a base64-encoded string.

  2. A corresponding header (Content-Security-Policy) or meta tag (<meta http-equiv="Content-Security-Policy" ...>) needs to be sent with the document to instruct the browser to enable CSP.

This article goes into a lot of great detail on the subject: https://www.troyhunt.com/locking-down-your-website-scripts-with-csp-hashes-nonces-and-report-uri/ and MDN also has a great article.

It'd also be good to include an example of a CSP that allows loading JS from a trusted CDN, since that's a common use case for FE projects:

Content-Security-Policy: default-src 'self' trusted.cdn.com

@EugeneHlushko
Copy link
Member Author

I've added more links and useful information, thank for nice catch @probablyup

cc @TheLarkInn can anyone review docs PRs they seem to hang in the air :(

@devdigital
Copy link

How is this intended to be used when a nonce is created per request? Is this just a static nonce at build time?

@devdigital
Copy link

devdigital commented Feb 22, 2018

If this is a global variable checked at run-time then you just need to set window.__webpack_nonce__ before your output webpack bundle executes on the page. This isn't very obvious from the current docs - the term entry script and example showing the React import could be misread as a build time setting.

@quantizor
Copy link

quantizor commented Feb 22, 2018 via email

@devdigital
Copy link

What does webpack do with the nonce value? as it appears this var is also used by some CSS-in-JS libs at runtime when they add inline styles etc.

@quantizor
Copy link

quantizor commented Feb 22, 2018 via email

@devdigital
Copy link

Ok, so would the guidance be to reduce the scope of the static nonce to just webpack and those libraries (e.g. glamor, styled-components) that use it, and use another nonce (created per request) for other inline scripts? and therefore use multiple nonce sources in the content security policy?

@quantizor
Copy link

quantizor commented Feb 22, 2018 via email

@devdigital
Copy link

devdigital commented Feb 22, 2018

ok, it sounds like it isn't required if you're able to share a value between server and client (for cases where you may be adding other inline scripts server side). I will test it out. Confirmation that it is a run-time variable has helped, thanks - I still think the example in the docs here can make that clearer.

Copy link
Collaborator

@skipjack skipjack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made a few grammar and formatting tweaks. Aside from that this looks like a great start. I think maybe we need to add a little more context about the connection between CSPs and nonce as, to someone who's unfamiliar with the topic, it's still a little unclear. I think this can be done in a follow-up PR though, signing off on it for now and will merge soon.

@skipjack skipjack merged commit eec2854 into webpack:master Feb 25, 2018
@frontendphil
Copy link

frontendphil commented Mar 27, 2018

@skipjack @probablyup @EugeneHlushko is someone working on a follow-up? Because currently, the documentation leaves me with more questions than answers. I think @devdigital raised some important questions. Where can I put that __webpack_nonce__ so that webpack can pick it up? It looks like a build-time config even though it should be something that I could inject at least on every page load. What are the hooks, mechanisms, etc.?

@EugeneHlushko
Copy link
Member Author

I think this can be achieved using an SSR set-up, but probably not out of the box, I would suggest filing an issue in webpack/webpack and i (or someone from the team) will pick it up shortly @frontendphil

@steffektif
Copy link

@EugeneHlushko might this be a suitable workaround?

@EugeneHlushko
Copy link
Member Author

@steffektif the last comment seems pretty much what i thought of but in the light of styled-components

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

Successfully merging this pull request may close these issues.

6 participants