The product receives input from an upstream component, but it does not neutralize or incorrectly neutralizes code syntax before inserting the input into an executable resource, such as a library, configuration file, or template.

Primary

Implementation
REALIZATION: This weakness is caused during implementation of an architectural security tactic.

Implementation
This issue is frequently found in PHP applications that allow users to set configuration variables that are stored within executable PHP files. Technically, this could also be performed in some compiled code (e.g., by byte-patching an executable), although it is highly unlikely.

Confidentiality
Read Files or Directories
Read Application Data
The injected code could access restricted data / files.

Access Control
Bypass Protection Mechanism
In some cases, injectable code controls authentication; this may lead to a remote vulnerability.

Access Control
Gain Privileges or Assume Identity
Injected code can access resources that the attacker is directly prevented from accessing.

Integrity
Confidentiality
Availability
Other
Execute Unauthorized Code or Commands
Code injection attacks can lead to loss of data integrity in nearly all cases as the control-plane data injected is always incidental to data recall or writing. Additionally, code injection can often result in the execution of arbitrary code.

Non-Repudiation
Hide Activities
Often the actions performed by injected control code are unlogged.

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.)

Implementation
Input Validation

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.

Implementation
Output Encoding
Perform proper output validation and escaping to neutralize all code syntax from data written to code files.

This example attempts to write user messages to a message file and allow users to view them.

$MessageFile = "messages.out";if ($_GET["action"] == "NewMessage") {$name = $_GET["name"];$message = $_GET["message"];$handle = fopen($MessageFile, "a+");fwrite($handle, "<b>$name</b> says '$message'<hr>\n");fclose($handle);echo "Message Saved!<p>\n";}else if ($_GET["action"] == "ViewMessages") {include($MessageFile);}

While the programmer intends for the MessageFile to only include data, an attacker can provide a message such as:

name=h4x0rmessage=%3C?php%20system(%22/bin/ls%20-l%22);?%3E

which will decode to the following:

<?php system("/bin/ls -l");?>

The programmer thought they were just including the contents of a regular data file, but PHP parsed it and executed the code. Now, this code is executed any time people view messages.
Notice that XSS (CWE-79) is also possible in this situation.

CVE-2002-0495
Perl code directly injected into CGI library file from parameters to another CGI program.
https://www.cve.org/CVERecord?id=CVE-2002-0495

CVE-2005-1876
Direct PHP code injection into supporting template file.
https://www.cve.org/CVERecord?id=CVE-2005-1876

CVE-2005-1894
Direct code injection into PHP script that can be accessed by attacker.
https://www.cve.org/CVERecord?id=CVE-2005-1894

CVE-2003-0395
PHP code from User-Agent HTTP header directly inserted into log file implemented as PHP script.
https://www.cve.org/CVERecord?id=CVE-2003-0395

CVE-2007-6652
chain: execution after redirect allows non-administrator to perform static code injection.
https://www.cve.org/CVERecord?id=CVE-2007-6652

File or Directory

Direct Static Code Injection

SFP24
Tainted Input to Command

Allowed
This CWE entry is at the Base level of abstraction, which is a preferred level of abstraction for mapping to the root causes of vulnerabilities.
Carefully read both the name and description to ensure that this mapping is an appropriate fit. Do not try to 'force' a mapping to a lower-level Base/Variant simply to comply with this preferred level of abstraction.

"HTML injection" (see CWE-79: XSS) could be thought of as an example of this, but the code is injected and executed on the client side, not the server side. Server-Side Includes (SSI) are an example of direct static code injection.

PLOVER
2006-07-19
Draft 3
2006-07-19

Eric Dalci
Cigital
2008-07-01
updated Potential_Mitigations, Time_of_Introduction

CWE Content Team
MITRE
2008-09-08
updated Applicable_Platforms, Relationships, Other_Notes, Taxonomy_Mappings, Weakness_Ordinalities

CWE Content Team
MITRE
2009-05-27
updated Description, Name

CWE Content Team
MITRE
2010-04-05
updated Description, Name

CWE Content Team
MITRE
2010-06-21
updated Potential_Mitigations

CWE Content Team
MITRE
2011-06-01
updated Common_Consequences

CWE Content Team
MITRE
2012-05-11
updated Common_Consequences, Demonstrative_Examples, Relationships

CWE Content Team
MITRE
2012-10-30
updated Potential_Mitigations

CWE Content Team
MITRE
2013-02-21
updated Observed_Examples

CWE Content Team
MITRE
2014-06-23
updated Enabling_Factors_for_Exploitation, Other_Notes, Relationship_Notes

CWE Content Team
MITRE
2014-07-30
updated Relationships, Taxonomy_Mappings

CWE Content Team
MITRE
2017-05-03
updated Related_Attack_Patterns

CWE Content Team
MITRE
2017-11-08
updated Affected_Resources, Applicable_Platforms, Causal_Nature, Demonstrative_Examples, Enabling_Factors_for_Exploitation, Modes_of_Introduction, Relationships

CWE Content Team
MITRE
2020-02-24
updated Potential_Mitigations, Relationships, Taxonomy_Mappings

CWE Content Team
MITRE
2020-06-25
updated Potential_Mitigations

CWE Content Team
MITRE
2021-03-15
updated Demonstrative_Examples

CWE Content Team
MITRE
2021-10-28
updated Relationships

CWE Content Team
MITRE
2022-10-13
updated Relationships, Taxonomy_Mappings

CWE Content Team
MITRE
2023-01-31
updated Description

CWE Content Team
MITRE
2023-04-27
updated Modes_of_Introduction, Relationships, Time_of_Introduction

CWE Content Team
MITRE
2023-06-29
updated Mapping_Notes

CWE Content Team
MITRE
2025-12-11
4.19
2025-12-11
updated Detection_Factors, Relationships

Direct Static Code Injection
Insufficient Control of Directives in Statically Saved Code (Static Code Injection)
Improper Sanitization of Directives in Statically Saved Code ('Static Code Injection')