Browse Source

src: handle thrown errors in CopyProperties()

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 <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>
v7.x
cjihrig 8 years ago
parent
commit
d7994db70c
  1. 9
      src/node_contextify.cc
  2. 13
      test/parallel/test-vm-proxies.js

9
src/node_contextify.cc

@ -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:
//

13
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/);

Loading…
Cancel
Save