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.

May 23, 2025 - 15:05
Updated: 
101.4k

Web & API Security in Cybersecurity & Ethical Hacking · Explainer

Ethical Hacking

Which of the Following Best Describes Code Injection? With Prevention

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?

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?

TypeInterpreterCWE
Code injectionApplication language (PHP, Python, JavaScript)CWE-94, CWE-95
OS command injectionSystem shellCWE-78
SQL injectionDatabaseCWE-89
Cross-site scriptingVictim's browserCWE-79
Template injectionServer template engineCWE-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?

MethodLooks for
Static analysis (SAST)eval, exec, Function constructor, unsafe deserialisation, dynamic includes
Code review checklistAny path from request data to an interpreter
Dependency scanningLibraries with known code injection CVEs
Runtime monitoringWeb 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?

  1. Never pass user input to eval, exec or similar functions.
  2. Parse data with JSON or literal-only parsers.
  3. Map user choices to allow-listed functions.
  4. Avoid unsafe deserialisation of untrusted data.
  5. Run apps with least privilege and in containers.
  6. 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

Code injection is inserting untrusted input into an application so that the application’s own interpreter executes it as program code.

MITRE CWE-94, Improper Control of Generation of Code, covers code injection, with CWE-95 covering eval injection specifically.

Code injection runs input in the application language such as Python or PHP, while command injection runs operating system shell commands.

They are related injection flaws, but SQL injection targets the database interpreter and has its own entry, CWE-89, in MITRE’s list.

eval executes any string as code, so if the string contains user input, an attacker can run their own logic with the application’s privileges.

Use JSON parsing or literal-only parsers such as Python’s ast.literal_eval for data, and allow-listed dispatch tables for choosing operations.

Use static analysis to flag eval and unsafe deserialisation, review data flows to interpreters, scan dependencies and monitor unusual process activity.

It often leads to remote code execution, theft of secrets, installation of backdoors and full compromise of the application server.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.