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.

Answer from SilverlightFox on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › eval
eval() - JavaScript - MDN Web Docs - Mozilla
You can mitigate this risk by always passing TrustedScript objects instead of strings and enforcing trusted types. See Security considerations for more information. The eval() function evaluates JavaScript code represented as a string and returns its completion value.
Top answer
1 of 2
25

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.

2 of 2
8

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.

🌐
Sourcery
sourcery.ai › vulnerabilities › eval-injection-javascript
Code injection via eval() and Function constructor in JavaScript | Security Vulnerability Database | Sourcery
A critical security vulnerability where user-controlled input is passed to JavaScript's eval() function, Function() constructor, or similar dynamic code execution mechanisms without proper sanitization.
🌐
GitHub
github.com › btarr › node-eval-code-injection
GitHub - btarr/node-eval-code-injection: code injection vulnerability in JavaScript eval() function using node.js for in-class demonstration · GitHub
This takes the user input and places it right into an eval() call. eval() will take the string (including the user input) and execute it as JavaScript code. This statement will get the property of the colorHexes object that matches the input ...
Author   btarr
🌐
Gitbooks
ckarande.gitbooks.io › owasp-nodegoat-tutorial › content › tutorial › a1_-_server_side_js_injection.html
Server Side JS Injection | OWASP NodeGoat Tutorial
Web applications using the JavaScript eval()function to parse the incoming data without any type of input validation are vulnerable to this attack. An attacker can inject arbitrary JavaScript code to be executed on the server.
🌐
VK9 Security
vk9-sec.com › exploiting-javascript-eval-code-injection
Exploiting JavaScript EVAL() Code Injection | VK9 Security
January 20, 2024 - Its primary purpose is to execute arbitrary JavaScript code represented as a string. There are four standard ways to evaluate strings in JavaScript:
🌐
C# Corner
c-sharpcorner.com › article › eval-and-hackers-dream-in-javascript
Eval In JavaScript As A Hacker's Dream
March 14, 2023 - Here, I have put the method "alert()" with some string inside the eval() method as a string. eval() method evaluates it and generates JavaScript dynamic code alert() with value. The alert box will appear after the execution of the code, like below. According to a famous security website, Owasp, it is prone to direct dynamic code evaluation or Eval Injection.
🌐
Medium
medium.com › r3d-buck3t › eval-console-log-rce-warning-be68e92c3090
Eval(“console.log(‘RCE Warning’)”) | by Nairuz Abulhul | R3d Buck3T | Medium
February 2, 2022 - Eval function is a JavaScript function that evaluates inputs in strings and expressions and dynamically generates them into executable run-time code interpreted by the browser or the back-end server.
Find elsewhere
🌐
Snyk
snyk.io › blog › 5-ways-to-prevent-code-injection-in-javascript-and-node-js
5 ways to prevent code injection in JavaScript and Node.js | Snyk
April 6, 2021 - Code injection is a specific form of broad injection attacks, in which an attacker can send JavaScript or Node.js code that is interpreted by the browser or the Node.js runtime. The security vulnerability manifests when the interpreter is unable to make a distinction between the trusted code the developer intended, and the injected code that the attacker provided as an input. As a key secure coding convention, do not allow any dynamic code execution in the application. This means you should avoid language constructs like eval and code strings passed to setTimeout() or the Function constructor.
🌐
Codiga
codiga.io › blog › javascript-eval-best-practices
JavaScript eval security best practices - Codiga
January 29, 2023 - However, it also poses a security risk when used improperly. Here are a few reasons why eval() is considered insecure: Code injection: When eval() is passed untrusted data (such as user input), it can ...
🌐
Ronald James
ronaldjamesgroup.com › article › dont-use-eval-instead-call-javascript-functions-using-their-string-name-javascript-injection-attacks
Don't use eval, instead call JavaScript Functions using their String name & JavaScript Injection Attacks | Ronald James
Don't use eval, instead call JavaScript Functions using their String name & JavaScript Injection Attacks Generally, we use the evil eval() method to invoke a JavaScript function whose name ...
🌐
Semgrep
semgrep.dev › docs › cheat-sheets › javascript-code-injection
Prevent Code Injection in JavaScript - Semgrep
September 30, 2025 - javascript.lang.security.audit.vm-injection.vm-runincontext-context-injection · The eval() or new Function() function evaluates JavaScript code represented as a string. Executing JavaScript from a string is an enormous security risk.
🌐
Jsmon Blog
blogs.jsmon.sh › what-is-node-js-eval-injection-ways-to-exploit-examples-and-impact
What is Node.js eval() Injection? Exploits and Prevention
June 13, 2026 - The eval() function is a built-in JavaScript feature that evaluates a string as code. If the string represents an expression, eval() evaluates the expression; if it represents one or more JavaScript statements, eval() executes those statements.
🌐
Beagle Security
beaglesecurity.com › blog › vulnerability › eval-injection.html
Eval injection
November 14, 2023 - The eval () function takes a string of code as input and evaluates it as if it were written in the programming language of the web application. This means that an attacker can inject malicious code into the web application and have it executed ...
🌐
Medium
medium.com › @hxu0407 › the-dangers-of-eval-and-what-to-use-instead-d2010b57188b
The Dangers of `eval()` and What to Use Instead | by Hui | Medium
April 1, 2025 - The eval() function executes a string of JavaScript code, making it extremely flexible. const expression = 'var x = 5; x+1'; eval(expression) //Output: 6 · However, this flexibility comes with significant downsides — including security ...
🌐
DEV Community
dev.to › micheaol › the-evil-of-the-eval-function-in-javascript-ll5
The Evil of the eval() Function in JavaScript - DEV Community
March 9, 2024 - JavaScript is a versatile programming language no doubt, that powers a significant portion of the web. It provides developers with powerful tools to manipulate and execute code dynamically. One such tool is the eval() function, which evaluates a string of code as though it were part of the script.
🌐
Reddit
reddit.com › r/learnjavascript › help me understand why the "eval()" function is dangerous?
r/learnjavascript on Reddit: Help me understand why the "eval()" function is dangerous?
November 20, 2023 -

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?

