crypto: avoid throwing CryptoKey brand checks · nodejs/node@15e7884 · GitHub
Skip to content

Commit 15e7884

Browse files
panvaaduh95
authored andcommitted
crypto: avoid throwing CryptoKey brand checks
Use the native CryptoKey FunctionTemplate brand check instead of probing slot access and catching ERR_INVALID_THIS. Keep a private-field fast path for ordinary same-realm CryptoKeys. 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 3302bec commit 15e7884

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
@@ -1848,12 +1848,15 @@ void NativeCryptoKey::Initialize(Environment* env, Local<Object> target) {
18481848
NativeCryptoKey::CreateCryptoKeyClass);
18491849
SetMethod(
18501850
env->context(), target, "getCryptoKeySlots", NativeCryptoKey::GetSlots);
1851+
SetMethodNoSideEffect(
1852+
env->context(), target, "isCryptoKey", NativeCryptoKey::IsCryptoKey);
18511853
}
18521854

18531855
void NativeCryptoKey::RegisterExternalReferences(
18541856
ExternalReferenceRegistry* registry) {
18551857
registry->Register(NativeCryptoKey::CreateCryptoKeyClass);
18561858
registry->Register(NativeCryptoKey::GetSlots);
1859+
registry->Register(NativeCryptoKey::IsCryptoKey);
18571860
registry->Register(NativeCryptoKey::New);
18581861
}
18591862

@@ -1870,6 +1873,12 @@ bool NativeCryptoKey::HasInstance(Environment* env, Local<Value> value) {
18701873
return IsNativeCryptoKey(env, value);
18711874
}
18721875

1876+
void NativeCryptoKey::IsCryptoKey(const FunctionCallbackInfo<Value>& args) {
1877+
Environment* env = Environment::GetCurrent(args);
1878+
CHECK_EQ(args.Length(), 1);
1879+
args.GetReturnValue().Set(HasInstance(env, args[0]));
1880+
}
1881+
18731882
MaybeLocal<Value> NativeCryptoKey::Create(Environment* env,
18741883
const KeyObjectData& data,
18751884
Local<Value> algorithm,

‎src/crypto/crypto_keys.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ class NativeCryptoKey : public BaseObject {
278278
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
279279
static void CreateCryptoKeyClass(
280280
const v8::FunctionCallbackInfo<v8::Value>& args);
281+
static void IsCryptoKey(const v8::FunctionCallbackInfo<v8::Value>& args);
281282

282283
static v8::MaybeLocal<v8::Value> Create(Environment* env,
283284
const KeyObjectData& data,

‎typings/internalBinding/crypto.d.ts‎

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)