66 JSONStringify,
77 ObjectDefineProperties,
88 ReflectApply,
9+ ReflectConstruct,
910 SafeSet,
1011 SymbolToStringTag,
1112 StringPrototypeRepeat,
@@ -31,6 +32,7 @@ const { TextDecoder, TextEncoder } = require('internal/encoding');
3132
3233const {
3334 codes : {
35+ ERR_ILLEGAL_CONSTRUCTOR ,
3436 ERR_INVALID_ARG_TYPE ,
3537 ERR_INVALID_THIS ,
3638 }
@@ -70,12 +72,21 @@ const {
7072 randomUUID : _randomUUID ,
7173} = require ( 'internal/crypto/random' ) ;
7274
73- const randomUUID = ( ) => _randomUUID ( ) ;
75+ async function digest ( algorithm , data ) {
76+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
77+ return ReflectApply ( asyncDigest , this , arguments ) ;
78+ }
79+
80+ function randomUUID ( ) {
81+ if ( this !== crypto ) throw new ERR_INVALID_THIS ( 'Crypto' ) ;
82+ return _randomUUID ( ) ;
83+ }
7484
7585async function generateKey (
7686 algorithm ,
7787 extractable ,
7888 keyUsages ) {
89+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
7990 algorithm = normalizeAlgorithm ( algorithm ) ;
8091 validateBoolean ( extractable , 'extractable' ) ;
8192 validateArray ( keyUsages , 'keyUsages' ) ;
@@ -123,6 +134,7 @@ async function generateKey(
123134}
124135
125136async function deriveBits ( algorithm , baseKey , length ) {
137+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
126138 algorithm = normalizeAlgorithm ( algorithm ) ;
127139 if ( ! isCryptoKey ( baseKey ) )
128140 throw new ERR_INVALID_ARG_TYPE ( 'baseKey' , 'CryptoKey' , baseKey ) ;
@@ -194,6 +206,7 @@ async function deriveKey(
194206 derivedKeyAlgorithm ,
195207 extractable ,
196208 keyUsages ) {
209+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
197210 algorithm = normalizeAlgorithm ( algorithm ) ;
198211 derivedKeyAlgorithm = normalizeAlgorithm ( derivedKeyAlgorithm ) ;
199212 if ( ! isCryptoKey ( baseKey ) )
@@ -238,7 +251,11 @@ async function deriveKey(
238251 throw lazyDOMException ( 'Unrecognized name.' ) ;
239252 }
240253
241- return importKey ( 'raw' , bits , derivedKeyAlgorithm , extractable , keyUsages ) ;
254+ return ReflectApply (
255+ importKey ,
256+ this ,
257+ [ 'raw' , bits , derivedKeyAlgorithm , extractable , keyUsages ] ,
258+ ) ;
242259}
243260
244261async function exportKeySpki ( key ) {
@@ -415,6 +432,7 @@ async function exportKeyJWK(key) {
415432}
416433
417434async function exportKey ( format , key ) {
435+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
418436 validateString ( format , 'format' ) ;
419437 validateOneOf ( format , 'format' , kExportFormats ) ;
420438 if ( ! isCryptoKey ( key ) )
@@ -496,6 +514,7 @@ async function importKey(
496514 algorithm ,
497515 extractable ,
498516 keyUsages ) {
517+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
499518 validateString ( format , 'format' ) ;
500519 validateOneOf ( format , 'format' , kExportFormats ) ;
501520 if ( format !== 'node.keyObject' && format !== 'jwk' )
@@ -557,8 +576,9 @@ async function importKey(
557576// subtle.wrapKey() is essentially a subtle.exportKey() followed
558577// by a subtle.encrypt().
559578async function wrapKey ( format , key , wrappingKey , algorithm ) {
579+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
560580 algorithm = normalizeAlgorithm ( algorithm ) ;
561- let keyData = await exportKey ( format , key ) ;
581+ let keyData = await ReflectApply ( exportKey , this , [ format , key ] ) ;
562582
563583 if ( format === 'jwk' ) {
564584 if ( keyData == null || typeof keyData !== 'object' )
@@ -586,6 +606,7 @@ async function unwrapKey(
586606 unwrappedKeyAlgo ,
587607 extractable ,
588608 keyUsages ) {
609+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
589610 wrappedKey = getArrayBufferOrView ( wrappedKey , 'wrappedKey' ) ;
590611 unwrapAlgo = normalizeAlgorithm ( unwrapAlgo ) ;
591612 let keyData = await cipherOrWrap (
@@ -607,7 +628,11 @@ async function unwrapKey(
607628 }
608629 }
609630
610- return importKey ( format , keyData , unwrappedKeyAlgo , extractable , keyUsages ) ;
631+ return ReflectApply (
632+ importKey ,
633+ this ,
634+ [ format , keyData , unwrappedKeyAlgo , extractable , keyUsages ] ,
635+ ) ;
611636}
612637
613638function signVerify ( algorithm , key , data , signature ) {
@@ -654,10 +679,12 @@ function signVerify(algorithm, key, data, signature) {
654679}
655680
656681async function sign ( algorithm , key , data ) {
682+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
657683 return signVerify ( algorithm , key , data ) ;
658684}
659685
660686async function verify ( algorithm , key , signature , data ) {
687+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
661688 return signVerify ( algorithm , key , data , signature ) ;
662689}
663690
@@ -707,30 +734,39 @@ async function cipherOrWrap(mode, algorithm, key, data, op) {
707734}
708735
709736async function encrypt ( algorithm , key , data ) {
737+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
710738 return cipherOrWrap ( kWebCryptoCipherEncrypt , algorithm , key , data , 'encrypt' ) ;
711739}
712740
713741async function decrypt ( algorithm , key , data ) {
742+ if ( this !== subtle ) throw new ERR_INVALID_THIS ( 'SubtleCrypto' ) ;
714743 return cipherOrWrap ( kWebCryptoCipherDecrypt , algorithm , key , data , 'decrypt' ) ;
715744}
716745
717746// The SubtleCrypto and Crypto classes are defined as part of the
718747// Web Crypto API standard: https://www.w3.org/TR/WebCryptoAPI/
719748
720- class SubtleCrypto { }
721- const subtle = new SubtleCrypto ( ) ;
749+ class SubtleCrypto {
750+ constructor ( ) {
751+ throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
752+ }
753+ }
754+ const subtle = ReflectConstruct ( function ( ) { } , [ ] , SubtleCrypto ) ;
722755
723756class Crypto {
757+ constructor ( ) {
758+ throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
759+ }
760+
724761 get subtle ( ) {
762+ if ( this !== crypto ) throw new ERR_INVALID_THIS ( 'Crypto' ) ;
725763 return subtle ;
726764 }
727765}
728- const crypto = new Crypto ( ) ;
766+ const crypto = ReflectConstruct ( function ( ) { } , [ ] , Crypto ) ;
729767
730768function getRandomValues ( array ) {
731- if ( ! ( this instanceof Crypto ) ) {
732- throw new ERR_INVALID_THIS ( 'Crypto' ) ;
733- }
769+ if ( this !== crypto ) throw new ERR_INVALID_THIS ( 'Crypto' ) ;
734770 return ReflectApply ( _getRandomValues , this , arguments ) ;
735771}
736772
@@ -799,7 +835,7 @@ ObjectDefineProperties(
799835 enumerable : true ,
800836 configurable : true ,
801837 writable : true ,
802- value : asyncDigest ,
838+ value : digest ,
803839 } ,
804840 generateKey : {
805841 enumerable : true ,
0 commit comments