crypto: avoid throwing KeyObject brand checks · nodejs/node@a13cc3f · GitHub
Skip to content

Commit a13cc3f

Browse files
panvaaduh95
authored andcommitted
crypto: avoid throwing KeyObject brand checks
Use the native KeyObject FunctionTemplate brand check instead of probing slot access and catching ERR_INVALID_THIS. Keep a private-field fast path for ordinary same-realm KeyObjects. Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #65503 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 2e2dc0f commit a13cc3f

4 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎lib/internal/crypto/keys.js‎

Lines changed: 6 additions & 12 deletions

‎src/crypto/crypto_keys.cc‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,12 +1696,15 @@ void NativeKeyObject::Initialize(Environment* env, Local<Object> target) {
16961696
NativeKeyObject::CreateNativeKeyObjectClass);
16971697
SetMethod(
16981698
env->context(), target, "getKeyObjectSlots", NativeKeyObject::GetSlots);
1699+
SetMethodNoSideEffect(
1700+
env->context(), target, "isKeyObject", NativeKeyObject::IsKeyObject);
16991701
}
17001702

17011703
void NativeKeyObject::RegisterExternalReferences(
17021704
ExternalReferenceRegistry* registry) {
17031705
registry->Register(NativeKeyObject::CreateNativeKeyObjectClass);
17041706
registry->Register(NativeKeyObject::GetSlots);
1707+
registry->Register(NativeKeyObject::IsKeyObject);
17051708
registry->Register(NativeKeyObject::New);
17061709
}
17071710

@@ -1710,6 +1713,12 @@ bool NativeKeyObject::HasInstance(Environment* env, Local<Value> value) {
17101713
return !t.IsEmpty() && t->HasInstance(value);
17111714
}
17121715

1716+
void NativeKeyObject::IsKeyObject(const FunctionCallbackInfo<Value>& args) {
1717+
Environment* env = Environment::GetCurrent(args);
1718+
CHECK_EQ(args.Length(), 1);
1719+
args.GetReturnValue().Set(HasInstance(env, args[0]));
1720+
}
1721+
17131722
void NativeKeyObject::New(const FunctionCallbackInfo<Value>& args) {
17141723
Environment* env = Environment::GetCurrent(args);
17151724
CHECK_EQ(args.Length(), 1);

‎src/crypto/crypto_keys.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class NativeKeyObject : public BaseObject {
209209
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
210210
static void CreateNativeKeyObjectClass(
211211
const v8::FunctionCallbackInfo<v8::Value>& args);
212+
static void IsKeyObject(const v8::FunctionCallbackInfo<v8::Value>& args);
212213

213214
// True if `value` is a real NativeKeyObject instance. Uses the
214215
// FunctionTemplate stored on the Environment as a brand check.

‎typings/internalBinding/crypto.d.ts‎

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)