Top answer
1 of 7
18
Here's an example: You're making a calculator and you allow people to share their results by adding the equation to the URL. For instance: calculator.net/calculate?equation=1+2 Then, in your Javascript, you get the value of the equation parameter and use eval to calculate the result. Users who visit your site will expect whatever value equation is to be evaluated, and if it's an invalid equation, for it not to be evaluated. However, if I wanted to be malicious, I could craft the URL into: calculator.net/calculate?equation=alert(1) When a user visits the link, you call eval on the value of the equation parameter, which is alert(1), which executes that JS. You can see how a malicious actor could execute some bad JS this way.
2 of 7
8
What I don't seem to understand is - how is this any different than just using the console to do the same thing? Only you (the end user) can use the console of your browser, and it can only be done intentionally. An eval() call in a script is called without the user's knowledge and could potentially execute injected code from another source, negatively impacting the user. The call itself isn't bad, it's just that using it securely safely is difficult. 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? The danger usually comes from the eval() being called on values that are supplied dynamically by the users browser, such as in a query string, header, cookie, etc. A script loaded into your page from another (compromised) source could modify those values, or just make another request to your service with fake values, to cause your eval to execute malicious code in the context of your site. This is possible because most "real" websites pull in JS scripts from many other places like CDNs and such.
🌐
OWASP Foundation
owasp.org › www-community › attacks › Direct_Dynamic_Code_Evaluation_Eval Injection
Direct Dynamic Code Evaluation - Eval Injection | OWASP Foundation
This attack consists of a script that does not properly validate user inputs in the page parameter. A remote user can supply a specially crafted URL to pass arbitrary code to an eval() statement, which results in code execution.
🌐
W3docs
w3docs.com › home › learn javascript › eval
JavaScript eval() | W3docs
In this example, if a user enters ... an alert box. An attacker could inject more harmful code, potentially compromising the entire system. eval() executes code in the same scope from which it’s called, hindering JavaScript engine optimizations....
🌐
www.secforce.com
secforce.com › blog › server-side-javascript-injection
Server Side JavaScript Injection
Server Side JavaScript injection is the ability for a user to inject code which will in turn be evaluated by the server, and therefore would allow an attacker to potentially execute arbitrary code under the context of the server and interaction ...