From a940dbacbafe9c0345d7a90e1b2f51f277d79639 Mon Sep 17 00:00:00 2001 From: himself65 Date: Mon, 13 Apr 2020 18:01:47 +0800 Subject: [PATCH] vm: throw error when duplicated exportNames in SyntheticModule Fixes: https://github.com/nodejs/node/issues/32806 --- lib/internal/vm/module.js | 12 +++++++++++- test/parallel/test-vm-module-basic.js | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index ed0dedd1e31b4f..992753ef680dbb 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -22,6 +22,7 @@ const { } = require('internal/util'); const { ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE, ERR_VM_MODULE_ALREADY_LINKED, ERR_VM_MODULE_DIFFERENT_CONTEXT, ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA, @@ -379,8 +380,17 @@ class SyntheticModule extends Module { constructor(exportNames, evaluateCallback, options = {}) { if (!ArrayIsArray(exportNames) || exportNames.some((e) => typeof e !== 'string')) { - throw new ERR_INVALID_ARG_TYPE('exportNames', 'Array of strings', + throw new ERR_INVALID_ARG_TYPE('exportNames', + 'Array of unique strings', exportNames); + } else { + exportNames.forEach((name, i) => { + if (exportNames.indexOf(name, i + 1) !== -1) { + throw new ERR_INVALID_ARG_VALUE(`exportNames.${name}`, + name, + 'is duplicated'); + } + }); } if (typeof evaluateCallback !== 'function') { throw new ERR_INVALID_ARG_TYPE('evaluateCallback', 'function', diff --git a/test/parallel/test-vm-module-basic.js b/test/parallel/test-vm-module-basic.js index 86e13f8b12bfa8..27b7f0476d502d 100644 --- a/test/parallel/test-vm-module-basic.js +++ b/test/parallel/test-vm-module-basic.js @@ -124,12 +124,22 @@ const util = require('util'); // Check to throws invalid exportNames { assert.throws(() => new SyntheticModule(undefined, () => {}, {}), { - message: 'The "exportNames" argument must be an Array of strings.' + - ' Received undefined', + message: 'The "exportNames" argument must be an ' + + 'Array of unique strings.' + + ' Received undefined', name: 'TypeError' }); } +// Check to throws duplicated exportNames +// https://github.com/nodejs/node/issues/32806 +{ + assert.throws(() => new SyntheticModule(['x', 'x'], () => {}, {}), { + message: 'The argument \'exportNames.x\' is duplicated. Received \'x\'', + name: 'TypeError', + }); +} + // Check to throws invalid evaluateCallback { assert.throws(() => new SyntheticModule([], undefined, {}), {