Skip to content

Commit f7dba5b

Browse files
panvaRafaelGSS
authored andcommitted
crypto: fix globalThis.crypto this check
PR-URL: #45857 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 97a8e05 commit f7dba5b

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

lib/internal/process/pre_execution.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const {
2323
} = require('internal/util');
2424

2525
const {
26+
ERR_INVALID_THIS,
2627
ERR_MANIFEST_ASSERT_INTEGRITY,
2728
ERR_NO_CRYPTO,
2829
} = require('internal/errors').codes;
@@ -278,7 +279,15 @@ function setupWebCrypto() {
278279

279280
if (internalBinding('config').hasOpenSSL) {
280281
defineReplaceableLazyAttribute(
281-
globalThis, 'internal/crypto/webcrypto', ['crypto'], false
282+
globalThis,
283+
'internal/crypto/webcrypto',
284+
['crypto'],
285+
false,
286+
function cryptoThisCheck() {
287+
if (this !== globalThis && this != null)
288+
throw new ERR_INVALID_THIS(
289+
'nullish or must be the global object');
290+
}
282291
);
283292
exposeLazyInterfaces(
284293
globalThis, 'internal/crypto/webcrypto',

lib/internal/util.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -552,14 +552,17 @@ function defineLazyProperties(target, id, keys, enumerable = true) {
552552
ObjectDefineProperties(target, descriptors);
553553
}
554554

555-
function defineReplaceableLazyAttribute(target, id, keys, writable = true) {
555+
function defineReplaceableLazyAttribute(target, id, keys, writable = true, check) {
556556
let mod;
557557
for (let i = 0; i < keys.length; i++) {
558558
const key = keys[i];
559559
let value;
560560
let setterCalled = false;
561561

562562
function get() {
563+
if (check !== undefined) {
564+
FunctionPrototypeCall(check, this);
565+
}
563566
if (setterCalled) {
564567
return value;
565568
}

test/wpt/status/WebCryptoAPI.json

-8
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,5 @@
1717
},
1818
"historical.any.js": {
1919
"skip": "Not relevant in Node.js context"
20-
},
21-
"idlharness.https.any.js": {
22-
"fail": {
23-
"expected": [
24-
"CryptoKey interface: existence and properties of interface object",
25-
"Window interface: attribute crypto"
26-
]
27-
}
2820
}
2921
}

0 commit comments

Comments
 (0)