1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
'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/);
|