Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAntoine du Hamel <duhamelantoine1995@gmail.com>2021-05-12 13:16:43 +0300
committerJames M Snell <jasnell@gmail.com>2021-05-19 19:21:37 +0300
commit2eeb4e1d944b4ebebcf80261d9250bc86eadc89a (patch)
tree8b0a3a4a23b4fa622c8b7d3d70d431e637d68378 /test
parentfc6e7e93e81aa70d55b374bd25a0d46f0f0523e3 (diff)
lib: make primordials Promise methods safe
`catch` and `finally` methods on %Promise.prototype% looks up the `then` property of the instance, making it at risk of prototype pollution. PR-URL: https://github.com/nodejs/node/pull/38650 Refs: https://tc39.es/ecma262/#sec-promise.prototype.catch Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/message/esm_display_syntax_error_import.out1
-rw-r--r--test/message/esm_display_syntax_error_import_module.out1
-rw-r--r--test/parallel/test-primordials-promise.js38
3 files changed, 40 insertions, 0 deletions
diff --git a/test/message/esm_display_syntax_error_import.out b/test/message/esm_display_syntax_error_import.out
index 8f94e4a5cf1..ad906af431b 100644
--- a/test/message/esm_display_syntax_error_import.out
+++ b/test/message/esm_display_syntax_error_import.out
@@ -6,3 +6,4 @@ SyntaxError: The requested module '../fixtures/es-module-loaders/module-named-ex
at async ModuleJob.run (node:internal/modules/esm/module_job:*:*)
at async Loader.import (node:internal/modules/esm/loader:*:*)
at async Object.loadESM (node:internal/process/esm_loader:*:*)
+ at async handleMainPromise (node:internal/modules/run_main:*:*)
diff --git a/test/message/esm_display_syntax_error_import_module.out b/test/message/esm_display_syntax_error_import_module.out
index fe13011c4aa..60a208d534a 100644
--- a/test/message/esm_display_syntax_error_import_module.out
+++ b/test/message/esm_display_syntax_error_import_module.out
@@ -6,3 +6,4 @@ SyntaxError: The requested module './module-named-exports.mjs' does not provide
at async ModuleJob.run (node:internal/modules/esm/module_job:*:*)
at async Loader.import (node:internal/modules/esm/loader:*:*)
at async Object.loadESM (node:internal/process/esm_loader:*:*)
+ at async handleMainPromise (node:internal/modules/run_main:*:*)
diff --git a/test/parallel/test-primordials-promise.js b/test/parallel/test-primordials-promise.js
new file mode 100644
index 00000000000..61651929384
--- /dev/null
+++ b/test/parallel/test-primordials-promise.js
@@ -0,0 +1,38 @@
+// Flags: --expose-internals
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+
+const {
+ PromisePrototypeCatch,
+ PromisePrototypeThen,
+ SafePromisePrototypeFinally,
+} = require('internal/test/binding').primordials;
+
+Promise.prototype.catch = common.mustNotCall();
+Promise.prototype.finally = common.mustNotCall();
+Promise.prototype.then = common.mustNotCall();
+
+assertIsPromise(PromisePrototypeCatch(Promise.reject(), common.mustCall()));
+assertIsPromise(PromisePrototypeThen(test(), common.mustCall()));
+assertIsPromise(SafePromisePrototypeFinally(test(), common.mustCall()));
+
+async function test() {
+ const catchFn = common.mustCall();
+ const finallyFn = common.mustCall();
+
+ try {
+ await Promise.reject();
+ } catch {
+ catchFn();
+ } finally {
+ finallyFn();
+ }
+}
+
+function assertIsPromise(promise) {
+ // Make sure the returned promise is a genuine %Promise% object and not a
+ // subclass instance.
+ assert.strictEqual(Object.getPrototypeOf(promise), Promise.prototype);
+}