Skip to content

Commit

Permalink
crypto: set env values in KeyObject Deserialize method
Browse files Browse the repository at this point in the history
Fixes: nodejs#35263

PR-URL: nodejs#35416
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ThakurKarthik authored and joesepi committed Oct 22, 2020
1 parent 9c56134 commit 6a6e7af
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
29 changes: 17 additions & 12 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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>();
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-crypto-worker-thread.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 6a6e7af

Please sign in to comment.