Skip to content

Commit 8f48edd

Browse files
addaleaxtargos
authored andcommitted
vm: mark global proxy as side-effect-free
Fixes: #27518 PR-URL: #27523 Reviewed-By: Aleksei Koziatinskii <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 7252a64 commit 8f48edd

3 files changed

+76
-8
lines changed

src/node_contextify.cc

+12-8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ using v8::PrimitiveArray;
6161
using v8::PropertyAttribute;
6262
using v8::PropertyCallbackInfo;
6363
using v8::PropertyDescriptor;
64+
using v8::PropertyHandlerFlags;
6465
using v8::Script;
6566
using v8::ScriptCompiler;
6667
using v8::ScriptOrigin;
@@ -149,13 +150,15 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
149150
if (!CreateDataWrapper(env).ToLocal(&data_wrapper))
150151
return MaybeLocal<Context>();
151152

152-
NamedPropertyHandlerConfiguration config(PropertyGetterCallback,
153-
PropertySetterCallback,
154-
PropertyDescriptorCallback,
155-
PropertyDeleterCallback,
156-
PropertyEnumeratorCallback,
157-
PropertyDefinerCallback,
158-
data_wrapper);
153+
NamedPropertyHandlerConfiguration config(
154+
PropertyGetterCallback,
155+
PropertySetterCallback,
156+
PropertyDescriptorCallback,
157+
PropertyDeleterCallback,
158+
PropertyEnumeratorCallback,
159+
PropertyDefinerCallback,
160+
data_wrapper,
161+
PropertyHandlerFlags::kHasNoSideEffect);
159162

160163
IndexedPropertyHandlerConfiguration indexed_config(
161164
IndexedPropertyGetterCallback,
@@ -164,7 +167,8 @@ MaybeLocal<Context> ContextifyContext::CreateV8Context(
164167
IndexedPropertyDeleterCallback,
165168
PropertyEnumeratorCallback,
166169
IndexedPropertyDefinerCallback,
167-
data_wrapper);
170+
data_wrapper,
171+
PropertyHandlerFlags::kHasNoSideEffect);
168172

169173
object_template->SetHandler(config);
170174
object_template->SetHandler(indexed_config);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
const common = require('../common');
3+
common.skipIfInspectorDisabled();
4+
5+
// Test that if there is a side effect in a getter invoked through the vm
6+
// global proxy, Runtime.evaluate recognizes that.
7+
8+
const assert = require('assert');
9+
const inspector = require('inspector');
10+
const vm = require('vm');
11+
12+
const session = new inspector.Session();
13+
session.connect();
14+
15+
const context = vm.createContext({
16+
get a() {
17+
global.foo = '1';
18+
return 100;
19+
}
20+
});
21+
22+
session.post('Runtime.evaluate', {
23+
expression: 'a',
24+
throwOnSideEffect: true,
25+
contextId: 2 // context's id
26+
}, (error, res) => {
27+
assert.ifError(error);
28+
const { exception } = res.exceptionDetails;
29+
assert.strictEqual(exception.className, 'EvalError');
30+
assert(/Possible side-effect/.test(exception.description));
31+
32+
assert(context); // Keep 'context' alive and make linter happy.
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common');
3+
common.skipIfInspectorDisabled();
4+
5+
// Regression test for https://github.com/nodejs/node/issues/27518.
6+
7+
const assert = require('assert');
8+
const inspector = require('inspector');
9+
const vm = require('vm');
10+
11+
const session = new inspector.Session();
12+
session.connect();
13+
14+
const context = vm.createContext({
15+
a: 100
16+
});
17+
18+
session.post('Runtime.evaluate', {
19+
expression: 'a',
20+
throwOnSideEffect: true,
21+
contextId: 2 // context's id
22+
}, (error, res) => {
23+
assert.ifError(error),
24+
assert.deepStrictEqual(res, {
25+
result: {
26+
type: 'number',
27+
value: context.a,
28+
description: '100'
29+
}
30+
});
31+
});

0 commit comments

Comments
 (0)