Skip to content

Commit

Permalink
fix: avoid cross-realm objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Nov 22, 2022
1 parent 8241da7 commit 18c59c6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
3 changes: 1 addition & 2 deletions lib/dependencies/CommonJsImportsParserPlugin.js
Expand Up @@ -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}`,
Expand Down
11 changes: 5 additions & 6 deletions lib/dependencies/ImportParserPlugin.js
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -161,7 +160,7 @@ class ImportParserPlugin {
)
);
} else {
exclude = new RegExp(importOptions.webpackExclude);
exclude = importOptions.webpackExclude;
}
}
if (importOptions.webpackExports !== undefined) {
Expand Down
7 changes: 4 additions & 3 deletions lib/dependencies/WorkerPlugin.js
Expand Up @@ -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}`,
Expand Down Expand Up @@ -246,7 +245,9 @@ class WorkerPlugin {
} else {
Object.assign(
entryOptions,
importOptions.webpackEntryOptions
JSON.parse(
JSON.stringify(importOptions.webpackEntryOptions)
)
);
}
}
Expand Down
15 changes: 11 additions & 4 deletions lib/javascript/JavascriptParser.js
Expand Up @@ -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 });
}
}
}
Expand Down

0 comments on commit 18c59c6

Please sign in to comment.