Skip to content

Commit

Permalink
src: handle thrown errors in CopyProperties()
Browse files Browse the repository at this point in the history
This commit prevents thrown JavaScript exceptions from crashing
the process in node_contextify's CopyProperties() function.

Fixes: #8537
PR-URL: #8649
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
cjihrig authored and Fishrock123 committed Oct 11, 2016
1 parent 73d54a6 commit 2dbef79
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ class ContextifyContext {
int length = names->Length();
for (int i = 0; i < length; i++) {
Local<String> key = names->Get(i)->ToString(env()->isolate());
bool has = sandbox_obj->HasOwnProperty(context, key).FromJust();
auto maybe_has = sandbox_obj->HasOwnProperty(context, key);

// Check for pending exceptions
if (!maybe_has.IsJust())
break;

bool has = maybe_has.FromJust();

if (!has) {
// Could also do this like so:
//
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-vm-proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,16 @@ sandbox = { Proxy: Proxy };
vm.runInNewContext('this.Proxy = Proxy', sandbox);
assert.strictEqual(typeof sandbox.Proxy, 'function');
assert.strictEqual(sandbox.Proxy, Proxy);

// Handle a sandbox that throws while copying properties
assert.throws(() => {
const handler = {
getOwnPropertyDescriptor: (target, prop) => {
throw new Error('whoops');
}
};
const sandbox = new Proxy({foo: 'bar'}, handler);
const context = vm.createContext(sandbox);

vm.runInContext('', context);
}, /whoops/);

0 comments on commit 2dbef79

Please sign in to comment.