From d7994db70cd762653070ff865c1fbc2d7ebd276b Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 18 Sep 2016 13:05:30 -0400 Subject: [PATCH] src: handle thrown errors in CopyProperties() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit prevents thrown JavaScript exceptions from crashing the process in node_contextify's CopyProperties() function. Fixes: https://github.com/nodejs/node/issues/8537 PR-URL: https://github.com/nodejs/node/pull/8649 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis Reviewed-By: Franziska Hinkelmann Reviewed-By: James M Snell Reviewed-By: Ilkka Myller Reviewed-By: Michaƫl Zasso --- src/node_contextify.cc | 9 ++++++++- test/parallel/test-vm-proxies.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 3262b15c7b6..c17ca6ef992 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -125,7 +125,14 @@ class ContextifyContext { int length = names->Length(); for (int i = 0; i < length; i++) { Local 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: // diff --git a/test/parallel/test-vm-proxies.js b/test/parallel/test-vm-proxies.js index d47f7508213..d908d713748 100644 --- a/test/parallel/test-vm-proxies.js +++ b/test/parallel/test-vm-proxies.js @@ -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/);