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

Description:
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").

Affected languages: Java, JavaScript, Python, Perl, PHP, Ruby, Interpreted
Affected technologies: AI/ML
Parent weaknesses: CWE-94

Consequences:
  - Confidentiality: Read Files or Directories
  - Access Control: Bypass Protection Mechanism
  - Access Control: Gain Privileges or Assume Identity
  - Integrity: Execute Unauthorized Code or Commands
  - Non-Repudiation: Hide Activities

Detection methods:
  - 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 havin

Mitigations:
  - [Architecture and Design] If possible, refactor your code so that it does not need to use eval() at all.
  - [Implementation] 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 pe
  - [Implementation] Inputs 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 introducin
  - [Implementation] 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 do

Demonstrative examples:
edit-config.pl: This CGI script is used to modify settings in a configuration file.
The script intends to take the 'action' parameter and invoke one of a variety of functions based on the value of that parameter - config_file_add_key(), config_file_set_key(), or config_file_delete_key(). It could set up a conditional to invoke each function separately, but eval() is a powerful way of doing the same thing in fewer lines of code, especially when a large number of functions or variables are involved. Unfortunately, in this case, the attacker can provide other values in the action parameter, such as:
use CGI qw(:standard); 
                      sub config_file_add_key { 
                         my ($fname, $key, $arg) = @_; 
                            # code to add a field/key to a file goe

This simple python3 script asks a user to supply a comma-separated list of numbers as input and adds them together.
The eval() function can take the user-supplied list and convert it into a Python list object, therefore allowing the programmer to use list comprehension methods to work with the data. However, if code is supplied to the eval() function, it will execute that code. For example, a malicious user could supply the following string:
def main(): 
                   
                    sum = 0 
		    try: 
		     
                      numbers = eval(input("Enter a comma-separated list of numbers: ")) 
		     
		    except SyntaxError: 
		     
		      print("Error: invalid input") 
		      return
		     
                    for num in numbers: 
                     
              

Real-world examples:
  - 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-2021-22204: 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-2021-22205: Chain: backslash followed by a newline can bypass a validation step (CWE-20), leading to eval injection (CWE-95), as exploited in the wild per CISA KEV.
  - CVE-2008-5071: Eval injection in PHP program.