fix(es/minifier): Abort eval on valueOf or toString (#8763)

**Related issue:**

 - Closes #8704
 - Closes #8705
This commit is contained in:
Donny/강동윤 2024-03-20 11:35:24 +09:00 committed by GitHub
parent 6fba5f7c97
commit 9f98a7026d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 171 additions and 6 deletions

View File

@ -0,0 +1,64 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": false
},
"target": "es2022",
"loose": false,
"minify": {
"compress": {
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
},
"mangle": false
}
},
"module": {
"type": "es6"
},
"minify": false,
"isModule": true
}

View File

@ -0,0 +1 @@
console.log(Math.pow({ valueOf() { return 42 } }, 1))

View File

@ -0,0 +1,3 @@
console.log(Math.pow({
valueOf: ()=>42
}, 1));

View File

@ -0,0 +1,47 @@
{
"defaults": true,
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
}

View File

@ -0,0 +1 @@
console.log({ toString() { return 'swc' } } + '')

View File

@ -0,0 +1,3 @@
console.log({
toString: ()=>'swc'
} + '');

View File

@ -0,0 +1,47 @@
{
"defaults": true,
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
}

View File

@ -0,0 +1 @@
console.log(Math.pow({ valueOf() { return 42 } }, 1))

View File

@ -0,0 +1,3 @@
console.log(Math.pow({
valueOf: ()=>42
}, 1));

View File

@ -661,10 +661,6 @@ fn test_folding_mix_types_early() {
fn test_folding_add1() {
fold("x = null + true", "x=1");
fold_same("x = a + true");
fold("x = '' + {}", "x = \"[object Object]\"");
fold("x = [] + {}", "x = \"[object Object]\"");
fold("x = {} + []", "x = \"[object Object]\"");
fold("x = {} + ''", "x = \"[object Object]\"");
}
#[test]

View File

@ -935,7 +935,7 @@ pub trait ExprExt {
}
}
Expr::Tpl(..) | Expr::Object(ObjectLit { .. }) | Expr::Array(ArrayLit { .. }) => {
Expr::Tpl(..) => {
return (
Pure,
num_from_str(&match self.as_pure_string(ctx) {
@ -1058,7 +1058,6 @@ pub trait ExprExt {
}
Known(buf.into())
}
Expr::Object(ObjectLit { .. }) => Known(Cow::Borrowed("[object Object]")),
_ => Unknown,
}
}