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.

How to remove eval and Function constructor from webpack build to avoid CSP issues

See original GitHub issue

Do 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:closed
  • Created 6 years ago
  • Reactions:8
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
HaNdTriXcommented, Jun 4, 2018

Disabling node or node.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 in global being undefined in your code.

How I solved the issue:

  1. Create a global.js file somewhere in your project:

    global.js

    module.exports = window
    
  2. Disable node.global in your webpack.config.js by setting it to false.

  3. Add the global.js by using webpacks provide plugin:

    webpack.config.js

    {
      ...
      node: {
        global: false
      },
      plugins: [
        ...
        new webpack.ProvidePlugin({
          global: require.resolve('./global.js')
        })
        ...
      ],
      ...
    }
    

Please note, that you should only use this configuration, if the webpack.target is web.

5reactions
jhchencommented, Feb 19, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

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