Use of Externally-Controlled Input to Select Classes or Code ('Unsafe Reflection')

Draft Base
Structure: Simple
Description

This vulnerability occurs when an application uses unvalidated external input, like a URL parameter or form field, to dynamically decide which class to load or which method to execute via reflection. An attacker can manipulate this input to force the application to load unexpected, potentially malicious code.

Extended Description

When an application lets user input dictate its control flow through reflection, it hands partial control of its logic to an attacker. By supplying crafted values, an attacker can bypass security checks, access unauthorized features, or trigger unexpected behaviors. The risk becomes critical if an attacker can also place malicious files on the application's classpath, allowing them to directly inject and execute their own code through this same reflection mechanism. Preventing unsafe reflection requires strict allow-listing of permitted classes or methods and rigorous validation of all dynamic input. While SAST tools can identify the vulnerable pattern, Plexicus uses AI to analyze the context and suggest precise code fixes—such as implementing a secure allow-list—transforming a complex security finding into an actionable remediation that saves development time.

Common Consequences 3
Scope: IntegrityConfidentialityAvailabilityOther

Impact: Execute Unauthorized Code or CommandsAlter Execution Logic

The attacker might be able to execute code that is not directly accessible to the attacker. Alternately, the attacker could call unexpected code in the wrong place or the wrong time, possibly modifying critical system state.

Scope: AvailabilityOther

Impact: DoS: Crash, Exit, or RestartOther

The attacker might be able to use reflection to call the wrong code, possibly with unexpected arguments that violate the API (7PK - API Abuse). This could cause the product to exit or hang.

Scope: Confidentiality

Impact: Read Application Data

By causing the wrong code to be invoked, the attacker might be able to trigger a runtime error that leaks sensitive information in the error message, such as Servlet Runtime Error Message Containing Sensitive Information.

Detection Methods 1
Automated Static AnalysisHigh
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.)
Potential Mitigations 3
Phase: Architecture and Design
Refactor your code to avoid using reflection.
Phase: Architecture and Design
Do not use user-controlled inputs to select and load classes or code.
Phase: Implementation
Apply strict input validation by using allowlists or indirect selection to ensure that the user is only selecting allowable classes or code.
Demonstrative Examples 1
A common reason that programmers use the reflection API is to implement their own command dispatcher. The following example shows a command dispatcher that does not use reflection:

Code Example:

Good
Java
java
A programmer might refactor this code to use reflection as follows:

Code Example:

Bad
Java
java
The refactoring initially appears to offer a number of advantages. There are fewer lines of code, the if/else blocks have been entirely eliminated, and it is now possible to add new command types without modifying the command dispatcher. However, the refactoring allows an attacker to instantiate any object that implements the Worker interface. If the command dispatcher is still responsible for access control, then whenever programmers create a new class that implements the Worker interface, they must remember to modify the dispatcher's access control code. If they do not modify the access control code, then some Worker classes will not have any access control.
One way to address this access control problem is to make the Worker object responsible for performing the access control check. An example of the re-refactored code follows:

Code Example:

Bad
Java
java
Although this is an improvement, it encourages a decentralized approach to access control, which makes it easier for programmers to make access control mistakes. This code also highlights another security problem with using reflection to build a command dispatcher. An attacker can invoke the default constructor for any kind of object. In fact, the attacker is not even constrained to objects that implement the Worker interface; the default constructor for any object in the system can be invoked. If the object does not implement the Worker interface, a ClassCastException will be thrown before the assignment to ao, but if the constructor performs operations that work in the attacker's favor, the damage will already have been done. Although this scenario is relatively benign in simple products, in larger products where complexity grows exponentially it is not unreasonable that an attacker could find a constructor to leverage as part of an attack.
Observed Examples 2
CVE-2018-1000613Cryptography API uses unsafe reflection when deserializing a private key
CVE-2004-2331Database system allows attackers to bypass sandbox restrictions by using the Reflection API.
References 1
Seven Pernicious Kingdoms: A Taxonomy of Software Security Errors
Katrina Tsipenyuk, Brian Chess, and Gary McGraw
NIST Workshop on Software Security Assurance Tools Techniques and MetricsNIST
07-11-2005
ID: REF-6
Applicable Platforms
Languages:
Java : UndeterminedPHP : UndeterminedInterpreted : Sometimes
Modes of Introduction
Architecture and Design
Implementation
Alternate Terms

Reflection Injection

Taxonomy Mapping
  • 7 Pernicious Kingdoms
  • The CERT Oracle Secure Coding Standard for Java (2011)