-
Notifications
You must be signed in to change notification settings - Fork 0
/
DangerousEval.js
40 lines (35 loc) · 1.89 KB
/
DangerousEval.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Using eval() in programming languages like JavaScript, Python, and PHP can demonstrate the potential dangers of executing dynamically generated code that may include user inputs. The eval() function evaluates or executes an argument as if it were an expression or a statement in the language's interpreter, which can introduce significant security risks, especially if the input can be influenced by an external user.
Why eval() is Dangerous
Remote Code Execution (RCE): If an attacker can inject malicious code that eval() executes, it can lead to unauthorized command execution on the server or client-side environment.
Data Leakage: Malicious scripts executed through eval() can access sensitive data from the application or user's environment.
Breaking Application Logic: Injected code can modify the way the application works, potentially causing persistent issues.
Demonstrating the Dangers with eval()
Here are demonstrations in JavaScript and Python to illustrate how using eval() can be risky:
JavaScript Example
Suppose you have a web page where JavaScript uses eval() to calculate an expression entered by the user:
html
Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Eval Example</title>
</head>
<body>
<input type="text" id="userInput" placeholder="Enter expression (e.g., 2 + 2)">
<button onclick="evaluateExpression()">Evaluate</button>
<script>
function evaluateExpression() {
var input = document.getElementById('userInput').value;
try {
var result = eval(input);
alert("Result: " + result);
} catch (e) {
alert("Error in expression");
}
}
</script>
</body>
</html>
Security Risk: If the user inputs something like alert(document.cookie),
it will execute and show the cookies of the current session, which could include sensitive session tokens.