Skip to content

Commit 90d4c93

Browse files
committed
vm: properly handle defining props on any value
While it was supposed to fix most of the remaining issues, nodejs#46458 missed some in strict mode. This PR adds some additional checks. It also clarifies what we are really checking to execute or not the `GetReturnValue`.
1 parent a37c083 commit 90d4c93

File tree

3 files changed

+171
-11
lines changed

3 files changed

+171
-11
lines changed

src/node_contextify.cc

+14-2
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,22 @@ void ContextifyContext::PropertySetterCallback(
527527
!is_function)
528528
return;
529529

530+
Local<Value> desc;
531+
bool is_get_set_property = false;
532+
if (is_declared_on_sandbox &&
533+
ctx->sandbox()->GetOwnPropertyDescriptor(context, property).ToLocal(&desc) &&
534+
desc->IsObject()) {
535+
Local<Object> desc_obj = desc.As<Object>();
536+
Isolate* isolate = context->GetIsolate();
537+
is_get_set_property = desc_obj->HasOwnProperty(context, String::NewFromUtf8(isolate, "get").ToLocalChecked()).FromMaybe(false) ||
538+
desc_obj->HasOwnProperty(context, String::NewFromUtf8(isolate, "set").ToLocalChecked()).FromMaybe(false);
539+
}
540+
530541
USE(ctx->sandbox()->Set(context, property, value));
531-
if (is_contextual_store || is_function) {
542+
543+
// We have to specify the return value for any contextual or get/set property
544+
if (is_get_set_property)
532545
args.GetReturnValue().Set(value);
533-
}
534546
}
535547

536548
// static

test/parallel/test-vm-global-setter.js

+146-9
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,164 @@ const common = require('../common');
33
const assert = require('assert');
44
const vm = require('vm');
55

6+
const getSetSymbolReceivingFunction = Symbol('sym-1');
7+
const getSetSymbolReceivingNumber = Symbol('sym-2');
8+
const symbolReceivingNumber = Symbol('sym-3');
9+
const unknownSymbolReceivingNumber = Symbol('sym-4');
10+
611
const window = createWindow();
712

8-
const descriptor =
9-
Object.getOwnPropertyDescriptor(window.globalProxy, 'onhashchange');
13+
const descriptor1 = Object.getOwnPropertyDescriptor(
14+
window.globalProxy,
15+
'getSetPropReceivingFunction'
16+
);
17+
assert.strictEqual(typeof descriptor1.get, 'function');
18+
assert.strictEqual(typeof descriptor1.set, 'function');
19+
assert.strictEqual(descriptor1.configurable, true);
20+
21+
const descriptor2 = Object.getOwnPropertyDescriptor(
22+
window.globalProxy,
23+
'getSetPropReceivingNumber'
24+
);
25+
assert.strictEqual(typeof descriptor2.get, 'function');
26+
assert.strictEqual(typeof descriptor2.set, 'function');
27+
assert.strictEqual(descriptor2.configurable, true);
28+
29+
const descriptor3 = Object.getOwnPropertyDescriptor(
30+
window.globalProxy,
31+
'propReceivingNumber'
32+
);
33+
assert.strictEqual(descriptor3.value, 44);
34+
35+
const descriptor4 = Object.getOwnPropertyDescriptor(
36+
window.globalProxy,
37+
'unknownPropReceivingNumber'
38+
);
39+
assert.strictEqual(descriptor4, undefined);
40+
41+
const descriptor5 = Object.getOwnPropertyDescriptor(
42+
window.globalProxy,
43+
getSetSymbolReceivingFunction
44+
);
45+
assert.strictEqual(typeof descriptor5.get, 'function');
46+
assert.strictEqual(typeof descriptor5.set, 'function');
47+
assert.strictEqual(descriptor5.configurable, true);
48+
49+
const descriptor6 = Object.getOwnPropertyDescriptor(
50+
window.globalProxy,
51+
getSetSymbolReceivingNumber
52+
);
53+
assert.strictEqual(typeof descriptor6.get, 'function');
54+
assert.strictEqual(typeof descriptor6.set, 'function');
55+
assert.strictEqual(descriptor6.configurable, true);
56+
57+
const descriptor7 = Object.getOwnPropertyDescriptor(
58+
window.globalProxy,
59+
symbolReceivingNumber
60+
);
61+
assert.strictEqual(descriptor7.value, 48);
1062

11-
assert.strictEqual(typeof descriptor.get, 'function');
12-
assert.strictEqual(typeof descriptor.set, 'function');
13-
assert.strictEqual(descriptor.configurable, true);
63+
const descriptor8 = Object.getOwnPropertyDescriptor(
64+
window.globalProxy,
65+
unknownSymbolReceivingNumber
66+
);
67+
assert.strictEqual(descriptor8, undefined);
68+
69+
const descriptor9 = Object.getOwnPropertyDescriptor(
70+
window.globalProxy,
71+
'getSetPropThrowing'
72+
);
73+
assert.strictEqual(typeof descriptor9.get, 'function');
74+
assert.strictEqual(typeof descriptor9.set, 'function');
75+
assert.strictEqual(descriptor9.configurable, true);
76+
77+
const descriptor10 = Object.getOwnPropertyDescriptor(
78+
window.globalProxy,
79+
'nonWritableProp'
80+
);
81+
assert.strictEqual(descriptor10.value, 51);
82+
assert.strictEqual(descriptor10.writable, false);
1483

