Skip to content

Commit a6f58c0

Browse files
ThakurKarthikMylesBorins
authored andcommitted
crypto: set env values in KeyObject Deserialize method
Fixes: #35263 PR-URL: #35416 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent cd80195 commit a6f58c0

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

src/node_crypto.cc

+17-12
Original file line numberDiff line numberDiff line change
@@ -3245,8 +3245,11 @@ size_t KeyObjectData::GetSymmetricKeySize() const {
32453245
return symmetric_key_len_;
32463246
}
32473247

3248-
Local<Function> KeyObjectHandle::Initialize(Environment* env,
3249-
Local<Object> target) {
3248+
Local<Function> KeyObjectHandle::Initialize(Environment* env) {
3249+
Local<Function> templ = env->crypto_key_object_handle_constructor();
3250+
if (!templ.IsEmpty()) {
3251+
return templ;
3252+
}
32503253
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
32513254
t->InstanceTemplate()->SetInternalFieldCount(
32523255
KeyObjectHandle::kInternalFieldCount);
@@ -3260,20 +3263,16 @@ Local<Function> KeyObjectHandle::Initialize(Environment* env,
32603263
env->SetProtoMethod(t, "export", Export);
32613264

32623265
auto function = t->GetFunction(env->context()).ToLocalChecked();
3263-
target->Set(env->context(),
3264-
FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"),
3265-
function).Check();
3266-
3267-
return function;
3266+
env->set_crypto_key_object_handle_constructor(function);
3267+
return KeyObjectHandle::Initialize(env);
32683268
}
32693269

32703270
MaybeLocal<Object> KeyObjectHandle::Create(
32713271
Environment* env,
32723272
std::shared_ptr<KeyObjectData> data) {
32733273
Local<Object> obj;
3274-
if (!env->crypto_key_object_handle_constructor()
3275-
->NewInstance(env->context(), 0, nullptr)
3276-
.ToLocal(&obj)) {
3274+
Local<Function> fctun = KeyObjectHandle::Initialize(env);
3275+
if (!fctun->NewInstance(env->context(), 0, nullptr).ToLocal(&obj)) {
32773276
return MaybeLocal<Object>();
32783277
}
32793278

@@ -3446,6 +3445,11 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
34463445

34473446
Local<Value> handle = KeyObjectHandle::Create(env, data_).ToLocalChecked();
34483447
Local<Function> key_ctor;
3448+
Local<Value> arg = FIXED_ONE_BYTE_STRING(env->isolate(),
3449+
"internal/crypto/keys");
3450+
if (env->native_module_require()->
3451+
Call(context, Null(env->isolate()), 1, &arg).IsEmpty())
3452+
return {};
34493453
switch (data_->GetKeyType()) {
34503454
case kKeyTypeSecret:
34513455
key_ctor = env->crypto_key_object_secret_constructor();
@@ -6976,8 +6980,9 @@ void Initialize(Local<Object> target,
69766980

69776981
Environment* env = Environment::GetCurrent(context);
69786982
SecureContext::Initialize(env, target);
6979-
env->set_crypto_key_object_handle_constructor(
6980-
KeyObjectHandle::Initialize(env, target));
6983+
target->Set(env->context(),
6984+
FIXED_ONE_BYTE_STRING(env->isolate(), "KeyObjectHandle"),
6985+
KeyObjectHandle::Initialize(env)).Check();
69816986
env->SetMethod(target, "createNativeKeyObjectClass",
69826987
CreateNativeKeyObjectClass);
69836988
CipherBase::Initialize(env, target);

src/node_crypto.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ class KeyObjectData {
446446

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

452451
static v8::MaybeLocal<v8::Object> Create(Environment* env,
453452
std::shared_ptr<KeyObjectData> data);
+18
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)