How to remove eval and Function constructor from webpack build to avoid CSP issues
See original GitHub issueDo you want to request a feature or report a bug? BUG : Stackoverflow Link
What is the current behavior? It gives both eval and Function contructor which is not required
try {
// This works if eval is allowed (see CSP)
g = g || Function("return this")() || (1,eval)("this");
} catch(e) {
// This works if the window reference is available
if(typeof window === "object")
g = window;
}
// Another method of build
function setImmediate(callback) {
// Callback can either be a function or a string
if (typeof callback !== "function") {
callback = new Function("" + callback);
}
If the current behavior is a bug, please provide the steps to reproduce.
$ npm install -g vue-cli
$ vue init webpack-simple my-project
$ cd my-project
$ npm install
$ npm run prod
You can see in `build.js` having eval
What is the expected behavior? I expect that webpack should provide provision to build according to CSP policy as I am using runtime vue which does not even require compiler.
Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.
Node.js : 9.5.0
Tried it with latest config too. Package.json
{
"webpack": "^3.10.0",
"babel-core": "^6.18.2",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.18.0",
"babel-preset-stage-2": "^6.24.1",
"file-loader": "^0.9.0",
"style-loader": "^0.18.2",
"vue-loader": "^10.0.2"
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:8
- Comments:9 (2 by maintainers)
Top Results From Across the Web
How to remove eval and Function constructor from webpack ...
This is what contained within build.js file from webpack. Both Function constructor and eval usages. try { // This works if eval is...
Read more >How to remove eval and Function constructor from webpack ...
How to remove eval and Function constructor from webpack build to avoid CSP issues.
Read more >How to remove eval and Function constructor from webpack ...
Coding example for the question How to remove eval and Function constructor from webpack build to avoid CSP issues-Vue.js.
Read more >Remove Eval() For Production Code Through Webpack ...
How I solved the issue: Create a global.js file somewhere in your project: global.js. module. exports window. Disable node. global in your webpack....
Read more >www.gfcmsu.edu/dentalclinic/styles/js/modules/form...
node_modules/core-js/internals/copy-constructor-properties.js","webpack:///. ... This works if eval is allowed (see CSP)\n\tg = g || new Function(\"return ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Disabling
node
ornode.global
might break your isomorphic/universal modules.Responsible for that eval call is the following code: https://github.com/webpack/webpack/blob/master/buildin/global.js#L8-L11
If you remove it by setting
node.global
to false, will result inglobal
being undefined in your code.How I solved the issue:
Create a
global.js
file somewhere in your project:global.js
Disable
node.global
in yourwebpack.config.js
by setting it to false.Add the
global.js
by using webpacks provide plugin:webpack.config.js
Please note, that you should only use this configuration, if the
webpack.target
isweb
.I was able to fix the first eval issue by setting node.global to false. This will avoid including this shim which includes an eval in a try/catch. Not sure if this will be confusing or clarifying but I only observed this CSP warning in Firefox and not Chrome.