Remote Code Execution Vulnerability Fix
In this tutorial, you'll learn about Remote Code Execution Vulnerability Fix. We cover key concepts, practical examples, and best practices.
An attacker can execute arbitrary system commands or code on your server — RCE occurs when user input is passed unsafely to functions like eval(), exec(), system(), or insecure deserialization.
Step-by-Step Fix
1. Fix eval() and exec() usage
# Wrong — evaluating user input directly
user_input = request.form.get("expression")
result = eval(user_input) # Can run arbitrary Python
# Right — use a safe alternative
import ast
user_input = request.form.get("expression")
try:
tree = ast.parse(user_input, mode="eval")
# Only allow simple arithmetic
for node in ast.walk(tree):
if not isinstance(node, (ast.Expression, ast.BinOp, ast.Num, ast.UnaryOp, ast.Add, ast.Sub, ast.Mult, ast.Div)):
raise ValueError("Invalid expression")
result = eval(compile(tree, "", "eval"))
except:
result = "Invalid input"
2. Fix command injection
import subprocess
import shlex
# Wrong — string formatting with user input
filename = request.form.get("filename")
subprocess.run(f"cat {filename}", shell=True)
# User can input: "; rm -rf /"
# Right — use subprocess with list arguments
filename = request.form.get("filename")
subprocess.run(["cat", filename]) # No shell injection
3. Fix unsafe deserialization
import pickle
import json
# Wrong — unsafe pickle deserialization
data = request.get_data()
obj = pickle.loads(data) # Can execute arbitrary code
# Right — use safe serialization
data = request.get_data()
obj = json.loads(data.decode("utf-8"))
4. Fix Node.js RCE
// Wrong — using eval on user input
const userCode = req.body.code;
const result = eval(userCode);
// Right — use vm module in a sandbox
const vm = require('vm');
const userCode = req.body.code;
const sandbox = { result: null };
try {
vm.runInNewContext(userCode, sandbox, { timeout: 1000 });
} catch (err) {
// Handle safely
}
Common Mistakes
| Mistake | Fix |
|---|---|
Using eval() or exec() with user input |
Never pass user input to code evaluation functions |
| Concatenating strings for shell commands | Use subprocess with list arguments to avoid shell injection |
| Unpickling/py-serializing untrusted data | Use JSON for serialization or validate input before unpickling |
Using shell=True in subprocess |
Omit shell=True or use shlex.quote() on user input |
| Unsafe template rendering | Use auto-escaped template engines (Jinja2, Handlebars) |
Prevention
- Never use
eval(),exec(), orFunction()constructor with user input. - Avoid
shell=Truein subprocess calls — use argument lists. - Use safe serialization formats (JSON, Protocol Buffers) instead of pickle.
- Run untrusted code in sandboxed environments (Docker, Firejail).
- Apply the principle of least privilege to the application process.
DodaTech Tools
Doda Browser includes a code security scanner that highlights dangerous function calls (eval, exec, system) in real time. DodaZIP safely archives untrusted archives without triggering deserialization exploits. Durga Antivirus Pro detects RCE payloads in both network traffic and file uploads.
Common Mistakes with vulnerability fix
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world RCE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro