patriksimek / vm2

Advanced vm/sandbox for Node.js
MIT License
3.86k stars 294 forks source link

Row and column are not given in certain errors since version vm2@3.9.8 #474

Closed nicolafio closed 1 year ago

nicolafio commented 1 year ago

I found a problem from starting in version 3.9.8. If we instantiate NodeVM like follows

const nodeVm = new NodeVM({
  wrapper: "commonjs",
  console: "off",
  eval: false,
  wasm: false,
});

and run this code

nodeVm.run(
  "\ninvalid varName = 'test'; module.exports = () => {}\n",
  "example.js"
);

The SyntaxError error that gets thrown doesn't have a .loc attribute any more, which we relied on to find exactly where the syntax error was in the code.

The specific example script to reproduce this regression is the following, which tries to retrieve the .loc attribute:

Content of test.js ```js const { NodeVM } = require("vm2"); const nodeVm = new NodeVM({ wrapper: "commonjs", console: "off", eval: false, wasm: false, }); const code = ` invalid varName = 'test'; module.exports = () => {} `; try { nodeVm.run(code, "example.js"); } catch (e) { console.log(e.loc); } ```

And what follow are the results on the command line using the versions of vm2 from 3.9.7 up to 3.9.11. With version 3.9.7, we correctly fetch the Position object, while in later versions we get undefined.

image

XmiliaH commented 1 year ago

This property was never documented nor is it a default property of SyntaxError. The bug is more that the property was there.

nicolafio commented 1 year ago

How are you able to infer the row and column then? Doesn't seem possible other than using regular expressions on the stack trace.

XmiliaH commented 1 year ago

The same way one would do when getting a SyntaxError from a call to eval. One way would be to use a library which parses the source for you again when a SyntaxError was thrown and gives more details about the error.