Skip to content

解决Node读取Js文件未设置编码造成的vm.run()运行报错问题和vm.runInContext()方法不存在造成的运行报错问题#79

Open
Gowther wants to merge 1 commit into
Peng-YM:masterfrom
Gowther:patch-1
Open

Conversation

@Gowther

@Gowther Gowther commented Nov 27, 2021

Copy link
Copy Markdown
  1. 本地Node版本为v12.18.3,根据Node官方文档说明, fs.readFileSync(path, endcoding)方法,不传encoding时方法返回的是二进制buffer,126行 vm.run(code) vm运行时会报错。
    If the encoding option is specified then this function returns a string. Otherwise it returns a buffer.
  2. 根据vm2官方文档说明,没有找到 vm.runInContext()方法, 160 行运行报错。
    What is the difference between Node's vm and vm2?
const vm = require('vm');
vm.runInNewContext('this.constructor.constructor("return process")().exit()');
const {VM} = require('vm2');
new VM().run('this.constructor.constructor("return process")().exit()');

1. 本地Node版本v12.18.3,根据Node官方文档(https://nodejs.org/api/fs.html#fsreaddirsyncpath-options)说明,fs.readFileSync(path, endcoding)方法,不传encoding时方法返回的是二进制的buffer,126行 vm.run(code) 运行时会报错
2. 根据vm2官方文档(https://github.com/patriksimek/vm2)说明,找不到 vm.runInContext()方法, 160 行运行报错
What is the difference between Node's vm and vm2?
const vm = require('vm');
vm.runInNewContext('this.constructor.constructor("return process")().exit()');
const {VM} = require('vm2');
new VM().run('this.constructor.constructor("return process")().exit()');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant