forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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: ayojs/ayo#111
- Loading branch information
Showing
8 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* eslint-disable prefer-assert-methods */ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
const { MessageChannel } = require('worker'); | ||
|
||
{ | ||
const context = vm.createContext(); | ||
const channel = new MessageChannel(); | ||
context.port = vm.moveMessagePortToContext(channel.port1, context); | ||
context.global = context; | ||
const port = channel.port2; | ||
vm.runInContext('(' + function() { | ||
function assert(condition) { if (!condition) throw new Error(); } | ||
|
||
{ | ||
assert(port instanceof Object); | ||
assert.strictEqual(port.onmessage, undefined); | ||
assert(port.postMessage instanceof Function); | ||
port.onmessage = function(msg) { | ||
assert(msg instanceof Object); | ||
port.postMessage(msg); | ||
}; | ||
port.start(); | ||
} | ||
|
||
{ | ||
let threw = false; | ||
try { | ||
port.postMessage(global); | ||
} catch (e) { | ||
assert(e instanceof Object); | ||
assert(e instanceof Error); | ||
threw = true; | ||
} | ||
assert(threw); | ||
} | ||
} + ')()', context); | ||
port.on('message', common.mustCall((msg) => { | ||
assert(msg instanceof Object); | ||
port.close(); | ||
})); | ||
port.postMessage({}); | ||
} |