CWE-94: Improper Control of Generation of Code ('Code Injection')
Also known as: Code Injection
The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
Last updated
Overview
CWE-94 (Improper Control of Generation of Code ('Code Injection')) is a base-level software weakness catalogued by MITRE in the Common Weakness Enumeration (CWE). It describes a recurring type of mistake that can lead to exploitable security vulnerabilities.
Real-world CVEs
2,740 recorded CVEs are caused by CWE-94 (Improper Control of Generation of Code ('Code Injection')), including 64 in CISA's KEV (Known Exploited Vulnerabilities) catalog. KEVs are shown first. 723 new CWE-94 CVEs have been recorded so far in 2026 (682 in 2025).
- CVE-2025-37164CISA KEVCritical · CVSS 10.0 · EPSS 100th2025-12-16
- CVE-2023-33246CISA KEV
Apache RocketMQ: Possible remote code execution vulnerability when using the update configuration function
Critical · CVSS 10.0 · EPSS 100th2023-05-24 - CVE-2022-24816CISA KEV
Improper Control of Generation of Code in jai-ext
Critical · CVSS 10.0 · EPSS 100th2022-04-13 - CVE-2022-22965CISA KEVCritical · CVSS 10.0 · EPSS 100th2022-04-01
- CVE-2022-22947CISA KEVCritical · CVSS 10.0 · EPSS 100th2022-03-03
- CVE-2021-22205CISA KEVCritical · CVSS 10.0 · EPSS 100th2021-04-23
- CVE-2019-4716CISA KEVCritical · CVSS 10.0 · EPSS 100th2019-12-18
- CVE-2019-7609CISA KEVCritical · CVSS 10.0 · EPSS 100th2019-03-25
- CVE-2018-14667CISA KEVCritical · CVSS 10.0 · EPSS 99th2018-11-06
- CVE-2018-7602CISA KEV
Drupal core - Highly critical - Remote Code Execution - SA-CORE-2018-004
Critical · CVSS 10.0 · EPSS 100th2018-07-19 - CVE-2017-7494CISA KEVCritical · CVSS 10.0 · EPSS 100th2017-05-30
- CVE-2014-6287CISA KEVCritical · CVSS 10.0 · EPSS 100th2014-10-07
Showing 12 of 2,740 recorded CWE-94 CVEs. Track new ones as they are published and get AI-written analysis and fixes.
Monitor CWE-94 vulnerabilitiesCommon consequences
What can happen when CWE-94 is exploited.
Bypass Protection Mechanism
Affects: Access Control
In some cases, injectable code controls authentication; this may lead to a remote vulnerability.
Gain Privileges or Assume Identity
Affects: Access Control
Injected code can access resources that the attacker is directly prevented from accessing.
Execute Unauthorized Code or Commands
Affects: Integrity, Confidentiality, Availability
When a product allows a user's input to contain code syntax, it might be possible for an attacker to craft the code in such a way that it will alter the intended control flow of the product. As a result, code injection can often result in the execution of arbitrary code. Code injection attacks can also lead to loss of data integrity in nearly all cases, since the control-plane data injected is always incidental to data recall or writing.
Hide Activities
Affects: Non-Repudiation
Often the actions performed by injected control code are unlogged.
How it happens
When it is introduced
Typically introduced during these phases of the software lifecycle.
Applies to
Languages
Technologies
How to prevent it
Practical mitigations for CWE-94, grouped by where in the lifecycle they apply.
Refactor your program so that you do not have to dynamically generate code.
Run your code in a "jail" or similar sandbox environment that enforces strict boundaries between the process and the operating system. This may effectively restrict which code can be executed by your product.
Examples include the Unix chroot jail and AppArmor. In general, managed code may provide some protection.
This may not be a feasible solution, and it only limits the impact to the operating system; the rest of your application may still be subject to compromise.
Be careful to avoid CWE-243 and other weaknesses related to jails.
Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does.
When performing input validation, consider all potentially relevant properties, including length, type of input, the full range of acceptable values, missing or extra inputs, syntax, consistency across related fields, and conformance to business rules. As an example of business rule logic, "boat" may be syntactically valid because it only contains alphanumeric characters, but it is not valid if the input is only expected to contain colors such as "red" or "blue."
Do not rely exclusively on looking for malicious or malformed inputs. This is likely to miss at least one undesirable input, especially if the code's environment changes. This can give attackers enough room to bypass the intended validation. However, denylists can be useful for detecting potential attacks or determining which inputs are so malformed that they should be rejected outright.
To reduce the likelihood of code injection, use stringent allowlists that limit which constructs are allowed. If you are dynamically constructing code that invokes a function, then verifying that the input is alphanumeric might be insufficient. An attacker might still be able to reference a dangerous function that you did not intend to allow, such as system(), exec(), or exit().
Use dynamic tools and techniques that interact with the product using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, and fault injection. The product's operation may slow down, but it should not become unstable, crash, or generate incorrect results.
Run the code in an environment that performs automatic taint propagation and prevents any command execution that uses tainted variables, such as Perl's "-T" switch. This will force the program to perform validation steps that remove the taint, although you must be careful to correctly validate your inputs so that you do not accidentally mark dangerous inputs as untainted (see CWE-183 and CWE-184).
Run the code in an environment that performs automatic taint propagation and prevents any command execution that uses tainted variables, such as Perl's "-T" switch. This will force the program to perform validation steps that remove the taint, although you must be careful to correctly validate your inputs so that you do not accidentally mark dangerous inputs as untainted (see CWE-183 and CWE-184).
For Python programs, it is frequently encouraged to use the ast.literal_eval() function instead of eval, since it is intentionally designed to avoid executing code. However, an adversary could still cause excessive memory or stack consumption via deeply nested structures [REF-1372], so the python documentation discourages use of ast.literal_eval() on untrusted data [REF-1373].
Effectiveness: Discouraged Common Practice
How to detect it
Automated Static Analysis
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Effectiveness: High
Code examples
Illustrative examples from MITRE showing how the weakness appears in code.
This example attempts to write user messages to a message file and allow users to view them.
Vulnerable example
$MessageFile = "messages.out";Attack input
name=h4x0rAttack input
<?php system("/bin/ls -l");?>edit-config.pl: This CGI script is used to modify settings in a configuration file.
Vulnerable example
use CGI qw(:standard);Attack input
add_key(",","); system("/bin/ls");Resulting query
config_file_add_key(",","); system("/bin/ls");This simple python3 script asks a user to supply a comma-separated list of numbers as input and adds them together.
Vulnerable example
def main():Attack input
__import__('subprocess').getoutput('rm -r *')Safe example
def main():The following code is a workflow job written using YAML. The code attempts to download pull request artifacts, unzip from the artifact called pr.zip and extract the value of the file NR into a variable "pr_number" that will be used later in another job. It attempts to create a github workflow environment variable, writing to $GITHUB_ENV. The environment variable value is retrieved from an external resource.
Vulnerable example
deploy:Illustrative examples
Real CVEs that MITRE cites as examples of this weakness.
- CVE-2021-22204CISA KEV— Chain: regex in EXIF processor code does not correctly determine where a string ends (CWE-625), enabling eval injection (CWE-95), as exploited in the wild per CISA KEV.
- CVE-2020-8218CISA KEV— "Code injection" in VPN product, as exploited in the wild per CISA KEV.
- CVE-2023-29374 — Math component in an LLM framework translates user input into a Python expression that is input into the Python exec() method, allowing code execution - one variant of a "prompt injection" attack.
- CVE-2024-5565 — Python-based library uses an LLM prompt containing user input to dynamically generate code that is then fed as input into the Python exec() method, allowing code execution - one variant of a "prompt injection" attack.
- CVE-2024-4181 — Framework for LLM applications allows eval injection via a crafted response from a hosting provider.
- CVE-2022-2054 — Python compiler uses eval() to execute malicious strings as Python code.
- CVE-2008-5071 — Eval injection in PHP program.
- CVE-2002-1750 — Eval injection in Perl program.
- CVE-2008-5305 — Eval injection in Perl program using an ID that should only contain hyphens and numbers.
- CVE-2002-1752 — Direct code injection into Perl eval function.
- CVE-2002-1753 — Eval injection in Perl program.
- CVE-2005-1527 — Direct code injection into Perl eval function.
- CVE-2005-2837 — Direct code injection into Perl eval function.
- CVE-2005-1921 — MFV. code injection into PHP eval statement using nested constructs that should not be nested.
- CVE-2005-2498 — MFV. code injection into PHP eval statement using nested constructs that should not be nested.
- CVE-2005-3302 — Code injection into Python eval statement from a field in a formatted file.
- CVE-2007-1253 — Eval injection in Python program.
- CVE-2001-1471 — chain: Resultant eval injection. An invalid value prevents initialization of variables, which can be modified by attacker and later injected into PHP eval statement.
- CVE-2002-0495 — Perl code directly injected into CGI library file from parameters to another CGI program.
- CVE-2005-1876 — Direct PHP code injection into supporting template file.
- CVE-2005-1894 — Direct code injection into PHP script that can be accessed by attacker.
- CVE-2003-0395 — PHP code from User-Agent HTTP header directly inserted into log file implemented as PHP script.
Terminology & mappings
Alternate terms
- Code Injection
Mapped taxonomies
- PLOVER: Code Evaluation and Injection (CODE)
- ISA/IEC 62443: Req CR 3.5 (Part 4-2)
- ISA/IEC 62443: Req SR 3.5 (Part 3-3)
- ISA/IEC 62443: Req SVV-1 (Part 4-1)
- ISA/IEC 62443: Req SVV-3 (Part 4-1)
Attack patterns
CAPEC attack patterns that exploit this weakness.
Frequently asked questions
Common questions about CWE-94.
- What is CWE-94?
- The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.
- What CVEs are caused by CWE-94?
- 2,740 recorded CVEs are attributed to CWE-94, including CVE-2025-37164, CVE-2023-33246, CVE-2022-24816. 64 are listed in CISA's Known Exploited Vulnerabilities (KEV) catalog.
- How do you prevent CWE-94?
- Refactor your program so that you do not have to dynamically generate code.
- How is CWE-94 detected?
- Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
- What are the consequences of CWE-94?
- Exploiting CWE-94 can lead to: Bypass Protection Mechanism, Gain Privileges or Assume Identity, Execute Unauthorized Code or Commands, Hide Activities.
- Is CWE-94 actively exploited?
- Yes. 64 CWE-94 vulnerabilities are in CISA's KEV catalog of actively exploited flaws, out of 2,740 recorded CVEs.
References
- MITRE CWE definition (CWE-94) (opens in a new tab)
- CWE-94 vulnerabilities on NVD (opens in a new tab)
- Learn: What is a CWE?
Weakness data is sourced from the MITRE CWE catalog (v4.20). CVE associations are aggregated and kept current by RadicalNotion.AI.
Stay ahead of CWE-94
Get alerted the moment a new CWE-94 vulnerability affects your stack, with AI-written analysis, severity context, and remediation guidance.