Skip to content

Commit

Permalink
crypto: fix globalThis.crypto this check
Browse files Browse the repository at this point in the history
PR-URL: #45857
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
panva authored and RafaelGSS committed Jan 5, 2023
1 parent 97a8e05 commit f7dba5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
11 changes: 10 additions & 1 deletion lib/internal/process/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
} = require('internal/util');

const {
ERR_INVALID_THIS,
ERR_MANIFEST_ASSERT_INTEGRITY,
ERR_NO_CRYPTO,
} = require('internal/errors').codes;
Expand Down Expand Up @@ -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',
Expand Down
5 changes: 4 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,17 @@ 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];
let value;
let setterCalled = false;

function get() {
if (check !== undefined) {
FunctionPrototypeCall(check, this);
}
if (setterCalled) {
return value;
}
Expand Down
8 changes: 0 additions & 8 deletions test/wpt/status/WebCryptoAPI.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
}

0 comments on commit f7dba5b

Please sign in to comment.