diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-01-27 13:36:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-27 13:36:36 +0100 |
commit | 884143218fad0e18f7553aaf079d52de703f7601 (patch) | |
tree | 9b9e9d30ea647041438ef8fa974b8d4234cabf73 /ext/crypto/00_crypto.js | |
parent | dcf8f144ab0516936bfa4e93357d71f1732d880e (diff) |
refactor: update runtime code for primordial checks for "instanceof" (#13497)
Diffstat (limited to 'ext/crypto/00_crypto.js')
-rw-r--r-- | ext/crypto/00_crypto.js | 100 |
1 files changed, 60 insertions, 40 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js index aa328d1d7..5a42efbfd 100644 --- a/ext/crypto/00_crypto.js +++ b/ext/crypto/00_crypto.js @@ -15,18 +15,20 @@ const { TextEncoder, TextDecoder } = window.__bootstrap.encoding; const { - ArrayBuffer, + ArrayBufferPrototype, ArrayBufferIsView, ArrayPrototypeEvery, ArrayPrototypeFind, ArrayPrototypeIncludes, - BigInt64Array, - Int16Array, - Int32Array, - Int8Array, + BigInt64ArrayPrototype, + BigUint64ArrayPrototype, + Int16ArrayPrototype, + Int32ArrayPrototype, + Int8ArrayPrototype, JSONParse, JSONStringify, ObjectAssign, + ObjectPrototypeIsPrototypeOf, StringPrototypeToLowerCase, StringPrototypeToUpperCase, Symbol, @@ -34,10 +36,11 @@ SyntaxError, TypedArrayPrototypeSlice, TypeError, - Uint16Array, - Uint32Array, + Uint16ArrayPrototype, + Uint32ArrayPrototype, Uint8Array, - Uint8ClampedArray, + Uint8ArrayPrototype, + Uint8ClampedArrayPrototype, WeakMap, WeakMapPrototypeGet, WeakMapPrototypeSet, @@ -286,26 +289,26 @@ /** @returns {string} */ get type() { - webidl.assertBranded(this, CryptoKey); + webidl.assertBranded(this, CryptoKeyPrototype); return this[_type]; } /** @returns {boolean} */ get extractable() { - webidl.assertBranded(this, CryptoKey); + webidl.assertBranded(this, CryptoKeyPrototype); return this[_extractable]; } /** @returns {string[]} */ get usages() { - webidl.assertBranded(this, CryptoKey); + webidl.assertBranded(this, CryptoKeyPrototype); // TODO(lucacasonato): return a SameObject copy return this[_usages]; } /** @returns {object} */ get algorithm() { - webidl.assertBranded(this, CryptoKey); + webidl.assertBranded(this, CryptoKeyPrototype); // TODO(lucacasonato): return a SameObject copy return this[_algorithm]; } @@ -323,6 +326,7 @@ } webidl.configurePrototype(CryptoKey); + const CryptoKeyPrototype = CryptoKey.prototype; /** * @param {string} type @@ -429,7 +433,7 @@ * @returns {Promise<Uint8Array>} */ async digest(algorithm, data) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'digest' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 2, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -461,7 +465,7 @@ * @returns {Promise<any>} */ async encrypt(algorithm, key, data) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'encrypt' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 3, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -509,7 +513,7 @@ * @returns {Promise<any>} */ async decrypt(algorithm, key, data) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'decrypt' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 3, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -705,7 +709,7 @@ * @returns {Promise<any>} */ async sign(algorithm, key, data) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'sign' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 3, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -837,7 +841,7 @@ */ // deno-lint-ignore require-await async importKey(format, keyData, algorithm, extractable, keyUsages) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'importKey' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 4, { prefix }); format = webidl.converters.KeyFormat(format, { @@ -863,13 +867,19 @@ // 2. if (format !== "jwk") { - if (ArrayBufferIsView(keyData) || keyData instanceof ArrayBuffer) { + if ( + ArrayBufferIsView(keyData) || + ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, keyData) + ) { keyData = copyBuffer(keyData); } else { throw new TypeError("keyData is a JsonWebKey"); } } else { - if (ArrayBufferIsView(keyData) || keyData instanceof ArrayBuffer) { + if ( + ArrayBufferIsView(keyData) || + ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, keyData) + ) { throw new TypeError("keyData is not a JsonWebKey"); } } @@ -949,7 +959,7 @@ */ // deno-lint-ignore require-await async exportKey(format, key) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'exportKey' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 2, { prefix }); format = webidl.converters.KeyFormat(format, { @@ -999,7 +1009,7 @@ * @returns {Promise<ArrayBuffer>} */ async deriveBits(algorithm, baseKey, length) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'deriveBits' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 3, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -1047,7 +1057,7 @@ extractable, keyUsages, ) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'deriveKey' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 5, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -1142,7 +1152,7 @@ * @returns {Promise<boolean>} */ async verify(algorithm, key, signature, data) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'verify' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 4, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -1263,7 +1273,7 @@ * @returns {Promise<any>} */ async wrapKey(format, key, wrappingKey, wrapAlgorithm) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'wrapKey' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 4, { prefix }); format = webidl.converters.KeyFormat(format, { @@ -1396,7 +1406,7 @@ extractable, keyUsages, ) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'unwrapKey' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 7, { prefix }); format = webidl.converters.KeyFormat(format, { @@ -1554,7 +1564,7 @@ * @returns {Promise<any>} */ async generateKey(algorithm, extractable, keyUsages) { - webidl.assertBranded(this, SubtleCrypto); + webidl.assertBranded(this, SubtleCryptoPrototype); const prefix = "Failed to execute 'generateKey' on 'SubtleCrypto'"; webidl.requiredArguments(arguments.length, 3, { prefix }); algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { @@ -1580,12 +1590,14 @@ usages, ); - if (result instanceof CryptoKey) { + if (ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result)) { const type = result[_type]; if ((type === "secret" || type === "private") && usages.length === 0) { throw new DOMException("Invalid key usages", "SyntaxError"); } - } else if (result.privateKey instanceof CryptoKey) { + } else if ( + ObjectPrototypeIsPrototypeOf(CryptoKeyPrototype, result.privateKey) + ) { if (result.privateKey[_usages].length === 0) { throw new DOMException("Invalid key usages", "SyntaxError"); } @@ -1594,6 +1606,7 @@ return result; } } + const SubtleCryptoPrototype = SubtleCrypto.prototype; async function generateKey(normalizedAlgorithm, extractable, usages) { const algorithmName = normalizedAlgorithm.name; @@ -3826,7 +3839,7 @@ } getRandomValues(arrayBufferView) { - webidl.assertBranded(this, Crypto); + webidl.assertBranded(this, CryptoPrototype); const prefix = "Failed to execute 'getRandomValues' on 'Crypto'"; webidl.requiredArguments(arguments.length, 1, { prefix }); arrayBufferView = webidl.converters.ArrayBufferView(arrayBufferView, { @@ -3835,15 +3848,21 @@ }); if ( !( - arrayBufferView instanceof Int8Array || - arrayBufferView instanceof Uint8Array || - arrayBufferView instanceof Uint8ClampedArray || - arrayBufferView instanceof Int16Array || - arrayBufferView instanceof Uint16Array || - arrayBufferView instanceof Int32Array || - arrayBufferView instanceof Uint32Array || - arrayBufferView instanceof BigInt64Array || - arrayBufferView instanceof BigUint64Array + ObjectPrototypeIsPrototypeOf(Int8ArrayPrototype, arrayBufferView) || + ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, arrayBufferView) || + ObjectPrototypeIsPrototypeOf( + Uint8ClampedArrayPrototype, + arrayBufferView, + ) || + ObjectPrototypeIsPrototypeOf(Int16ArrayPrototype, arrayBufferView) || + ObjectPrototypeIsPrototypeOf(Uint16ArrayPrototype, arrayBufferView) || + ObjectPrototypeIsPrototypeOf(Int32ArrayPrototype, arrayBufferView) || + ObjectPrototypeIsPrototypeOf(Uint32ArrayPrototype, arrayBufferView) || + ObjectPrototypeIsPrototypeOf( + BigInt64ArrayPrototype, + arrayBufferView, + ) || + ObjectPrototypeIsPrototypeOf(BigUint64ArrayPrototype, arrayBufferView) ) ) { throw new DOMException( @@ -3861,12 +3880,12 @@ } randomUUID() { - webidl.assertBranded(this, Crypto); + webidl.assertBranded(this, CryptoPrototype); return core.opSync("op_crypto_random_uuid"); } get subtle() { - webidl.assertBranded(this, Crypto); + webidl.assertBranded(this, CryptoPrototype); return subtle; } @@ -3876,6 +3895,7 @@ } webidl.configurePrototype(Crypto); + const CryptoPrototype = Crypto.prototype; window.__bootstrap.crypto = { SubtleCrypto, |