Security

What is Dynamic Variable Evaluation (CWE-627)?

Learn how dynamic variable evaluation vulnerabilities work in real-world code, with examples and fixes for Java, Node.js, Python/Django, PHP. Scan your site...

SP
Shreya Pillai July 29, 2026 5 min read Security
AI-friendly summary

What it is: Dynamic Variable Evaluation (CWE-627) is a vulnerability where an attacker can manipulate variable names at runtime to read or write arbitrary data.

Why it matters: This allows attackers to access sensitive internal application state and execute unauthorized commands, compromising the integrity of the system.

How to fix it: Refactor code to avoid dynamic variable evaluation whenever possible or use allowlists for acceptable variable names.

TL;DR: Dynamic Variable Evaluation (CWE-627) is a vulnerability that allows attackers to manipulate variable names at runtime, compromising application integrity. Fix by refactoring code and using strict validation.

Field Value
CWE ID CWE-627
OWASP Category Not directly mapped
CAPEC None known
Typical Severity High
Affected Technologies any language that allows dynamic variable names at runtime
Detection Difficulty Moderate
Last Updated 2026-07-29

What is Dynamic Variable Evaluation?

Dynamic Variable Evaluation (CWE-627) is a type of vulnerability where an attacker can manipulate the name of variables or functions at runtime, leading to unauthorized access and execution. As defined by the MITRE Corporation under CWE-627, this weakness occurs when variable names are not controlled properly in languages that allow dynamic naming.

Quick Summary

Dynamic Variable Evaluation allows attackers to read or write arbitrary data within an application’s internal state, compromising integrity and confidentiality. This vulnerability can be exploited through user input manipulation, leading to severe security breaches. Jump to: Overview · How It Works · Business Impact · Attack Scenario · Detection · Fixing · Framework Fixes · Asking AI · Best Practices · FAQ · Related Vulnerabilities

Jump to: Quick Summary · Dynamic Variable Evaluation Overview · How Dynamic Variable Evaluation Works · Business Impact of Dynamic Variable Evaluation · Dynamic Variable Evaluation Attack Scenario · How to Detect Dynamic Variable Evaluation · How to Fix Dynamic Variable Evaluation · Framework-Specific Fixes for Dynamic Variable Evaluation · How to Ask AI to Check Your Code for Dynamic Variable Evaluation · Dynamic Variable Evaluation Best Practices Checklist · Dynamic Variable Evaluation FAQ · Vulnerabilities Related to Dynamic Variable Evaluation · References · Scan Your Own Site

Dynamic Variable Evaluation Overview

What: Dynamic Variable Evaluation is a vulnerability where an attacker can manipulate variable names at runtime, leading to unauthorized access and execution.

Why it matters: This weakness allows attackers to gain control over internal application state, execute arbitrary commands, and compromise system integrity. It poses significant risks to the confidentiality, integrity, and availability of applications.

Where it occurs: In any language or framework that supports dynamic variable naming based on user input without proper validation.

Who is affected: Developers using languages with dynamic variable capabilities and frameworks that do not enforce strict control over such variables.

Who is NOT affected: Systems where variable names are strictly controlled, and no external influence can alter them dynamically.

How Dynamic Variable Evaluation Works

Root Cause

The root cause of this vulnerability lies in the lack of proper validation for dynamically named variables. When an application allows users to specify or manipulate variable names at runtime without strict control, attackers can inject malicious input to read or write arbitrary data within the program’s internal state.

Attack Flow

  1. The attacker identifies a part of the code that uses dynamic variable naming.
  2. They craft input to influence these dynamically named variables.
  3. The application processes this input and alters the variable names based on user control.
  4. The attacker gains unauthorized access to sensitive data or executes arbitrary commands within the application.

Prerequisites to Exploit

  • Code that allows dynamic variable naming based on external input.
  • Lack of validation for such dynamic variable names.

Vulnerable Code

user_input = input("Enter a variable name: ")
globals()[user_input] = "sensitive_data"

This code snippet is vulnerable because it directly uses user-provided input to define global variables without any checks or validations. This allows an attacker to manipulate the variable names and access sensitive data.

Secure Code

allowed_variables = ['variable1', 'variable2']
user_input = input("Enter a variable name: ")
if user_input in allowed_variables:
    globals()[user_input] = "sensitive_data"
else:
    raise ValueError('Invalid variable name')

The secure code ensures that only predefined and trusted variable names are used, preventing unauthorized access to sensitive data.

Business Impact of Dynamic Variable Evaluation

Confidentiality

  • Data Exposure: Sensitive internal variables can be accessed by attackers.
  • Financial Losses: Data breaches due to exposure of confidential information may result in financial penalties and loss of trust from customers.

Integrity

  • Uncontrolled Modifications: Attackers can modify application state, leading to unauthorized changes in data integrity.
  • Reputation Damage: Unauthorized modifications can harm the reputation of an organization if such actions are detected by users or regulatory bodies.

