Skip to content

Commit 2dbef79

Browse files
cjihrigFishrock123
authored andcommitted
src: handle thrown errors in CopyProperties()
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>
1 parent 73d54a6 commit 2dbef79

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/node_contextify.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,14 @@ class ContextifyContext {
125125
int length = names->Length();
126126
for (int i = 0; i < length; i++) {
127127
Local<String> key = names->Get(i)->ToString(env()->isolate());
128-
bool has = sandbox_obj->HasOwnProperty(context, key).FromJust();
128+
auto maybe_has = sandbox_obj->HasOwnProperty(context, key);
129+
130+
// Check for pending exceptions
131+
if (!maybe_has.IsJust())
132+
break;
133+
134+
bool has = maybe_has.FromJust();
135+
129136
if (!has) {
130137
// Could also do this like so:
131138
//

test/parallel/test-vm-proxies.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ sandbox = { Proxy: Proxy };
1616
vm.runInNewContext('this.Proxy = Proxy', sandbox);
1717
assert.strictEqual(typeof sandbox.Proxy, 'function');
1818
assert.strictEqual(sandbox.Proxy, Proxy);
19+
20+
// Handle a sandbox that throws while copying properties
21+
assert.throws(() => {
22+
const handler = {
23+
getOwnPropertyDescriptor: (target, prop) => {
24+
throw new Error('whoops');
25+
}
26+
};
27+
const sandbox = new Proxy({foo: 'bar'}, handler);
28+
const context = vm.createContext(sandbox);
29+
30+
vm.runInContext('', context);
31+
}, /whoops/);

0 commit comments

Comments
 (0)