CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code ('Eval Injection')

What is CWE-95?

CWE-95 is Improper Neutralization of Directives in Dynamically Evaluated Code ('Eval Injection'). The flaw arises when the product receives input from an upstream component but fails to neutralize, or incorrectly neutralizes, code syntax before passing that input to dynamic evaluation such as `eval`. The database currently holds 92 vulnerabilities mapped to CWE-95 and 1 of them are in the CISA Known Exploited Vulnerabilities catalog.

OWASP TOP 10:2025 RANK5 — A05:2025 — Injection
Related CVES (365 days)92
AbstractionVariant
Likelihood of exploitMedium

Vulnerabilities mapped to CWE-95

92 vulnerabilities411% increase year over year

Vulnerabilities in CISA KEV for CWE-95

1 vulnerabilitiesNo change year over year

Official definition

ByMitre CWE

The product receives input from an upstream component, but it does not neutralize or incorrectly neutralizes code syntax before using the input in a dynamic evaluation call (e.g. "eval").

Detailed description

When external input is not kept separate from code syntax, attacker-controlled content can be interpreted by a dynamic evaluator as executable directives rather than data. Incomplete validation, incorrect canonicalization, or repeated decoding can allow dangerous syntax to appear after a validation step. The weakness commonly arises when dynamic evaluation is used to dispatch many functions or assign many variables, and it applies to interpreted environments including Java, JavaScript, Python, Perl, PHP, and Ruby, as well as other interpreted technologies.

Characteristics

The defining characteristic is that externally influenced data reaches code syntax or a dynamic evaluator instead of remaining data. The weakness commonly occurs in handler or dispatch procedures that need to invoke many functions or set many variables, and it is introduced during implementation. The record identifies Java, JavaScript, Python, Perl, PHP, Ruby, interpreted languages generally, and AI/ML technology as applicable environments.

Common consequences

Injected code may read restricted files, directories, or application data, bypass protection mechanisms, gain privileges, or assume another identity. It can execute unauthorized code or commands and alter application behavior, potentially affecting confidentiality, integrity, and availability. Actions performed by injected control code may not be logged, allowing activities to be hidden; in some cases, authentication can be controlled, creating a remote vulnerability.

ImpactScopeExplanation
Read Files or Directories, Read Application DataConfidentialityThe injected code could access restricted data / files.
Bypass Protection MechanismAccess ControlIn some cases, injectable code controls authentication; this may lead to a remote vulnerability.
Gain Privileges or Assume IdentityAccess ControlInjected code can access resources that the attacker is directly prevented from accessing.
Execute Unauthorized Code or CommandsIntegrity, Confidentiality, Availability, OtherCode 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 or at least modify what code can be executed.
Hide ActivitiesNon-RepudiationOften the actions performed by injected control code are unlogged.

Risk mitigations

Avoid dynamic evaluation. During architecture and implementation, refactor the design so it does not need eval or equivalent dynamic code evaluation.

Use strict allowlist validation. Treat all input as potentially malicious and accept only values that strictly conform to the specification. Check relevant properties including length, type, complete value range, missing or extra fields, syntax, consistency between related fields, and business rules. Reject nonconforming input or transform it into a conforming representation. Do not rely exclusively on denylists, although denylists can help identify attacks or reject obviously malformed input.

Canonicalize before validation. Decode and canonicalize input into the application's current internal representation before validating it. Avoid decoding the same input more than once, since double-decoding or related errors can bypass an allowlist. Canonicalization controls such as the OWASP ESAPI Canonicalization control may be used. Repeated canonicalization until the input stops changing can address double-decoding, but may alter inputs that legitimately contain encoded dangerous content.

Python-specific caution. ast.literal_eval is often suggested instead of eval because it is designed not to execute code. However, deeply nested input can still cause excessive memory or stack consumption, and the Python documentation discourages using it with untrusted data.

  1. Refactoring · Architecture and Design, ImplementationIf possible, refactor your code so that it does not need to use eval() at all.
  2. Input Validation · ImplementationAssume 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.
  3. ImplementationInputs should be decoded and canonicalized to the application's current internal representation before being validated (CWE-180, CWE-181). Make sure that your application does not inadvertently decode the same input twice (CWE-174). Such errors could be used to bypass allowlist schemes by introducing dangerous inputs after they have been checked. Use libraries such as the OWASP ESAPI Canonicalization control. Consider performing repeated canonicalization until your input does not change any more. This will avoid double-decoding and similar scenarios, but it might inadvertently modify inputs that are allowed to contain properly-encoded dangerous content.
  4. Implementation · Effectiveness: Discouraged Common PracticeFor 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].

Detection methods

Automated static analysis. Static Application Security Testing, or SAST, can identify some instances by modeling data flow and control flow and searching for patterns that connect input sources to dynamic-evaluation sinks or other external components. Its effectiveness is high, but it does not guarantee coverage of every case, especially where code generation or data flow is difficult to resolve statically.

MethodApproachEffectiveness
Automated Static AnalysisAutomated 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.)High

Representative vulnerabilities

The following are representative examples, not an exhaustive list:

  • CVE-2024-4181: An LLM application framework permits eval injection through a crafted response from a hosting provider.
  • CVE-2022-2054: A Python compiler uses eval to execute malicious strings as Python code.
  • CVE-2021-22204: A chain involving CWE-625, where an EXIF processor incorrectly determines the end of a string, enables eval injection; exploitation in the wild was recorded in CISA KEV.
  • CVE-2021-22205: A chain involving CWE-20, where a backslash followed by a newline bypasses validation and leads to eval injection; exploitation in the wild was recorded in CISA KEV.
  • CVE-2008-5071: Eval injection in a PHP program.
  • CVE-2002-1750, CVE-2008-5305, CVE-2002-1752, CVE-2002-1753: Eval injection or direct code injection into Perl eval, including a case involving an identifier intended to contain only hyphens and numbers.
  • CVE-2005-1527, CVE-2005-2837: Direct code injection into a Perl eval function.
  • CVE-2005-1921, CVE-2005-2498: Code injection into a PHP eval statement using constructs that should not be nested.
  • CVE-2005-3302, CVE-2007-1253: Code injection into Python eval, including one originating from a field in a formatted file.
  • CVE-2001-1471: A chain in which an invalid value prevents variable initialization, allowing attacker modification before the variables are inserted into a PHP eval statement.
  • CVE-2007-2713: Execution after a redirect triggers eval injection.

Below are representative vulnerabilities related to this CWE, prioritized by severity.

Sources (2)

CWE™ Program, operated by The MITRE Corporation. Copyright © 2006–2026, The MITRE Corporation. The MITRE Corporation hereby grants you a non-exclusive, royalty-free license to use CWE for research, development, and commercial purposes. CWE Terms of Use.

Learn more

Run an in-depth assessment with complete web risk management

CyStack VulnScan continuously discovers assets, validates vulnerabilities, and helps security teams prioritize remediation across the organization.