Which of the Following Best Describes Code Injection? With Prevention
Code injection is best described as an attack where untrusted input is inserted into code that the application then interprets and executes in its own language. If a PHP, Python or JavaScript app passes user input to functions such as eval, the attacker's text runs as program code with the app's privileges. MITRE calls this CWE-94, Improper Control of Generation of Code.
Topic: Web & API Security in Cybersecurity & Ethical Hacking · Explainer
Also covers: Ethical Hacking
Quick answer: Code injection is best described as an attack where untrusted input is inserted into code that the application then interprets and executes in its own language. If a PHP, Python or JavaScript app passes user input to functions such as eval, the attacker's text runs as program code with the app's privileges. MITRE calls this CWE-94, Improper Control of Generation of Code. It differs from command injection, which runs operating system commands (CWE-78), and from SQL injection, which targets the database. Prevent it by never evaluating user input as code, using safe parsers and allow lists, and running apps with least privilege.
Key takeaways
- Code injection runs attacker input in the application's own language.
- Dynamic evaluation functions are the usual root cause.
- Command, SQL and template injection are related but distinct.
- Safe parsers and dispatch tables replace eval in almost every case.
- Static analysis rules can find dangerous functions before release.
Table of Contents
- Which Definition Best Describes Code Injection?
- How Does Code Injection Happen?
- How Is It Different From Other Injection Types?
- What Is the Impact of Code Injection?
- What Do Unsafe and Safe Patterns Look Like?
- How Can Teams Detect Code Injection Risks?
- How Do You Prevent Code Injection?
- References
Which Definition Best Describes Code Injection?
In exam questions, the correct choice is usually: the injection of untrusted data into an application in a way that the data is executed as code by the application's interpreter. MITRE's CWE-94 says the product builds all or part of a code segment from external input without neutralising elements that change its meaning. The OWASP code injection page adds that the attacker is limited only by what the language itself can do.
How Does Code Injection Happen?
The typical cause is a developer shortcut: evaluating a user-supplied expression for a calculator, building a function name from a parameter, deserialising data into live objects, or loading a file path chosen by the user. Each turns data into instructions. CWE-95 eval injection is the most common child weakness.
How Is It Different From Other Injection Types?
| Type | Interpreter | CWE |
|---|---|---|
| Code injection | Application language (PHP, Python, JavaScript) | CWE-94, CWE-95 |
| OS command injection | System shell | CWE-78 |
| SQL injection | Database | CWE-89 |
| Cross-site scripting | Victim's browser | CWE-79 |
| Template injection | Server template engine | CWE-1336 |
All sit under OWASP A03 Injection. Learn the database variant in our SQL injection guide and the browser variant in our XSS prevention guide.
What Is the Impact of Code Injection?
- Full control of the application process and its data.
- Reading secrets such as database passwords and API keys.
- Pivoting to the operating system, often leading to command execution.
- Installing web shells or backdoors for persistence.
Because impact is usually remote code execution, these flaws are rated high or critical.
What Do Unsafe and Safe Patterns Look Like?
Python: evaluating input as code versus parsing it as data.
# UNSAFE: input is executed as Python code
settings = eval(request.args['settings'])
# SAFE: only literals (numbers, strings, lists, dicts) are accepted
import ast
settings = ast.literal_eval(request.args['settings'])
# SAFER for web input: use JSON
import json
settings = json.loads(request.args['settings'])
Python's documentation describes ast.literal_eval as evaluating only literal structures. JavaScript: dynamic function names versus an allow list.
// UNSAFE: builds and runs code from a query parameter
const result = eval(req.query.op + '(a, b)');
// SAFE: dispatch table with an allow list
const ops = { add: (a, b) => a + b, sub: (a, b) => a - b };
const fn = ops[req.query.op];
if (!fn) return res.status(400).send('Unsupported operation');
const result = fn(a, b);
How Can Teams Detect Code Injection Risks?
| Method | Looks for |
|---|---|
| Static analysis (SAST) | eval, exec, Function constructor, unsafe deserialisation, dynamic includes |
| Code review checklist | Any path from request data to an interpreter |
| Dependency scanning | Libraries with known code injection CVEs |
| Runtime monitoring | Web processes spawning shells or unusual child processes |
A quick self-review on your own codebase:
grep -RnE 'eval\(|exec\(|new Function\(|pickle\.loads' --include=*.py --include=*.js --include=*.php .
How Do You Prevent Code Injection?
- Never pass user input to eval, exec or similar functions.
- Parse data with JSON or literal-only parsers.
- Map user choices to allow-listed functions.
- Avoid unsafe deserialisation of untrusted data.
- Run apps with least privilege and in containers.
- Follow the OWASP Injection Prevention Cheat Sheet; CISA's Secure by Design alert applies the same idea to command injection.
Testing apps you do not own breaches Sections 43 and 66 of the IT Act 2000; get written authorisation. WebAsha offers secure coding training.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0