Skip to content

sandbox inheritied properties flattened after vm.runInNewContext #5350

Closed
@waynedpj

Description

@waynedpj

on node-4.5.1 when using a sandbox Object with inherited properties, after running sandbox in vm.runInNewContext all properties up the prototype chain are copied to sandbox Object, even if they are not changed by code, though the prototype chain is still there. here is a failing test case

'use strict';

let vm = require('vm'), assert = require('assert');

let base = Object.create(null);
base.x = 1;
base.y = 2;

let sandbox = Object.create(base);
sandbox.z = 3;

console.log('base:\n%j', base);
console.log('sandbox:\n%j', sandbox);
console.log("sandbox's own keys:\n%j", Object.keys(sandbox));

let code = 'x = 0; z = 4;';
let result = vm.runInNewContext(code, sandbox);
console.log('result: %s', result);

console.log('base:\n%j', base);
console.log('sandbox:\n%j', sandbox);
console.log("sandbox's own keys:\n%j", Object.keys(sandbox));

assert((Object.keys(sandbox)).indexOf('y') === -1);  // fails

note that the same problem occurs even if code is empty, as well as with vm.runInContext.

not sure if this is expected behavior or not, but i was surprised to have sandbox modified like that. if it is normal, i did not find any mention of it in the docs, and would ask if someone could kindly explain why?

thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions