Skip to content

Commit

Permalink
src: don't overwrite non-writable vm globals
Browse files Browse the repository at this point in the history
Check that the property doesn't have the read-only flag set before
overwriting it.

Fixes: nodejs#10223
PR-URL: nodejs#10227
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
  • Loading branch information
bnoordhuis committed Dec 13, 2016
1 parent 3b2a630 commit 524f693
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,22 @@ class ContextifyContext {
if (ctx->context_.IsEmpty())
return;

auto attributes = PropertyAttribute::None;
bool is_declared =
ctx->global_proxy()->HasRealNamedProperty(ctx->context(),
property).FromJust();
bool is_contextual_store = ctx->global_proxy() != args.This();
ctx->global_proxy()->GetRealNamedPropertyAttributes(ctx->context(),
property)
.To(&attributes);
bool read_only =
static_cast<int>(attributes) &
static_cast<int>(PropertyAttribute::ReadOnly);

if (is_declared && read_only)
return;

bool set_property_will_throw =
args.ShouldThrowOnError() &&
!is_declared &&
is_contextual_store;
if (!is_declared && args.ShouldThrowOnError())
return;

if (!set_property_will_throw) {
ctx->sandbox()->Set(property, value);
}
ctx->sandbox()->Set(property, value);
}


Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-vm-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,14 @@ assert.throws(function() {
// https://github.com/nodejs/node/issues/6158
ctx = new Proxy({}, {});
assert.strictEqual(typeof vm.runInNewContext('String', ctx), 'function');

// https://github.com/nodejs/node/issues/10223
ctx = vm.createContext();
vm.runInContext('Object.defineProperty(this, "x", { value: 42 })', ctx);
assert.strictEqual(ctx.x, undefined); // Not copied out by cloneProperty().
assert.strictEqual(vm.runInContext('x', ctx), 42);
vm.runInContext('x = 0', ctx); // Does not throw but x...
assert.strictEqual(vm.runInContext('x', ctx), 42); // ...should be unaltered.
assert.throws(() => vm.runInContext('"use strict"; x = 0', ctx),
/Cannot assign to read only property 'x'/);
assert.strictEqual(vm.runInContext('x', ctx), 42);

0 comments on commit 524f693

Please sign in to comment.