-
-
Notifications
You must be signed in to change notification settings - Fork 80
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
The use of eval in vm is restricted #425
Comments
It seems you are using the engine pool which disables |
It's more like a JS trick as follows. nodeRuntime.allowEval(true);
nodeRuntime.getExecutor("const vm = require(\"node:vm\");\n" +
"const script = new vm.Script(\"eval('1+1')\");\n" +
"console.log(script.runInThisContext());\n" +
"console.log(script.runInNewContext(vm.createContext({eval})));").executeVoid();
// Output
2
2 |
Have you tried |
Those API require certain understanding on how V8 works internally. Please let your code. |
fun resolve(runtime: V8Runtime, resourceName: String, v8ModuleReferrer: IV8Module): Any? {
return when {
resourceName.startsWith("/") || resourceName.startsWith("./") || resourceName.startsWith("../") -> {
val parentModuleName = v8ModuleReferrer.getResourceName()
val moduleRelativePath = Paths.get(parentModuleName).parent.resolve(resourceName).normalize()
val moduleFile = Paths.get(localRelativePath).resolve(moduleRelativePath).toFile()
if (moduleFile.exists() && moduleFile.isFile) {
val url = "file://${moduleFile.canonicalPath}"
val code = readFile(url)
runtime.getExecutor("import.meta.url=${Json.encodeToString(url)};$code")
.setResourceName(moduleRelativePath.toString()).compileV8Module()
} else {
null
}
}
resourceName.startsWith("node:") -> nodeResolve.resolve(runtime, resourceName, v8ModuleReferrer)
else -> {
val moduleObject = runtime.getNodeModule(resourceName, nodeModuleAnyClazz).getModuleObject()
moduleObject.set("default", moduleObject)
runtime.createV8Module(resourceName, moduleObject)
}
}
} This is my code. |
It doesn't seem to be a complete reproducible code. Could you leave a repo with the issue? |
Can you take a look at it for me? |
const vm = require('vm'); // 创建一个沙箱上下文 // 创建一个虚拟机上下文,允许执行 eval // 使用 vm.runInContext 执行代码 console.log(result); // 输出: 3 |
v8Isolate->SetModifyCodeGenerationFromStringsCallback(nullptr); |
It's normal to write like this.
But
Will throw an error:
EvalError: Code generation from strings disallowed for this context
So this is a bug?
The text was updated successfully, but these errors were encountered: