As B-Con mentioned, the attacker is not the one sitting at the computer so could be using the eval() already in your script as a means to pass malicious code to your site in order to exploit the current user's session in someway (e.g. a user following a malicious link).
The danger of eval() is when it is executed on unsanitised values, and can lead to a DOM Based XSS vulnerability.
e.g. consider the following code in your HTML (rather contrived, but it demonstrates the issue I hope)
<script>
eval('alert("Your query string was ' + unescape(document.location.search) + '");');
</script>
Now if the query string is ?foo you simply get an alert dialog stating the following: Your query string was ?foo
But what this code will allow a user to do is redirect users from their site to a URL such as http://www.example.com/page.htm?hello%22);alert(document.cookie+%22, where www.example.com is your website.
This modifies the code that is executed by eval() to
alert("Your query string was hello");
alert(document.cookie+"");
(New lines added by me for clarity). Now this could be doing something more malicious than showing the current cookie value, as the required code is simply passed on the query string by the attacker's link in encoded form. For example, it could be sending the cookie to the attacker's domain in a resource request, enabling the authentication session to be hijacked.
This applies to any value from user/external input that is unsanitised and executed directly in the eval(), not just the query string as shown here.
As B-Con mentioned, the attacker is not the one sitting at the computer so could be using the eval() already in your script as a means to pass malicious code to your site in order to exploit the current user's session in someway (e.g. a user following a malicious link).
The danger of eval() is when it is executed on unsanitised values, and can lead to a DOM Based XSS vulnerability.
e.g. consider the following code in your HTML (rather contrived, but it demonstrates the issue I hope)
<script>
eval('alert("Your query string was ' + unescape(document.location.search) + '");');
</script>
Now if the query string is ?foo you simply get an alert dialog stating the following: Your query string was ?foo
But what this code will allow a user to do is redirect users from their site to a URL such as http://www.example.com/page.htm?hello%22);alert(document.cookie+%22, where www.example.com is your website.
This modifies the code that is executed by eval() to
alert("Your query string was hello");
alert(document.cookie+"");
(New lines added by me for clarity). Now this could be doing something more malicious than showing the current cookie value, as the required code is simply passed on the query string by the attacker's link in encoded form. For example, it could be sending the cookie to the attacker's domain in a resource request, enabling the authentication session to be hijacked.
This applies to any value from user/external input that is unsanitised and executed directly in the eval(), not just the query string as shown here.
An attacker doesn't have access to the user's browser's Developer Tools. The attacker is likely not the user sitting at the computer.
The danger of eval() is that an attacker may be able to manipulate data that is eventually run through eval() in other ways. If the eval()'d string comes from an HTTP connection, the attacker may perform a MITM attack and modify the string. If the string comes from external storage, the attacker may have manipulated the data in that storage location. Etc.
Hi, beginner to web development here working through The Odin Project. I'm about to tackle a calculator project and was given explicit instruction NOT to use the eval() function due to it being dangerous. This piqued my curiosity and so I found out that it can be a security vulnerability since it allows an external user to inject code into the DOM. What I don't seem to understand is - how is this any different than just using the console to do the same thing? Also, if each person who accesses a webpage is served a copy of that page, how can e.g. client1 inject any code that could also affect client2 trying to access the page at a later time?