forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: intercept property descriptors on sandbox
Fixes: nodejs#2734 Fixes: nodejs#5350 Fixes: nodejs#5679
- Loading branch information
Showing
8 changed files
with
246 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
test/known_issues/test-vm-attributes-property-not-on-sandbox.js
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
test/parallel/test-vm-attributes-property-not-on-sandbox.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
|
||
const sandbox = {}; | ||
vm.createContext(sandbox); | ||
const code = `Object.defineProperty( | ||
this, | ||
'foo', | ||
{ get: function() {return 17} } | ||
); | ||
var desc = Object.getOwnPropertyDescriptor(this, 'foo');`; | ||
|
||
vm.runInContext(code, sandbox); | ||
assert.strictEqual(typeof sandbox.desc.get, 'function'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const vm = require('vm'); | ||
|
||
const dSymbol = Symbol('d'); | ||
const sandbox = { | ||
a: 'a', | ||
dSymbol | ||
}; | ||
|
||
Object.defineProperties(sandbox, { | ||
b: { | ||
value: 'b' | ||
}, | ||
c: { | ||
value: 'c', | ||
writable: true, | ||
enumerable: true | ||
}, | ||
[dSymbol]: { | ||
value: 'd' | ||
}, | ||
e: { | ||
value: 'e', | ||
configurable: true | ||
}, | ||
f: {} | ||
}); | ||
|
||
const ctx = vm.createContext(sandbox); | ||
|
||
const result = vm.runInContext(` | ||
const getDesc = (prop) => Object.getOwnPropertyDescriptor(this, prop); | ||
const result = { | ||
a: getDesc('a'), | ||
b: getDesc('b'), | ||
c: getDesc('c'), | ||
d: getDesc(dSymbol), | ||
e: getDesc('e'), | ||
f: getDesc('f'), | ||
g: getDesc('g') | ||
}; | ||
result; | ||
`, ctx); | ||
|
||
//eslint-disable-next-line no-restricted-properties | ||
assert.deepEqual(result, { | ||
a: { value: 'a', writable: true, enumerable: true, configurable: true }, | ||
b: { value: 'b', writable: false, enumerable: false, configurable: false }, | ||
c: { value: 'c', writable: true, enumerable: true, configurable: false }, | ||
d: { value: 'd', writable: false, enumerable: false, configurable: false }, | ||
e: { value: 'e', writable: false, enumerable: false, configurable: true }, | ||
f: { | ||
value: undefined, | ||
writable: false, | ||
enumerable: false, | ||
configurable: false | ||
}, | ||
g: undefined | ||
}); | ||
|
||
// define new properties | ||
vm.runInContext(` | ||
Object.defineProperty(this, 'h', {value: 'h'}); | ||
Object.defineProperty(this, 'i', {}); | ||
Object.defineProperty(this, 'j', { | ||
get() { return 'j'; } | ||
}); | ||
let kValue = 0; | ||
Object.defineProperty(this, 'k', { | ||
get() { return kValue; }, | ||
set(value) { kValue = value } | ||
}); | ||
`, ctx); | ||
|
||
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(ctx, 'h'), { | ||
value: 'h', | ||
writable: false, | ||
enumerable: false, | ||
configurable: false | ||
}); | ||
|
||
assert.deepStrictEqual(Object.getOwnPropertyDescriptor(ctx, 'i'), { | ||
value: undefined, | ||
writable: false, | ||
enumerable: false, | ||
configurable: false | ||
}); | ||
|
||
const jDesc = Object.getOwnPropertyDescriptor(ctx, 'j'); | ||
assert.strictEqual(typeof jDesc.get, 'function'); | ||
assert.strictEqual(typeof jDesc.set, 'undefined'); | ||
assert.strictEqual(jDesc.enumerable, false); | ||
assert.strictEqual(jDesc.configurable, false); | ||
|
||
const kDesc = Object.getOwnPropertyDescriptor(ctx, 'k'); | ||
assert.strictEqual(typeof kDesc.get, 'function'); | ||
assert.strictEqual(typeof kDesc.set, 'function'); | ||
assert.strictEqual(kDesc.enumerable, false); | ||
assert.strictEqual(kDesc.configurable, false); | ||
|
||
assert.strictEqual(ctx.k, 0); | ||
ctx.k = 1; | ||
assert.strictEqual(ctx.k, 1); | ||
assert.strictEqual(vm.runInContext('k;', ctx), 1); | ||
vm.runInContext('k = 2;', ctx); | ||
assert.strictEqual(ctx.k, 2); | ||
assert.strictEqual(vm.runInContext('k;', ctx), 2); | ||
|
||
// redefine properties on the global object | ||
assert.strictEqual(typeof vm.runInContext('encodeURI;', ctx), 'function'); | ||
assert.strictEqual(ctx.encodeURI, undefined); | ||
vm.runInContext(` | ||
Object.defineProperty(this, 'encodeURI', { value: 42 }); | ||
`, ctx); | ||
assert.strictEqual(vm.runInContext('encodeURI;', ctx), 42); | ||
assert.strictEqual(ctx.encodeURI, 42); | ||
|
||
// redefine properties on the sandbox | ||
vm.runInContext(` | ||
Object.defineProperty(this, 'e', { value: 'newE' }); | ||
`, ctx); | ||
assert.strictEqual(ctx.e, 'newE'); | ||
|
||
assert.throws(() => vm.runInContext(` | ||
'use strict'; | ||
Object.defineProperty(this, 'f', { value: 'newF' }); | ||
`, ctx), /TypeError: Cannot redefine property: f/); |