Availability

  • System Disruption: Exploitation may lead to system instability and downtime as a result of uncontrolled variable manipulations.

Dynamic Variable Evaluation Attack Scenario

  1. The attacker identifies that the application uses dynamic variable names based on user input.
  2. They craft an HTTP request with a payload designed to influence these variables, such as ?variable_name=secret_data.
  3. The application processes this input and sets or modifies the specified variable without validation.
  4. The attacker gains unauthorized access to sensitive data stored in those variables.

How to Detect Dynamic Variable Evaluation

Manual Testing

  • Review code for any use of dynamic variable naming based on user input.
  • Check if there are proper validations in place for dynamically named variables.
  • Verify that only allowed and predefined variable names are used.

Automated Scanners (SAST / DAST)

Static analysis can detect the presence of functions or methods that allow dynamic variable creation. Dynamic testing is necessary to confirm whether these patterns can be exploited during runtime.

PenScan Detection

PenScan’s automated scanners such as ZAP, Nuclei, Wapiti, and Nikto are capable of identifying instances where dynamic variable evaluation might occur.

False Positive Guidance

False positives may arise when the code uses dynamic variable names for legitimate purposes without introducing security risks. Ensure that only controlled and validated variables are dynamically named to distinguish between real vulnerabilities and benign usage patterns.

How to Fix Dynamic Variable Evaluation

  • Refactor the code to avoid dynamic variable evaluation whenever possible.
  • Use allowlists of acceptable variable or function names.
  • For function names, ensure proper argument validation to prevent unexpected null arguments.
  • Implement strict input validation for any user-controlled inputs that influence variable names.

Framework-Specific Fixes for Dynamic Variable Evaluation

Java

List<String> allowedVariables = Arrays.asList("variable1", "variable2");
String userInput = request.getParameter("variable_name");
if (allowedVariables.contains(userInput)) {
    Object value = new HashMap<String, Object>() ;
} else {
    throw new IllegalArgumentException("Invalid variable name");
}

Node.js

const allowedVariables = ['variable1', 'variable2'];
let userInput = req.query.variable_name;
if (allowedVariables.includes(userInput)) {
    global[userInput] = 'sensitive_data';
} else {
    res.status(400).send('Invalid variable name');
}

Python/Django

ALLOWED_VARIABLES = ['variable1', 'variable2']
user_input = request.GET.get('variable_name')
if user_input in ALLOWED_VARIABLES:
    globals()[user_input] = "sensitive_data"
else:
    raise ValueError("Invalid variable name")

PHP

$allowedVariables = array('variable1', 'variable2');
$userInput = $_GET['variable_name'];
if (in_array($userInput, $allowedVariables)) {
    $$userInput = 'sensitive_data';
} else {
    http_response_code(400);
}

How to Ask AI to Check Your Code for Dynamic Variable Evaluation

Copy-paste prompt

Review the following [language] code block for potential CWE-627 Dynamic Variable Evaluation vulnerabilities and rewrite it using strict validation: [paste code here]

Dynamic Variable Evaluation Best Practices Checklist

✅ Refactor code to avoid dynamic variable evaluation whenever possible.

✅ Use allowlists of acceptable variable or function names.

✅ Ensure proper argument validation when calling functions based on user input.

✅ Implement strict input validation for any user-controlled inputs that influence variable names.

✅ Verify that only allowed and predefined variable names are used in the application.

Dynamic Variable Evaluation FAQ

How does dynamic variable evaluation work in code?

In languages that allow dynamic variable names at runtime, an attacker can manipulate these names to read or write arbitrary variables and functions.

What are the potential impacts of CWE-627 vulnerabilities?

An attacker could gain unauthorized access to internal program data and execute arbitrary commands within the application.

How do I detect dynamic variable evaluation in my codebase?

Use manual testing techniques like code reviews, as well as automated tools that can identify suspicious patterns of dynamic variable usage.

What is a real-world example of CWE-627 exploitation?

An attacker might inject malicious input to dynamically set sensitive configuration variables or access internal application state.

How do I prevent dynamic variable evaluation in Python code?

Restrict the use of dynamic variable names by using allowlists and validating all variable references against a predefined set of acceptable names.

What is the best practice for refactoring to avoid CWE-627 issues?

Refactor your codebase to eliminate or strictly control any instances where variables are dynamically named based on user input.

How can I test my application for dynamic variable evaluation vulnerabilities?

Conduct thorough manual reviews and use automated scanners that specifically look for patterns indicative of CWE-627 weaknesses.

CWE Name Relationship
CWE-914 Improper Control of Dynamically-Identified Variables ChildOf
CWE-183 Permissive List of Allowed Inputs PeerOf

References

Scan Your Own Site

Manual code review catches what you know to look for. An automated scan catches what you didn’t. Scan your own website using PenScan to find Dynamic Variable Evaluation and other risks before an attacker does.