Skip to content

Commit 1f15e34

Browse files
ThakurKarthikdanielleadams
authored andcommitted
crypto: set env values in KeyObject Deserialize method
Fixes: #35263 PR-URL: #35416 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 5a85d4f commit 1f15e34

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

src/node_crypto.cc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,8 +3265,11 @@ size_t KeyObjectData::GetSymmetricKeySize() const {
32653265
return symmetric_key_len_;
32663266
}
32673267

3268-
Local<Function> KeyObjectHandle::Initialize(Environment* env,
3269-
Local<Object> target) {
3268+
Local<Function> KeyObjectHandle::Initialize(Environment* env) {
3269+
Local<Function> templ = env->crypto_key_object_handle_constructor();
3270+
if (!templ.IsEmpty()) {
3271+
return templ;
3272+
}
32703273
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
32713274
t->InstanceTemplate()->SetInternalFieldCount(
32723275
KeyObjectHandle::kInternalFieldCount);
@@ -3280,20 +3283,16 @@ Local<Function> KeyObjectHandle::Initialize(Environment* env,
32803283
env->SetProtoMethod(t, "export", Export);
32813284

32823285
auto function = t->GetFunction(env->context()).ToLocalChecked();
3283-
target->Set(env->context(),
3284-
FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"),
3285-
function).Check();
3286-
3287-
return function;
3286+
env->set_crypto_key_object_handle_constructor(function);
3287+
return KeyObjectHandle::Initialize(env);
32883288
}
32893289

32903290
MaybeLocal<Object> KeyObjectHandle::Create(
32913291
Environment* env,
32923292
std::shared_ptr<KeyObjectData> data) {
32933293
Local<Object> obj;
3294-
if (!env->crypto_key_object_handle_constructor()
3295-
->NewInstance(env->context(), 0, nullptr)
3296-
.ToLocal(&obj)) {
3294+
Local<Function> fctun = KeyObjectHandle::Initialize(env);
3295+
if (!fctun->NewInstance(env->context(), 0, nullptr).ToLocal(&obj)) {
32973296
return MaybeLocal<Object>();
32983297
}
32993298

@@ -3466,6 +3465,11 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
34663465

34673466
Local<Value> handle = KeyObjectHandle::Create(env, data_).ToLocalChecked();
34683467
Local<Function> key_ctor;
3468+
Local<Value> arg = FIXED_ONE_BYTE_STRING(env->isolate(),
3469+
"internal/crypto/keys");
3470+
if (env->native_module_require()->
3471+
Call(context, Null(env->isolate()), 1, &arg).IsEmpty())
3472+
return {};
34693473
switch (data_->GetKeyType()) {
34703474
case kKeyTypeSecret:
34713475
key_ctor = env->crypto_key_object_secret_constructor();
@@ -6950,8 +6954,9 @@ void Initialize(Local<Object> target,
69506954

69516955
Environment* env = Environment::GetCurrent(context);
69526956
SecureContext::Initialize(env, target);
6953-
env->set_crypto_key_object_handle_constructor(
6954-
KeyObjectHandle::Initialize(env, target));
6957+
target->Set(env->context(),
6958+
FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"),
6959+
KeyObjectHandle::Initialize(env)).Check();
69556960
env->SetMethod(target, "createNativeKeyObjectClass",
69566961
CreateNativeKeyObjectClass);
69576962
CipherBase::Initialize(env, target);

src/node_crypto.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,7 @@ class KeyObjectData {
447447

448448
class KeyObjectHandle : public BaseObject {
449449
public:
450-
static v8::Local<v8::Function> Initialize(Environment* env,
451-
v8::Local<v8::Object> target);
450+
static v8::Local<v8::Function> Initialize(Environment* env);
452451

453452
static v8::MaybeLocal<v8::Object> Create(Environment* env,
454453
std::shared_ptr<KeyObjectData> data);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
// Issue https://github.com/nodejs/node/issues/35263
7+
/* Description: test for checking keyobject passed to worker thread
8+
does not crash */
9+
const { createSecretKey } = require('crypto');
10+
11+
const { Worker, isMainThread, workerData } = require('worker_threads');
12+
13+
if (isMainThread) {
14+
const key = createSecretKey(Buffer.from('hello'));
15+
new Worker(__filename, { workerData: key });
16+
} else {
17+
console.log(workerData);
18+
}

0 commit comments

Comments
 (0)