Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

security: avoid cross-realm objects #16500

Merged
merged 6 commits into from Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/dependencies/ImportParserPlugin.js
Expand Up @@ -137,7 +137,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 +146,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 +161,7 @@ class ImportParserPlugin {
)
);
} else {
exclude = new RegExp(importOptions.webpackExclude);
exclude = importOptions.webpackExclude;
}
}
if (importOptions.webpackExports !== undefined) {
Expand Down
18 changes: 14 additions & 4 deletions lib/javascript/JavascriptParser.js
Expand Up @@ -3635,17 +3635,27 @@ class JavascriptParser extends Parser {
return EMPTY_COMMENT_OPTIONS;
}
let options = {};
/** @type {unknown[]} */
let errors = [];
for (const comment of comments) {
const { value } = comment;
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);
for (let [key, val] of Object.entries(
vm.runInNewContext(`(function(){return {${value}};})()`)
)) {
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);
const newErr = new Error(String(e.message));
newErr.stack = String(e.stack);
Object.assign(newErr, { comment });
errors.push(newErr);
}
}
}
Expand Down