From 92bcd1931b1abd80706e8bec516bac5fa14b9ecd Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 10 Sep 2017 01:43:55 +0200 Subject: [PATCH] worker: implement vm.moveMessagePortToContext() This should help a lot with actual sandboxing of JS code. Thanks to Timothy Gu, Stephen Belanger and Benjamin Gruenbaum for reviewing this change in its original form. Refs: https://github.com/ayojs/ayo/pull/111 --- doc/api/vm.md | 43 +++++++++++++++++++++ doc/api/worker.md | 11 +++++- lib/vm.js | 3 ++ src/node_contextify.cc | 20 ++++++++-- src/node_contextify.h | 5 +++ src/node_messaging.cc | 34 ++++++++++++++++ src/node_messaging.h | 5 +++ test/parallel/test-message-channel-move.js | 45 ++++++++++++++++++++++ 8 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 test/parallel/test-message-channel-move.js diff --git a/doc/api/vm.md b/doc/api/vm.md index cc9b3135381dad..d70043b1891a89 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -732,6 +732,49 @@ console.log(util.inspect(sandbox)); // { globalVar: 1024 } ``` +## vm.moveMessagePortToContext(port, contextifiedSandbox) + + +* `port` {MessagePort} +* `contextifiedSandbox` {Object} A contextified object as returned by the + `vm.createContext()` method. +* Returns: {MessagePort} + +Bind a `MessagePort` to a specific VM context. This returns a new `MessagePort` +object, whose prototype and methods act as if they were created in the passed +context. The received messages will also be emitted as objects from the passed +context. + +The `port` object on which this method was called can not be used for sending +or receiving further messages. + +## vm.runInDebugContext(code) + + +> Stability: 0 - Deprecated. An alternative is in development. + +* `code` {string} The JavaScript code to compile and run. + +The `vm.runInDebugContext()` method compiles and executes `code` inside the V8 +debug context. The primary use case is to gain access to the V8 `Debug` object: + +```js +const vm = require('vm'); +const Debug = vm.runInDebugContext('Debug'); +console.log(Debug.findScript(process.emit).name); // 'events.js' +console.log(Debug.findScript(process.exit).name); // 'internal/process.js' +``` + +*Note*: The debug context and object are intrinsically tied to V8's debugger +implementation and may change (or even be removed) without prior warning. + +The `Debug` object can also be made available using the V8-specific +`--expose_debug_as=` [command line option][]. + ## vm.runInNewContext(code[, sandbox][, options])