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

__webpack_nonce__ support or alternative way of solving CSP? #306

Closed
madjam002 opened this issue Mar 6, 2018 · 13 comments · Fixed by #410
Closed

__webpack_nonce__ support or alternative way of solving CSP? #306

madjam002 opened this issue Mar 6, 2018 · 13 comments · Fixed by #410

Comments

@madjam002
Copy link

I'm looking at using Webpack on a site which requires strict Content Security Policy headers.

I'm setting window.__webpack_nonce__ but it doesn't seem to be adding a nonce attribute to style elements created by style-loader. Looking at webpack#3210 it only seems to be adding a nonce attribute to generated script elements? Is there some additional work required in style-loader to support nonce attributes?

I don't mind doing a PR, just wanted to make sure first though that I'm not missing something!

@alexander-akait
Copy link
Member

@madjam002 Thanks for issue, need discussion, maybe better implement csp-loader, where solve this

@madjam002
Copy link
Author

Just to add as well, I've noticed some discussion in #155 regarding CSP, but the way that custom attributes has been implemented there isn't suitable for nonce values as nonce values need to be secure cryptographically generated random strings for each page load and not hard coded into the build

@jwbay
Copy link

jwbay commented Mar 16, 2018

The way aphrodite solves this is to look for an existing style tag before generating one. In their case something like <style data-aphrodite>. This effectively solves CSP because you can provide a style tag with a nonce already attached.

@iamdavidfrancis
Copy link
Contributor

iamdavidfrancis commented Apr 20, 2018

Has there been any progress on this? I'm also working on a site with a strict CSP.
It doesn't seem like it would be a huge PR to support __webpack_nonce__

@alexander-akait
Copy link
Member

PR welcome 👍

@plondon
Copy link

plondon commented Aug 19, 2018

I think this can be closed now since #319 has been merged and included in 0.22.0

@plondon
Copy link

plondon commented Aug 19, 2018

Also fyi @madjam002 you'll want to just set __webpack_nonce__ and not window.__webpack_nonce__. Also do not try to do this with hot module reloading.

@alexander-akait
Copy link
Member

@plondon can you send PR to README about this?

@alshdavid
Copy link

alshdavid commented Jul 23, 2019

What's the status with this?

Can I use style_loader with a nonce?
Is there anything special I need to do?
Will it read my window.__webpack_nonce__?
Will it work with dynamic imports?

@plondon
Copy link

plondon commented Jul 23, 2019

What's the status with this?

Resolved but confusing docs?

Can I use style_loader with a nonce?

Yes

Is there anything special I need to do?

Make sure the files where you set the nonce is one of the very first imports in the project:
See: create-nonce.js (https://github.com/blockchain/blockchain-wallet-v4-frontend/blob/development/packages/blockchain-wallet-v4-frontend/src/create-nonce.js#L4) is then imported from the first main entry point index.js (https://github.com/blockchain/blockchain-wallet-v4-frontend/blob/0477f60e62b3635ef00db3338319c939506c8756/packages/blockchain-wallet-v4-frontend/src/index.js)

Will it read my window.webpack_nonce?

No, webpack_nonce only

Will it work with dynamic imports?

Yes

@alexander-akait
Copy link
Member

@plondon maybe you can send a PR to docs ? 😄 anyway thanks for answer

@caub
Copy link

caub commented Feb 24, 2022

@plondon

you'll want to just set webpack_nonce and not window.webpack_nonce

What's the difference please? when executed from a script, it should be the same

__webpack_nonce__ = '..';
window.__webpack_nonce__ = '..';
globalThis.__webpack_nonce__ = '..';

@theKashey
Copy link

As many other "magic variables" webpack will transform __webpack_nonce__ to __webpack_require__.nc. Please refer to https://github.com/webpack/webpack/blob/1ed8aaf2e291fc0b14597ef452e6224ad10ccbc9/lib/RuntimeGlobals.js#L161

So the result of complication will be

__webpack_require__.nc = '..';
window.__webpack_nonce__ = '..';
globalThis.__webpack_nonce__ = '..';

But only if complied with webpack. Without it these lines are equal.

@xi xi mentioned this issue Oct 16, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants