Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
src: don't overwrite non-writable vm globals
Check that the property doesn't have the read-only flag set before
overwriting it.

This is Ben Noordhuis previous commit, but keeping
is_contextual_store. is_contextual_store describes whether this.foo = 42 or
foo = 42 was called. The second is contextual and will fail
in strict mode if foo is used without declaration. Therefore only do an
early return if it is a contextual store. In particular,
don't do an early return for Object.defineProperty(this, ...).

Fixes: #10223
Refs: #10227
  • Loading branch information
fhinkel committed Feb 3, 2017
commit c081b5b92cb78fdbc2c0ccd632e876f965952d6e
27 changes: 18 additions & 9 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,28 @@ class ContextifyContext {
if (ctx->context_.IsEmpty())
return;

auto attributes = PropertyAttribute::None;
bool is_declared =
ctx->global_proxy()->HasRealNamedProperty(ctx->context(),
property).FromJust();
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;

// true for x = 5
// false for this.x = 5
// false for Object.defineProperty(this, 'foo', ...)
// false for vmResult.x = 5 where vmResult = vm.runInContext();
bool is_contextual_store = ctx->global_proxy() != args.This();

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

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


Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-vm-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: These tests can be logically grouped. Can you group them in blocks?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which groups did you have in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this?

{
  // https://github.com/nodejs/node/issues/10223
  const ctx = vm.createContext();

  {
    vm.runInContext('Object.defineProperty(this, "x", { value: 42 })', ctx);
    assert.strictEqual(ctx.x, 42);
    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);
  }

}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocks in JS Code look weird to me. I added some extra blank lines. OK like this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool :-) This looks clean to my eyes now.

assert.strictEqual(ctx.x, 42);
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);