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: #35263

PR-URL: #35416
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
ThakurKarthik authored and MylesBorins committed Nov 16, 2020
1 parent cd80195 commit a6f58c0
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
Expand Up @@ -3245,8 +3245,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 @@ -3260,20 +3263,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 @@ -3446,6 +3445,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 @@ -6976,8 +6980,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
Expand Up @@ -446,8 +446,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
@@ -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 a6f58c0

Please sign in to comment.