From f7dba5bef7accb64ca86cbb8bac7aa5480053a9f Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 16 Dec 2022 13:32:22 +0100 Subject: [PATCH] crypto: fix globalThis.crypto this check PR-URL: https://github.com/nodejs/node/pull/45857 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell --- lib/internal/process/pre_execution.js | 11 ++++++++++- lib/internal/util.js | 5 ++++- test/wpt/status/WebCryptoAPI.json | 8 -------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index c3e84854ccefdd..4c44927fb0c15a 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -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', diff --git a/lib/internal/util.js b/lib/internal/util.js index 38002b421376ec..0d769e061fe448 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -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; } diff --git a/test/wpt/status/WebCryptoAPI.json b/test/wpt/status/WebCryptoAPI.json index 78a79a71920c6f..bd1db9eba685b7 100644 --- a/test/wpt/status/WebCryptoAPI.json +++ b/test/wpt/status/WebCryptoAPI.json @@ -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" - ] - } } }