From a6f58c08880e10f1138f476dfa1ec8402ff72539 Mon Sep 17 00:00:00 2001 From: ThakurKarthik Date: Tue, 29 Sep 2020 00:58:58 +0530 Subject: [PATCH] crypto: set env values in KeyObject Deserialize method Fixes: https://github.com/nodejs/node/issues/35263 PR-URL: https://github.com/nodejs/node/pull/35416 Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott --- src/node_crypto.cc | 29 +++++++++++++--------- src/node_crypto.h | 3 +-- test/parallel/test-crypto-worker-thread.js | 18 ++++++++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 test/parallel/test-crypto-worker-thread.js diff --git a/src/node_crypto.cc b/src/node_crypto.cc index db2e122ab07b2a..764dcb872093ba 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3245,8 +3245,11 @@ size_t KeyObjectData::GetSymmetricKeySize() const { return symmetric_key_len_; } -Local KeyObjectHandle::Initialize(Environment* env, - Local target) { +Local KeyObjectHandle::Initialize(Environment* env) { + Local templ = env->crypto_key_object_handle_constructor(); + if (!templ.IsEmpty()) { + return templ; + } Local t = env->NewFunctionTemplate(New); t->InstanceTemplate()->SetInternalFieldCount( KeyObjectHandle::kInternalFieldCount); @@ -3260,20 +3263,16 @@ Local 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 KeyObjectHandle::Create( Environment* env, std::shared_ptr data) { Local obj; - if (!env->crypto_key_object_handle_constructor() - ->NewInstance(env->context(), 0, nullptr) - .ToLocal(&obj)) { + Local fctun = KeyObjectHandle::Initialize(env); + if (!fctun->NewInstance(env->context(), 0, nullptr).ToLocal(&obj)) { return MaybeLocal(); } @@ -3446,6 +3445,11 @@ BaseObjectPtr NativeKeyObject::KeyObjectTransferData::Deserialize( Local handle = KeyObjectHandle::Create(env, data_).ToLocalChecked(); Local key_ctor; + Local 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(); @@ -6976,8 +6980,9 @@ void Initialize(Local 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 38fd806e62d154..52b3044bd08da0 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -446,8 +446,7 @@ class KeyObjectData { class KeyObjectHandle : public BaseObject { public: - static v8::Local Initialize(Environment* env, - v8::Local target); + static v8::Local Initialize(Environment* env); static v8::MaybeLocal Create(Environment* env, std::shared_ptr data); diff --git a/test/parallel/test-crypto-worker-thread.js b/test/parallel/test-crypto-worker-thread.js new file mode 100644 index 00000000000000..c3b978ceb3d37c --- /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); +}