vm,src: add property query interceptors · nodejs/node@9fd976b · GitHub
Skip to content

Commit 9fd976b

Browse files
legendecastargos
authored andcommitted
vm,src: add property query interceptors
Distinguish named property and indexed property when enumerating keys and handle query interceptions. Co-Authored-By: Michaël Zasso <targos@protonmail.com> PR-URL: #53517 Fixes: #52720 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 021e2cf commit 9fd976b

4 files changed

Lines changed: 255 additions & 5 deletions

File tree

src/node_contextify.cc

Lines changed: 116 additions & 5 deletions

src/node_contextify.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class ContextifyContext : public BaseObject {
9494
bool produce_cached_data,
9595
v8::Local<v8::Symbol> id_symbol,
9696
const errors::TryCatchScope& try_catch);
97+
static v8::Intercepted PropertyQueryCallback(
98+
v8::Local<v8::Name> property,
99+
const v8::PropertyCallbackInfo<v8::Integer>& args);
97100
static v8::Intercepted PropertyGetterCallback(
98101
v8::Local<v8::Name> property,
99102
const v8::PropertyCallbackInfo<v8::Value>& args);
@@ -113,6 +116,8 @@ class ContextifyContext : public BaseObject {
113116
const v8::PropertyCallbackInfo<v8::Boolean>& args);
114117
static void PropertyEnumeratorCallback(
115118
const v8::PropertyCallbackInfo<v8::Array>& args);
119+
static v8::Intercepted IndexedPropertyQueryCallback(
120+
uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& args);
116121
static v8::Intercepted IndexedPropertyGetterCallback(
117122
uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& args);
118123
static v8::Intercepted IndexedPropertySetterCallback(
@@ -127,6 +132,8 @@ class ContextifyContext : public BaseObject {
127132
const v8::PropertyCallbackInfo<void>& args);
128133
static v8::Intercepted IndexedPropertyDeleterCallback(
129134
uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& args);
135+
static void IndexedPropertyEnumeratorCallback(
136+
const v8::PropertyCallbackInfo<v8::Array>& args);
130137

131138
v8::Global<v8::Context> context_;
132139
std::unique_ptr<v8::MicrotaskQueue> microtask_queue_;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
require('../common');
3+
const vm = require('vm');
4+
const assert = require('assert');
5+
6+
// Regression of https://github.com/nodejs/node/issues/53346
7+
8+
const cases = [
9+
{
10+
get key() {
11+
return 'value';
12+
},
13+
},
14+
{
15+
// Intentionally single setter.
16+
// eslint-disable-next-line accessor-pairs
17+
set key(value) {},
18+
},
19+
{},
20+
{
21+
key: 'value',
22+
},
23+
(new class GetterObject {
24+
get key() {
25+
return 'value';
26+
}
27+
}()),
28+
(new class SetterObject {
29+
// Intentionally single setter.
30+
// eslint-disable-next-line accessor-pairs
31+
set key(value) {
32+
// noop
33+
}
34+
}()),
35+
[],
36+
[['key', 'value']],
37+
{
38+
__proto__: {
39+
key: 'value',
40+
},
41+
},
42+
];
43+
44+
for (const [idx, obj] of cases.entries()) {
45+
const ctx = vm.createContext(obj);
46+
const globalObj = vm.runInContext('this', ctx);
47+
const keys = Object.keys(globalObj);
48+
assert.deepStrictEqual(keys, Object.keys(obj), `Case ${idx} failed`);
49+
}
Lines changed: 83 additions & 0 deletions

0 commit comments

Comments
 (0)