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

fix(es/minifier): Fix detection of direct eval #6215

Merged
merged 9 commits into from
Oct 21, 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
12 changes: 6 additions & 6 deletions crates/swc_ecma_minifier/tests/benches-full/terser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'exports',
'source-map'
], factory) : factory((global1 = 'undefined' != typeof globalThis ? globalThis : global1 || self).Terser = {}, global1.sourceMap);
}(this, function(exports, MOZ_SourceMap) {
}(this, function(exports1, MOZ_SourceMap) {
'use strict';
let mangle_options;
var def_is_string, def_find_defs, MOZ_SourceMap__default = MOZ_SourceMap && 'object' == typeof MOZ_SourceMap && 'default' in MOZ_SourceMap ? MOZ_SourceMap : {
Expand Down Expand Up @@ -4085,14 +4085,14 @@
AST_Node.from_mozilla_ast = function(node) {
var save_stack = FROM_MOZ_STACK;
FROM_MOZ_STACK = [];
var ast1 = from_moz(node);
return FROM_MOZ_STACK = save_stack, ast1;
var ast = from_moz(node);
return FROM_MOZ_STACK = save_stack, ast;
};
var TO_MOZ_STACK = null;
function to_moz(node) {
null === TO_MOZ_STACK && (TO_MOZ_STACK = []), TO_MOZ_STACK.push(node);
var ast1 = null != node ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
return TO_MOZ_STACK.pop(), 0 === TO_MOZ_STACK.length && (TO_MOZ_STACK = null), ast1;
var ast = null != node ? node.to_mozilla_ast(TO_MOZ_STACK[TO_MOZ_STACK.length - 2]) : null;
return TO_MOZ_STACK.pop(), 0 === TO_MOZ_STACK.length && (TO_MOZ_STACK = null), ast;
}
function to_moz_in_destructuring() {
for(var i = TO_MOZ_STACK.length; i--;)if (TO_MOZ_STACK[i] instanceof AST_Destructuring) return !0;
Expand Down Expand Up @@ -18048,5 +18048,5 @@
return error.defs;
}
}
exports._default_options = _default_options, exports._run_cli = run_cli, exports.minify = minify;
exports1._default_options = _default_options, exports1._run_cli = run_cli, exports1.minify = minify;
});

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@
}, y = Object.getOwnPropertyDescriptor;
if (y) try {
y({}, "");
} catch (r) {
} catch (r1) {
y = null;
}
var throwTypeError = function() {
Expand Down Expand Up @@ -1209,8 +1209,8 @@
if ("%" === t && "%" !== e) throw new n("invalid intrinsic syntax, expected closing `%`");
if ("%" === e && "%" !== t) throw new n("invalid intrinsic syntax, expected opening `%`");
var o = [];
return P(r, O, function(r, t, e, n1) {
o[o.length] = e ? P(n1, w, "$1") : t || r;
return P(r, O, function(r, t, e, n) {
o[o.length] = e ? P(n, w, "$1") : t || r;
}), o;
}, j = function(r, t) {
var o, e = r;
Expand Down Expand Up @@ -1293,7 +1293,7 @@
}, y = Object.getOwnPropertyDescriptor;
if (y) try {
y({}, "");
} catch (r) {
} catch (r1) {
y = null;
}
var throwTypeError = function() {
Expand Down Expand Up @@ -1607,8 +1607,8 @@
if ("%" === t && "%" !== e) throw new n("invalid intrinsic syntax, expected closing `%`");
if ("%" === e && "%" !== t) throw new n("invalid intrinsic syntax, expected opening `%`");
var o = [];
return m(r, h, function(r, t, e, n1) {
o[o.length] = e ? m(n1, O, "$1") : t || r;
return m(r, h, function(r, t, e, n) {
o[o.length] = e ? m(n, O, "$1") : t || r;
}), o;
}, E = function(r, t) {
var o, e = r;
Expand Down Expand Up @@ -2252,7 +2252,7 @@
}, y = Object.getOwnPropertyDescriptor;
if (y) try {
y({}, "");
} catch (r) {
} catch (r1) {
y = null;
}
var throwTypeError = function() {
Expand Down
1,972 changes: 986 additions & 986 deletions crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var c = a;
c();
var d = a.b;
d();
var e = eval;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an indirect eval, so it cannot use c nor b

e();
var v = a;
v();
var r = a.b;
r();
var b = eval;
b();
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(function (foo) {
(function(foo) {
foo();
(0, foo.bar)();
(0, eval)("console.log(foo);");
})();
(function (foo) {
(function(foo) {
var eval = console;
foo();
(0, foo.bar)();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(function(foo) {
(0, foo)();
(0, foo.bar)();
(function(o) {
(0, o)();
(0, o.bar)();
(0, eval)("console.log(foo);");
})();
(function(foo) {
(function(o) {
var eval = console;
(0, foo)();
(0, foo.bar)();
(0, o)();
(0, o.bar)();
(0, eval)("console.log(foo);");
})();
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ function f1() {
eval(x, y, z, e);
}
function p1() {
var a = foo(), b = bar(), eval = baz();
return a + b + eval;
var a = foo(), o = bar(), eval = baz();
return a + o + eval;
}
function p2() {
var a = foo(), b = bar(), eval = baz;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function f1(a, eval, c, d, e) {
return a("c") + eval;
function f1(n, eval, c, r, t) {
return n("c") + eval;
}
function f2(a, b, c, d, e) {
return a + eval("c");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function f1(a, eval, c, d, e) {
return a("c") + eval;
function f1(n, eval, c, r, t) {
return n("c") + eval;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this eval can be renamed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added code to explicitly prevent it because terser does so.

}
function f2(a, b, c, d, e) {
return a + eval("c");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl Scope {

for id in queue {
let fid = fast_id(id.clone());
if to.get(&fid).is_some() || previous.get(&fid).is_some() {
if to.get(&fid).is_some() || previous.get(&fid).is_some() || id.0 == js_word!("eval") {
continue;
}

Expand Down Expand Up @@ -273,7 +273,11 @@ impl Scope {

for id in queue {
let fid = fast_id(id.clone());
if preserved.contains(&id) || to.get(&fid).is_some() || previous.get(&fid).is_some() {
if preserved.contains(&id)
|| to.get(&fid).is_some()
|| previous.get(&fid).is_some()
|| id.0 == js_word!("eval")
{
continue;
}

Expand Down
14 changes: 11 additions & 3 deletions crates/swc_ecma_transforms_base/src/rename/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ impl Visit for EvalFinder {

fn visit_export_namespace_specifier(&mut self, _: &ExportNamespaceSpecifier) {}

fn visit_ident(&mut self, i: &Ident) {
if i.sym == js_word!("eval") {
self.found = true;
fn visit_callee(&mut self, c: &Callee) {
c.visit_children_with(self);

if let Callee::Expr(e) = c {
if let Expr::Ident(Ident {
sym: js_word!("eval"),
Comment on lines +37 to +39
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how you represent optional chains, but eval?.() is not a direct eval either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Nice catch, thank you!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it was OptCall which does not contain Callee

..
}) = &**e
{
self.found = true
}
}
}

Expand Down