From 14d01065f0e61845fb3a89e903214efe9807d0eb Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Wed, 18 Jan 2023 19:07:26 +0100 Subject: [PATCH] crypto: add CryptoKey Symbol.toStringTag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #45987 PR-URL: https://github.com/nodejs/node/pull/46042 Fixes: https://github.com/nodejs/node/issues/45987 Reviewed-By: Antoine du Hamel Reviewed-By: Tobias Nießen Reviewed-By: Juan José Arboleda --- lib/internal/crypto/keys.js | 8 ++++++++ test/parallel/test-webcrypto-keygen.js | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index 4303dc44fe60d7..ae16a0dcbf45a7 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -697,6 +697,14 @@ class CryptoKey extends JSTransferable { } } +ObjectDefineProperties(CryptoKey.prototype, { + [SymbolToStringTag]: { + __proto__: null, + configurable: true, + value: 'CryptoKey', + }, +}); + // All internal code must use new InternalCryptoKey to create // CryptoKey instances. The CryptoKey class is exposed to end // user code but is not permitted to be constructed directly. diff --git a/test/parallel/test-webcrypto-keygen.js b/test/parallel/test-webcrypto-keygen.js index b0977d5935082e..946a1e58889122 100644 --- a/test/parallel/test-webcrypto-keygen.js +++ b/test/parallel/test-webcrypto-keygen.js @@ -277,6 +277,8 @@ const vectors = { assert.strictEqual(publicKey.type, 'public'); assert.strictEqual(privateKey.type, 'private'); + assert.strictEqual(publicKey.toString(), '[object CryptoKey]'); + assert.strictEqual(privateKey.toString(), '[object CryptoKey]'); assert.strictEqual(publicKey.extractable, true); assert.strictEqual(privateKey.extractable, true); assert.deepStrictEqual(publicKey.usages, publicUsages); @@ -439,6 +441,8 @@ const vectors = { assert.strictEqual(publicKey.type, 'public'); assert.strictEqual(privateKey.type, 'private'); + assert.strictEqual(publicKey.toString(), '[object CryptoKey]'); + assert.strictEqual(privateKey.toString(), '[object CryptoKey]'); assert.strictEqual(publicKey.extractable, true); assert.strictEqual(privateKey.extractable, true); assert.deepStrictEqual(publicKey.usages, publicUsages); @@ -503,6 +507,7 @@ const vectors = { assert(isCryptoKey(key)); assert.strictEqual(key.type, 'secret'); + assert.strictEqual(key.toString(), '[object CryptoKey]'); assert.strictEqual(key.extractable, true); assert.deepStrictEqual(key.usages, usages); assert.strictEqual(key.algorithm.name, name); @@ -562,6 +567,7 @@ const vectors = { assert(isCryptoKey(key)); assert.strictEqual(key.type, 'secret'); + assert.strictEqual(key.toString(), '[object CryptoKey]'); assert.strictEqual(key.extractable, true); assert.deepStrictEqual(key.usages, usages); assert.strictEqual(key.algorithm.name, 'HMAC'); @@ -629,6 +635,8 @@ assert.throws(() => new CryptoKey(), { code: 'ERR_ILLEGAL_CONSTRUCTOR' }); assert.strictEqual(publicKey.type, 'public'); assert.strictEqual(privateKey.type, 'private'); + assert.strictEqual(publicKey.toString(), '[object CryptoKey]'); + assert.strictEqual(privateKey.toString(), '[object CryptoKey]'); assert.strictEqual(publicKey.extractable, true); assert.strictEqual(privateKey.extractable, true); assert.deepStrictEqual(publicKey.usages, publicUsages);