question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

__webpack_nonce__ fails due to imports getting hoisted above the assignment.

See original GitHub issue

Bug report

What is the current behavior?

I’m attempting to setup CSP in our application, and the __webpack_nonce__ assignment simply isn’t working. After digging into the generated code, I discovered it’s because all of the imports get hoisted above the assignment value, and thus get loaded before it ever gets set.

// entry.js
__webpack_nonce__ = "foo";
import { createRoot } from 'react-dom/client';
import App from './App';

const element = document.getElementById('root');
const root = createRoot(element);
root.render(<App />);

This produces the following:

/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {

"use strict";
/* harmony import */ var react_dom_client__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react-dom/client */ "./node_modules/react-dom/client.js");
/* harmony import */ var _App__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./App */ "./src/App.js");
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! react/jsx-runtime */ "./node_modules/react/jsx-runtime.js");

__webpack_require__.nc = "foo";
const element = document.getElementById('root');
const root = (0,react_dom_client__WEBPACK_IMPORTED_MODULE_1__.createRoot)(element);
root.render( /*#__PURE__*/(0,react_jsx_runtime__WEBPACK_IMPORTED_MODULE_3__.jsx)(_App__WEBPACK_IMPORTED_MODULE_2__["default"], {}));

I can confirm that at every invocation of a script include, __webpack_require__.nc is undefined because it executed before the assignment.

What is the expected behavior?

I expect to be able to set the nonce, per the documentation, and have it be attached to the script tags that the runtime produces.

Other relevant information: webpack version: 5.72.0 Node.js version: 16.14.0 Operating System: macOS 12.3 Additional tools: React 18, babel 7

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
vankopcommented, May 9, 2022

yeah, got a problem. I’ll fix this

0reactions
Twippedcommented, May 6, 2022

Reproduction repo here: https://github.com/Twipped/webpack-nonce-repro npm install and npm start should pop open a browser window that demonstrates the issue. If you use the console to navigate to load script and break at the check the value of __webpack_require__.nc on line 20, you’ll see it’s undefined, while window.NONCE does have a value present.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Content Security Policies - webpack
Webpack is capable of adding a nonce to all scripts that it loads. ... A unique hash-based nonce will then be generated and...
Read more >
How do I integrate the value of __webpack_nonce___ with my ...
We were able to get a dynamic nonce by having webpack build our index page (e.g via HtmlWebpackPlugin) as a template then serving...
Read more >
JavaScript key concepts & syntax | Modern JS
Once a variable is declared in this manner -- due to hoisting or because it isn't assigned an explicit value -- it becomes...
Read more >
https://elicejus.lt/wp-content/plugins/presto-play...
node_modules/@emotion/react/dist/emotion-react.browser.esm.js","webpack:///. ... -hoist-non-react-statics-do-not-use-this-in-your-code.browser.esm.js" ...
Read more >
Safari Technology Preview Release Notes - Apple Developer
Note: Shared Tab Groups and syncing for Tab Groups, Website Settings, and Web Extensions are not enabled in this release. WebAssembly. Fixed error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found