diff options
-rw-r--r-- | src/node_crypto.cc | 29 | ||||
-rw-r--r-- | src/node_crypto.h | 3 | ||||
-rw-r--r-- | test/parallel/test-crypto-worker-thread.js | 18 |
3 files changed, 36 insertions, 14 deletions
diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 05da3af09c..ed886abd74 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3265,8 +3265,11 @@ size_t KeyObjectData::GetSymmetricKeySize() const { return symmetric_key_len_; } -Local<Function> KeyObjectHandle::Initialize(Environment* env, - Local<Object> target) { +Local<Function> KeyObjectHandle::Initialize(Environment* env) { + Local<Function> templ = env->crypto_key_object_handle_constructor(); + if (!templ.IsEmpty()) { + return templ; + } Local<FunctionTemplate> t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( KeyObjectHandle::kInternalFieldCount); @@ -3280,20 +3283,16 @@ Local<Function> KeyObjectHandle::Initialize(Environment* env, env->SetProtoMethod(t, "export", Export); auto function = t->GetFunction(env->context()).ToLocalChecked(); - target->Set(env->context(), - FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"), - function).Check(); - - return function; + env->set_crypto_key_object_handle_constructor(function); + return KeyObjectHandle::Initialize(env); } MaybeLocal<Object> KeyObjectHandle::Create( Environment* env, std::shared_ptr<KeyObjectData> data) { Local<Object> obj; - if (!env->crypto_key_object_handle_constructor() - ->NewInstance(env->context(), 0, nullptr) - .ToLocal(&obj)) { + Local<Function> fctun = KeyObjectHandle::Initialize(env); + if (!fctun->NewInstance(env->context(), 0, nullptr).ToLocal(&obj)) { return MaybeLocal<Object>(); } @@ -3466,6 +3465,11 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize( Local<Value> handle = KeyObjectHandle::Create(env, data_).ToLocalChecked(); Local<Function> key_ctor; + Local<Value> arg = FIXED_ONE_BYTE_STRING(env->isolate(), + "internal/crypto/keys"); + if (env->native_module_require()-> + Call(context, Null(env->isolate()), 1, &arg).IsEmpty()) + return {}; switch (data_->GetKeyType()) { case kKeyTypeSecret: key_ctor = env->crypto_key_object_secret_constructor(); @@ -6950,8 +6954,9 @@ void Initialize(Local<Object> target, Environment* env = Environment::GetCurrent(context); SecureContext::Initialize(env, target); - env->set_crypto_key_object_handle_constructor( - KeyObjectHandle::Initialize(env, target)); + target->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"), + KeyObjectHandle::Initialize(env)).Check(); env->SetMethod(target, "createNativeKeyObjectClass", CreateNativeKeyObjectClass); CipherBase::Initialize(env, target); diff --git a/src/node_crypto.h b/src/node_crypto.h index 63468e4f18..8f78103560 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -447,8 +447,7 @@ class KeyObjectData { class KeyObjectHandle : public BaseObject { public: - static v8::Local<v8::Function> Initialize(Environment* env, - v8::Local<v8::Object> target); + static v8::Local<v8::Function> Initialize(Environment* env); static v8::MaybeLocal<v8::Object> Create(Environment* env, std::shared_ptr<KeyObjectData> data); diff --git a/test/parallel/test-crypto-worker-thread.js b/test/parallel/test-crypto-worker-thread.js new file mode 100644 index 0000000000..c3b978ceb3 --- /dev/null +++ b/test/parallel/test-crypto-worker-thread.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +// Issue https://github.com/nodejs/node/issues/35263 +/* Description: test for checking keyobject passed to worker thread + does not crash */ +const { createSecretKey } = require('crypto'); + +const { Worker, isMainThread, workerData } = require('worker_threads'); + +if (isMainThread) { + const key = createSecretKey(Buffer.from('hello')); + new Worker(__filename, { workerData: key }); +} else { + console.log(workerData); +} |