crypto: fix globalThis.crypto this check

PR-URL: https://github.com/nodejs/node/pull/45857
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Filip Skokan 2022-12-16 13:32:22 +01:00 committed by Node.js GitHub Bot
parent e8b56fb152
commit 3f3966cb82
3 changed files with 14 additions and 10 deletions

View File

@ -23,6 +23,7 @@ const {
} = require('internal/util');
const {
ERR_INVALID_THIS,
ERR_MANIFEST_ASSERT_INTEGRITY,
ERR_NO_CRYPTO,
} = require('internal/errors').codes;
@ -278,7 +279,15 @@ function setupWebCrypto() {
if (internalBinding('config').hasOpenSSL) {
defineReplaceableLazyAttribute(
globalThis, 'internal/crypto/webcrypto', ['crypto'], false
globalThis,
'internal/crypto/webcrypto',
['crypto'],
false,
function cryptoThisCheck() {
if (this !== globalThis && this != null)
throw new ERR_INVALID_THIS(
'nullish or must be the global object');
}
);
exposeLazyInterfaces(
globalThis, 'internal/crypto/webcrypto',

View File

@ -552,7 +552,7 @@ function defineLazyProperties(target, id, keys, enumerable = true) {
ObjectDefineProperties(target, descriptors);
}
function defineReplaceableLazyAttribute(target, id, keys, writable = true) {
function defineReplaceableLazyAttribute(target, id, keys, writable = true, check) {
let mod;
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
@ -560,6 +560,9 @@ function defineReplaceableLazyAttribute(target, id, keys, writable = true) {
let setterCalled = false;
function get() {
if (check !== undefined) {
FunctionPrototypeCall(check, this);
}
if (setterCalled) {
return value;
}

View File

@ -17,13 +17,5 @@
},
"historical.any.js": {
"skip": "Not relevant in Node.js context"
},
"idlharness.https.any.js": {
"fail": {
"expected": [
"CryptoKey interface: existence and properties of interface object",
"Window interface: attribute crypto"
]
}
}
}