Safe property accessors
Methods declared through InstanceMethod throw a TypeError when invoked on a non-instance. Accessors declared through InstanceAccessor, however, do not, and I haven't found a straightforward way to prevent a segmentation fault:
> require('pqclean').kem.PublicKey.prototype.generateKey()
Uncaught TypeError: Illegal invocation
> require('pqclean').kem.PublicKey.prototype.algorithm
Segmentation fault (core dumped)
These properties are declared here:
auto publicKeyClass = KEMPublicKey::DefineClass(env, "PQCleanKEMPublicKey", {
Napi::ObjectWrap<KEMPublicKey>::InstanceAccessor("algorithm", &KEMPublicKey::GetAlgorithm, nullptr, napi_enumerable),
Napi::ObjectWrap<KEMPublicKey>::InstanceMethod("generateKey", &KEMPublicKey::GenerateKey),
Napi::ObjectWrap<KEMPublicKey>::InstanceMethod("export", &KEMPublicKey::Export)
});
obj.DefineProperty(Napi::PropertyDescriptor::Value("PublicKey", publicKeyClass, napi_enumerable));
I am not sure if this belongs here or in https://github.com/nodejs/node. Please feel free to transfer the issue there if you think it belongs there.
The root cause is that you can pass a v8::Signature when defining an instance method but not when defining an instance accessor. You can work around this with type tags, though verifying the tag entails an additional property retrieval.
NM. Looks like accessors are created using two FunctionTemplates. So, from https://github.com/nodejs/node/blob/main/src/js_native_api_v8.cc#L789-L812 it looks like we could simply add the v8::Signature to getter_tpl and setter_tpl and achieve the same kind of safety.
@tniessen how can you test to make sure accessors throw "Illegal invocation" when called with some empty object?
I tried adding
try {
TestConstructor.prototype.echo.call({}, 'hello');
}
catch (e) {
console.log(e);
}
try {
Object.getOwnPropertyDescriptor(TestConstructor.prototype, 'readwriteValue').get.call({});
} catch (e) {
console.log(e);
}
try {
Object.getOwnPropertyDescriptor(TestConstructor.prototype, 'readwriteValue').set.call({}, 5);
} catch (e) {
console.log(e);
}
and only the instance method correctly throws "Illegal invocation", because the property descriptor for a native-backed property does not expose the getter and setter individually.
... to https://github.com/nodejs/node/blob/main/test/js-native-api/test_constructor/test.js.
Oh, I see. You attempt to run the accessor on the prototype 🤦
@tniessen FWIW, I tried the following patch:
diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc
index 032a01053f..cc0d2cddce 100644
--- a/src/js_native_api_v8.cc
+++ b/src/js_native_api_v8.cc
@@ -791,11 +791,11 @@ napi_define_class(napi_env env,
v8::Local<v8::FunctionTemplate> setter_tpl;
if (p->getter != nullptr) {
STATUS_CALL(v8impl::FunctionCallbackWrapper::NewTemplate(
- env, p->getter, p->data, &getter_tpl));
+ env, p->getter, p->data, &getter_tpl, v8::Signature::New(isolate, tpl)));
}
if (p->setter != nullptr) {
STATUS_CALL(v8impl::FunctionCallbackWrapper::NewTemplate(
- env, p->setter, p->data, &setter_tpl));
+ env, p->setter, p->data, &setter_tpl, v8::Signature::New(isolate, tpl)));
}
tpl->PrototypeTemplate()->SetAccessorProperty(property_name,
diff --git a/test/js-native-api/test_constructor/test.js b/test/js-native-api/test_constructor/test.js
index 6fb33d9354..d5e60629cd 100644
--- a/test/js-native-api/test_constructor/test.js
+++ b/test/js-native-api/test_constructor/test.js
@@ -16,6 +16,10 @@ assert.strictEqual(test_object.readwriteValue, 1);
test_object.readwriteValue = 2;
assert.strictEqual(test_object.readwriteValue, 2);
+assert.throws(() => TestConstructor.prototype.echo.call({}, 'hello'))
+assert.throws(() => TestConstructor.prototype.readwriteValue)
+assert.throws(() => { TestConstructor.prototype.readwriteValue = 5 })
+
assert.throws(() => { test_object.readonlyValue = 3; },
/^TypeError: Cannot assign to read only property 'readonlyValue' of object '#<MyObject>'$/);
... and adding the v8::Signature to the accessors does not end up protecting them.
* frame #0: 0x0000000112122dbc test_constructor.node`GetValue(env=0x0000600003914000, info=0x000000016fdfdcb8) at test_constructor.c:101:10
frame #1: 0x0000000100181d84 node`v8impl::(anonymous namespace)::CallbackWrapperBase::InvokeCallback(this=0x000000016fdfdc58, env=0x0000600003914000)::'lambda'(napi_env__*)::operator()(napi_env__*) const at js_native_api_v8.cc:314:54
frame #2: 0x0000000100181c90 node`void napi_env__::CallIntoModule<v8impl::(anonymous namespace)::CallbackWrapperBase::InvokeCallback()::'lambda'(napi_env__*), v8impl::(anonymous namespace)::CallbackWrapperBase::InvokeCallback()::'lambda'(napi_env__*, v8::Local<v8::Value>)>(this=0x0000600003914000, call=0x000000016fdfdc58, handle_exception=0x000000016fdfdc50)::CallbackWrapperBase::InvokeCallback()::'lambda'(napi_env__*)&&, v8impl::(anonymous namespace)::CallbackWrapperBase::InvokeCallback()::'lambda'(napi_env__*, v8::Local<v8::Value>)&&) at js_native_api_v8.h:93:5
frame #3: 0x00000001001817d8 node`v8impl::(anonymous namespace)::CallbackWrapperBase::InvokeCallback(this=0x000000016fdfdcb8) at js_native_api_v8.cc:314:10
frame #4: 0x00000001001816e0 node`v8impl::(anonymous namespace)::FunctionCallbackWrapper::Invoke(info=0x000000016fdfdd08) at js_native_api_v8.cc:336:15
frame #5: 0x00000001025dd1d0 node`Builtins_CallApiCallback + 176
frame #6: 0x00000001025e9d14 node`Builtins_LoadIC_NoFeedback + 4660
frame #7: 0x0000000102755494 node`Builtins_GetNamedPropertyHandler + 5940
frame #8: 0x00000001025db6c4 node`Builtins_InterpreterEntryTrampoline + 260
AFAICT native-backed property accessors get called directly from the jitted code, so that's probably why the GetCompatibleReceiver mechanism is not used.
@gabrielschulhof Thank you for investigating the issue. While this is unfortunate, I am not sure if I want to implement a workaround based on TypeTag for this edge case.
@tniessen I agree. I think it's very unlikely that somebody will read/assign the property on the prototype.
ฉันชอบนะและเห็นด้วย แต่คุณคิดว่ามันจะเป็นประโยชน์ไหมหละ , ฉันหมายถึงหากจะเข้าถึงและจะพบเจอและสามารถนำมาปรับใช้ได้สะดวกได้ง่ายๆไหมนะครับ
@tniessen, from discussion in the node-api team meeting today it sounds like the discussion has answered the question and it's not possible to do the required checks with Javascript standard apis in V8 so its not possible.
Going to close out this issue. Please let us know if that was not the right thing to do.