mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
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: https://github.com/nodejs/node/pull/65503 Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
1 parent
2e2dc0f556
commit
a13cc3f723
4 files changed
+17
-12
No files matched your search
@@ -16,6 +16,7 @@ const {
|
||||
createNativeKeyObjectClass,
|
||||
// eslint-disable-next-line no-restricted-syntax -- intended here
|
||||
getKeyObjectSlots: nativeGetKeyObjectSlots,
|
||||
isKeyObject: isNativeKeyObject,
|
||||
createCryptoKeyClass,
|
||||
// eslint-disable-next-line no-restricted-syntax -- intended here
|
||||
getCryptoKeySlots: nativeGetCryptoKeySlots,
|
||||
@@ -120,6 +121,7 @@ for (const m of [[kKeyEncodingPKCS1, 'pkcs1'], [kKeyEncodingPKCS8, 'pkcs8'],
|
||||
// slot tuple in a private field so no forgeable own Symbols are exposed on
|
||||
// public KeyObject instances.
|
||||
let getKeyObjectSlots; // Populated by the createNativeKeyObjectClass callback.
|
||||
let isKeyObject;
|
||||
|
||||
const kKeyObjectSlotType = 0;
|
||||
const kKeyObjectSlotHandle = 1;
|
||||
@@ -225,6 +227,10 @@ const {
|
||||
}
|
||||
|
||||
static {
|
||||
isKeyObject = (key) => {
|
||||
if (key == null || typeof key !== 'object') return false;
|
||||
return #slots in key || isNativeKeyObject(key);
|
||||
};
|
||||
getKeyObjectSlots = (key) => {
|
||||
if (!key || typeof key !== 'object')
|
||||
throw new ERR_INVALID_THIS('KeyObject');
|
||||
@@ -1044,18 +1050,6 @@ function getKeyObjectAsymmetricKeyDetails(key) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
function isKeyObject(obj) {
|
||||
if (obj == null || typeof obj !== 'object')
|
||||
return false;
|
||||
|
||||
try {
|
||||
getKeyObjectSlots(obj);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// CryptoKey is a plain JS class whose prototype's [[Prototype]] is
|
||||
// Object.prototype, as Web Crypto requires. Instance storage (type enum,
|
||||
// extractable, algorithm, usages mask, and the KeyObject handle) lives
|
||||
|
||||
@@ -1696,12 +1696,15 @@ void NativeKeyObject::Initialize(Environment* env, Local<Object> target) {
|
||||
NativeKeyObject::CreateNativeKeyObjectClass);
|
||||
SetMethod(
|
||||
env->context(), target, "getKeyObjectSlots", NativeKeyObject::GetSlots);
|
||||
SetMethodNoSideEffect(
|
||||
env->context(), target, "isKeyObject", NativeKeyObject::IsKeyObject);
|
||||
}
|
||||
|
||||
void NativeKeyObject::RegisterExternalReferences(
|
||||
ExternalReferenceRegistry* registry) {
|
||||
registry->Register(NativeKeyObject::CreateNativeKeyObjectClass);
|
||||
registry->Register(NativeKeyObject::GetSlots);
|
||||
registry->Register(NativeKeyObject::IsKeyObject);
|
||||
registry->Register(NativeKeyObject::New);
|
||||
}
|
||||
|
||||
@@ -1710,6 +1713,12 @@ bool NativeKeyObject::HasInstance(Environment* env, Local<Value> value) {
|
||||
return !t.IsEmpty() && t->HasInstance(value);
|
||||
}
|
||||
|
||||
void NativeKeyObject::IsKeyObject(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
CHECK_EQ(args.Length(), 1);
|
||||
args.GetReturnValue().Set(HasInstance(env, args[0]));
|
||||
}
|
||||
|
||||
void NativeKeyObject::New(const FunctionCallbackInfo<Value>& args) {
|
||||
Environment* env = Environment::GetCurrent(args);
|
||||
CHECK_EQ(args.Length(), 1);
|
||||
|
||||
@@ -209,6 +209,7 @@ class NativeKeyObject : public BaseObject {
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void CreateNativeKeyObjectClass(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsKeyObject(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
// True if `value` is a real NativeKeyObject instance. Uses the
|
||||
// FunctionTemplate stored on the Environment as a brand check.
|
||||
|
||||
Vendored
+1
@@ -939,6 +939,7 @@ export interface CryptoBinding {
|
||||
getExtraCACertificates(): string[];
|
||||
getFipsCrypto(): 0 | 1;
|
||||
getHashes(): string[];
|
||||
isKeyObject(key: unknown): boolean;
|
||||
getKeyObjectSlots(key: object): InternalCryptoBinding.KeyObjectSlots;
|
||||
getOpenSSLSecLevelCrypto(): number | undefined;
|
||||
getSSLCiphers(): string[];
|
||||
|
||||
Reference in new issue
Block a user