1584
// Regression test for GH-42962. This assignment should not throw.
16-
window.globalProxy.onhashchange = () => {};
85+
window.globalProxy.getSetPropReceivingFunction = () => {};
86+
assert.strictEqual(window.globalProxy.getSetPropReceivingFunction, 42);
87+
88+
window.globalProxy.getSetPropReceivingNumber = 143;
89+
assert.strictEqual(window.globalProxy.getSetPropReceivingNumber, 43);
90+
91+
window.globalProxy.propReceivingNumber = 144;
92+
assert.strictEqual(window.globalProxy.propReceivingNumber, 144);
93+
94+
window.globalProxy.unknownPropReceivingNumber = 145;
95+
assert.strictEqual(window.globalProxy.unknownPropReceivingNumber, 145);
96+
97+
window.globalProxy[getSetSymbolReceivingFunction] = () => {};
98+
assert.strictEqual(window.globalProxy[getSetSymbolReceivingFunction], 46);
99+
100+
window.globalProxy[getSetSymbolReceivingNumber] = 147;
101+
assert.strictEqual(window.globalProxy[getSetSymbolReceivingNumber], 47);
17102

18-
assert.strictEqual(window.globalProxy.onhashchange, 42);
103+
window.globalProxy[symbolReceivingNumber] = 148;
104+
assert.strictEqual(window.globalProxy[symbolReceivingNumber], 148);
105+
106+
window.globalProxy[unknownSymbolReceivingNumber] = 149;
107+
assert.strictEqual(window.globalProxy[unknownSymbolReceivingNumber], 149);
108+
109+
let throwOnSet = false;
110+
try {
111+
window.globalProxy.getSetPropThrowing = 150;
112+
} catch (err) {
113+
throwOnSet = true;
114+
}
115+
assert.strictEqual(window.globalProxy.getSetPropThrowing, 50);
116+
assert.ok(throwOnSet);
117+
118+
throwOnSet = false;
119+
try {
120+
window.globalProxy.nonWritableProp = 151;
121+
} catch (err) {
122+
throwOnSet = true;
123+
}
124+
assert.strictEqual(window.globalProxy.nonWritableProp, 51);
125+
assert.ok(throwOnSet);
19126

20127
function createWindow() {
21128
const obj = {};
22129
vm.createContext(obj);
23-
Object.defineProperty(obj, 'onhashchange', {
130+
Object.defineProperty(obj, 'getSetPropReceivingFunction', {
24131
get: common.mustCall(() => 42),
25132
set: common.mustCall(),
26-
configurable: true
133+
configurable: true,
134+
});
135+
Object.defineProperty(obj, 'getSetPropReceivingNumber', {
136+
get: common.mustCall(() => 43),
137+
set: common.mustCall(),
138+
configurable: true,
139+
});
140+
obj.propReceivingNumber = 44;
141+
// no property for unknownPropReceivingNumber
142+
Object.defineProperty(obj, getSetSymbolReceivingFunction, {
143+
get: common.mustCall(() => 46),
144+
set: common.mustCall(),
145+
configurable: true,
146+
});
147+
Object.defineProperty(obj, getSetSymbolReceivingNumber, {
148+
get: common.mustCall(() => 47),
149+
set: common.mustCall(),
150+
configurable: true,
151+
});
152+
obj[symbolReceivingNumber] = 48;
153+
// no property for unknownSymbolReceivingNumber
154+
Object.defineProperty(obj, 'getSetPropThrowing', {
155+
get: common.mustCall(() => 50),
156+
set: common.mustCall(() => {
157+
throw new Error('setter called');
158+
}),
159+
configurable: true,
160+
});
161+
Object.defineProperty(obj, 'nonWritableProp', {
162+
value: 51,
163+
writable: false,
27164
});
28165

29166
obj.globalProxy = vm.runInContext('this', obj);

test/parallel/test-vm-global-symbol.js

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const assert = require('assert');
44
const vm = require('vm');
55

66
const global = vm.runInContext('this', vm.createContext());
7+
78
const totoSymbol = Symbol.for('toto');
89
Object.defineProperty(global, totoSymbol, {
910
enumerable: true,
@@ -13,3 +14,13 @@ Object.defineProperty(global, totoSymbol, {
1314
});
1415
assert.strictEqual(global[totoSymbol], 4);
1516
assert.ok(Object.getOwnPropertySymbols(global).includes(totoSymbol));
17+
18+
const totoKey = 'toto';
19+
Object.defineProperty(global, totoKey, {
20+
enumerable: true,
21+
writable: true,
22+
value: 5,
23+
configurable: true,
24+
});
25+
assert.strictEqual(global[totoKey], 5);
26+
assert.ok(Object.getOwnPropertyNames(global).includes(totoKey));

0 commit comments

Comments
 (0)