Skip to content

added nonce capability - #3210

Merged
sokra merged 1 commit into
webpack:masterfrom
lunasofia:new_add_nonce
Nov 23, 2016
Merged

sokra merged 1 commit into
webpack:masterfrom
lunasofia:new_add_nonce

Conversation

@lunasofia

Copy link
Copy Markdown
Contributor

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior? (You can also link to an open issue here)
#455

Currently, webpack does provide a way to add nonces to scripts. Given that the future of CSP could be a more nonce-based approach, having support for nonces is increasingly important.
https://research.google.com/pubs/pub45542.html

What is the new behavior?
When user specifies webpack_nonce, a nonce is added to all loaded scripts. Because nonces are generally different on each page load, the nonce is specified in the entry file, not in the config file, similarly to a public path not known ahead of time.

Does this PR introduce a breaking change?

  • Yes
  • No

If this PR contains a breaking change, please describe the following...

  • Impact:
  • Migration path for existing applications:
  • Github Issue(s) this is regarding:

Other information:

@lunasofia lunasofia mentioned this pull request Oct 30, 2016
3 of 11 tasks

@sokra sokra left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Manual test required to verify this PR

@sokra

sokra commented Oct 31, 2016

Copy link
Copy Markdown
Member

Could you explain why you need this nonce attribute on the script tag? From what I know it's only required for inline script tags...

cc @samertm

@lunasofia

Copy link
Copy Markdown
Contributor Author

Recent research out of Google suggests that using nonces for external scripts as well as inline scripts is a better way to write CSPs, since it avoids problems with transitive trust given to certain external sources. https://research.google.com/pubs/pub45542.html

@samertm

samertm commented Oct 31, 2016

Copy link
Copy Markdown

+1, we're planning on using this patch at Dropbox

@sokra sokra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

some spacing issues

Comment thread test/cases/nonce/set-nonce/index.js Outdated
__webpack_nonce__ = 'nonce1234';
require.ensure([], function(require) {
require("./empty?a");
}, "chunk-with-nonce");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

too many spaces

Comment thread test/cases/nonce/set-nonce/index.js Outdated
__webpack_nonce__ = undefined;
// if in browser context, test that nonce was added.
if (typeof document !== 'undefined') {
var script = document.querySelector('script[src="js/chunk-with-nonce.web.js"]');

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mixed spaces and tabs

Comment thread test/cases/nonce/set-nonce/index.js Outdated
it("should load script without nonce", function(done) {
require.ensure([], function(require) {
require("./empty?b");
}, "chunk-without-nonce");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

spaces

Comment thread test/cases/nonce/set-nonce/index.js Outdated
// if in browser context, test that nonce was added.
if (typeof document !== 'undefined') {
var script = document.querySelector('script[src="js/chunk-without-nonce.web.js"]');
script.hasAttribute('nonce').should.be.eql(false);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mixed

@sokra

sokra commented Nov 9, 2016

Copy link
Copy Markdown
Member

Pling

Please at least sign the CLA so another person can pick up your commit and continue it.

@lunasofia

Copy link
Copy Markdown
Contributor Author

I've made the necessary changes, but unfortunately there's another merge conflict in the stats test cases. I'm on a different computer now than I was, but all that I need to do is run those test cases and figure out what the new outputs should be – I'm just not sure what I need to install to do this. Can someone point me in the right direction? I installed Yarn, but it doesn't seem to be working (various packages missing) and before I go down that rabbit hole, I was wondering whether there was an easier way to just run the single Stats.test.js file.

I'd really like to get this done soon, and since I'm on Thanksgiving break from school I should actually be able to finish it all quickly once I get these tests running.

@sokra

sokra commented Nov 17, 2016

Copy link
Copy Markdown
Member
npm install
npm link
npm link webpack
npm test
node test/statsCases/accept-stats.js

instead of npm test you could also run mocha -g "Stats" after npm install mocha -g.

Make sure to run npm run beautify-lint and npm run lint. This will point you to wrong spacing etc.

@jsf-clabot

jsf-clabot commented Nov 21, 2016 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@sokra

sokra commented Nov 23, 2016

Copy link
Copy Markdown
Member

PR looks great. I'll merge it once you signed the CLA. Make sure your commits include the correct email.

change expected outputs of stats cases so they match new output

added nonce test to browser tests
@lunasofia

Copy link
Copy Markdown
Contributor Author

Email issue should be fixed now!

@sokra
sokra merged commit f9e57d9 into webpack:master Nov 23, 2016
@sokra

sokra commented Nov 23, 2016

Copy link
Copy Markdown
Member

Great thanks for the PR

@ruudk

ruudk commented Feb 10, 2017

Copy link
Copy Markdown

@lunasofia Thanks for this addition. I can't get it to work since there is no documentation found anywhere about this. Any hints? Should I just add __webpack_nonce__ = 'value'; in my HTML head?

@lunasofia

Copy link
Copy Markdown
Contributor Author

@ruudk you should set it in your index.js file, I believe

@toukenath

Copy link
Copy Markdown

Hello @lunasofia. Is there any documentation about this feature and how to implement it?

I assume I need to set the webpack_nonce = "myToken" somewhere in a config file but I don't know where. You specified an index.js, can you, please, tell me where it is?

My application uses :
Angular-cli : 1.0.3
ngtools/webpack : 1.3.1
webpack : 2.2.0

@nickytonline

Copy link
Copy Markdown

@lunasofia @sokra, I'm trying to wrap my head around this and I feel like I'm missing something very obvious. I've added the magic string in my app's entry point, e.g. __webpack_nonce__ = "somenoncy";, https://twitter.com/probablyup/status/909475500514529280, but I don't see webpack adding it to inline script that it injects, e.g.

<script>!function(e){function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}var r=window.webpackJsonp;...

Is there something in my webpack config that I need to add as well to "nonce"ify my inline scripts injected by webpack?

@lunasofia

Copy link
Copy Markdown
Contributor Author

Hi! I'm so sorry I haven't been responding to questions here. I worked on this for an internship, and did not have the time to keep up to date on the project after my internship ended. It's likely that @samertm will be able to answer your questions.

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.

7 participants