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

Replace eval() with new Function() #963

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

duzun
Copy link

@duzun duzun commented Jan 29, 2020

Replace

eval(attrValue)

with

(new Function('return ('+attrValue+')'))()

Why?

eval() is evaluated in the scope where it is called, which exposes all the (private) variables to the string script being evaluated.
Besides security considerations, it disables mangling of all variables names in the scope (and all parent scopes) during code minification, because every variable could be potentially used in the eval().

The (new Function(str))() approach is much safer, cause it does not have access to the current scope.

Replace 
```js
eval(attrValue)
```
with
```js
(new Function('return ('+attrValue+')'))()
```

### Why?

`eval()` is evaluated in the scope where it is called, which exposes all the (private) variables to the string script being evaluated.
Besides security considerations, it disables mangling of all variables names in the scope (and all parent scopes) during code minification, because every variable could be potentially used in the `eval()`.

The `(new Function(str))()` approach is much safer, cause it does not have access to the current scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant