From 2849283c4cebbfbf523cc24303941dc36df9332f Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 24 Feb 2022 12:19:36 +0100 Subject: [PATCH] crypto: remove non-standard `webcrypto.Crypto.prototype.CryptoKey` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `CryptoKey` is already available on the global object. PR-URL: https://github.com/nodejs/node/pull/42083 Reviewed-By: Matteo Collina Reviewed-By: Filip Skokan Reviewed-By: Michaƫl Zasso --- lib/internal/crypto/webcrypto.js | 24 +++++++++++++------ .../test-crypto-subtle-zero-length.js | 2 +- test/parallel/test-webcrypto-keygen.js | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index df4bb072043def..736137d406b7b0 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -5,6 +5,7 @@ const { JSONParse, JSONStringify, ObjectDefineProperties, + ObjectDefineProperty, ReflectApply, ReflectConstruct, SafeSet, @@ -28,6 +29,10 @@ const { validateString, } = require('internal/validators'); +const { + getOptionValue, +} = require('internal/options'); + const { TextDecoder, TextEncoder } = require('internal/encoding'); const { @@ -792,15 +797,20 @@ ObjectDefineProperties( writable: true, value: randomUUID, }, - CryptoKey: { - __proto__: null, - enumerable: true, - configurable: true, - writable: true, - value: CryptoKey, - } }); +if (getOptionValue('--no-experimental-global-webcrypto')) { + // For backward compatibility, keep exposing CryptoKey in the Crypto prototype + // when using the flag. + ObjectDefineProperty(Crypto.prototype, 'CryptoKey', { + __proto__: null, + enumerable: true, + configurable: true, + writable: true, + value: CryptoKey, + }); +} + ObjectDefineProperties( SubtleCrypto.prototype, { [SymbolToStringTag]: { diff --git a/test/parallel/test-crypto-subtle-zero-length.js b/test/parallel/test-crypto-subtle-zero-length.js index ffca84cf56129e..544374b7482a83 100644 --- a/test/parallel/test-crypto-subtle-zero-length.js +++ b/test/parallel/test-crypto-subtle-zero-length.js @@ -15,7 +15,7 @@ const crypto = require('crypto').webcrypto; { name: 'AES-GCM' }, false, [ 'encrypt', 'decrypt' ]); - assert(k instanceof crypto.CryptoKey); + assert(k instanceof CryptoKey); const e = await crypto.subtle.encrypt({ name: 'AES-GCM', diff --git a/test/parallel/test-webcrypto-keygen.js b/test/parallel/test-webcrypto-keygen.js index 5acea2debdd292..7d266e601b7295 100644 --- a/test/parallel/test-webcrypto-keygen.js +++ b/test/parallel/test-webcrypto-keygen.js @@ -9,7 +9,7 @@ if (!common.hasCrypto) const assert = require('assert'); const { types: { isCryptoKey } } = require('util'); const { - webcrypto: { subtle, CryptoKey }, + webcrypto: { subtle }, createSecretKey, KeyObject, } = require('crypto');