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

Commit 3302bec

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 d39a902 commit 3302bec

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
@@ -1695,12 +1695,15 @@ void NativeKeyObject::Initialize(Environment* env, Local<Object> target) {
16951695
NativeKeyObject::CreateNativeKeyObjectClass);
16961696
SetMethod(
16971697
env->context(), target, "getKeyObjectSlots", NativeKeyObject::GetSlots);
1698+
SetMethodNoSideEffect(
1699+
env->context(), target, "isKeyObject", NativeKeyObject::IsKeyObject);
16981700
}
16991701

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

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

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