From 18c59c600d6d6149011f3c0a3b6d0e85b31329f3 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Tue, 22 Nov 2022 11:37:16 +0800 Subject: [PATCH] fix: avoid cross-realm objects --- lib/dependencies/CommonJsImportsParserPlugin.js | 3 +-- lib/dependencies/ImportParserPlugin.js | 11 +++++------ lib/dependencies/WorkerPlugin.js | 7 ++++--- lib/javascript/JavascriptParser.js | 15 +++++++++++---- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/dependencies/CommonJsImportsParserPlugin.js b/lib/dependencies/CommonJsImportsParserPlugin.js index e74e5c9743b..5a6e5faa4cd 100644 --- a/lib/dependencies/CommonJsImportsParserPlugin.js +++ b/lib/dependencies/CommonJsImportsParserPlugin.js @@ -236,8 +236,7 @@ class CommonJsImportsParserPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const e of commentErrors) { - const { comment } = e; + for (const { cause: e, comment } of commentErrors) { parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, diff --git a/lib/dependencies/ImportParserPlugin.js b/lib/dependencies/ImportParserPlugin.js index 151ff89adcc..3dc84bd83af 100644 --- a/lib/dependencies/ImportParserPlugin.js +++ b/lib/dependencies/ImportParserPlugin.js @@ -55,8 +55,7 @@ class ImportParserPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const e of commentErrors) { - const { comment } = e; + for (const { cause: e, comment } of commentErrors) { parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, @@ -137,7 +136,7 @@ class ImportParserPlugin { if (importOptions.webpackInclude !== undefined) { if ( !importOptions.webpackInclude || - importOptions.webpackInclude.constructor.name !== "RegExp" + !(importOptions.webpackInclude instanceof RegExp) ) { parser.state.module.addWarning( new UnsupportedFeatureWarning( @@ -146,13 +145,13 @@ class ImportParserPlugin { ) ); } else { - include = new RegExp(importOptions.webpackInclude); + include = importOptions.webpackInclude; } } if (importOptions.webpackExclude !== undefined) { if ( !importOptions.webpackExclude || - importOptions.webpackExclude.constructor.name !== "RegExp" + !(importOptions.webpackExclude instanceof RegExp) ) { parser.state.module.addWarning( new UnsupportedFeatureWarning( @@ -161,7 +160,7 @@ class ImportParserPlugin { ) ); } else { - exclude = new RegExp(importOptions.webpackExclude); + exclude = importOptions.webpackExclude; } } if (importOptions.webpackExports !== undefined) { diff --git a/lib/dependencies/WorkerPlugin.js b/lib/dependencies/WorkerPlugin.js index 5b68d84c06a..4dd193d44d7 100644 --- a/lib/dependencies/WorkerPlugin.js +++ b/lib/dependencies/WorkerPlugin.js @@ -203,8 +203,7 @@ class WorkerPlugin { parser.parseCommentOptions(expr.range); if (commentErrors) { - for (const e of commentErrors) { - const { comment } = e; + for (const { cause: e, comment } of commentErrors) { parser.state.module.addWarning( new CommentCompilationWarning( `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, @@ -246,7 +245,9 @@ class WorkerPlugin { } else { Object.assign( entryOptions, - importOptions.webpackEntryOptions + JSON.parse( + JSON.stringify(importOptions.webpackEntryOptions) + ) ); } } diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index c10c7b16eaf..c1e0d34dc3b 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -3641,11 +3641,18 @@ class JavascriptParser extends Parser { if (value && webpackCommentRegExp.test(value)) { // try compile only if webpack options comment is present try { - const val = vm.runInNewContext(`(function(){return {${value}};})()`); - Object.assign(options, val); + let val = vm.runInNewContext(`(function(){return {${value}};})()`); + const key = Object.getOwnPropertyNames(val)[0]; + if (!key) continue; + val = val[key]; + + if (typeof val === "object" && val !== null) { + if (val.constructor.name === "RegExp") val = new RegExp(val); + else val = JSON.parse(JSON.stringify(val)); + } + options[key] = val; } catch (e) { - e.comment = comment; - errors.push(e); + errors.push({ comment, cause: e }); } } }