CWE
Home > CWE List > CWE- Individual Dictionary Definition (1.0.1)  
Search by ID:

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

Individual Definition in a New Window
Use of Externally-Controlled Input to Select Classes or Code (aka 'Unsafe Reflection')
Status: Draft
Weakness ID: 470 (Weakness Base)
Description
Summary

The application uses external input with reflection to select which classes or code to use, but it does not sufficiently prevent the input from selecting improper classes or code.

Extended Description

If the application uses external inputs to determine which class to instantiate or which method to invoke, then an attacker could supply values to select unexpected classes or methods. If this occurs, then the attacker could create control flow paths that were not intended by the developer. These paths could bypass authentication or access control checks, or otherwise cause the application to behave in an unexpected manner. This situation becomes a doomsday scenario if the attacker can upload files into a location that appears on the application's classpath (CWE-427) or add new entries to the application's classpath (CWE-426). Under either of these conditions, the attacker can use reflection to introduce new, malicious behavior into the application.

Potential Mitigations

Do not use user-controlled variable to load Classes or code.

Apply input validation.

Demonstrative Examples

Example: 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:

String ctl = request.getParameter("ctl");
Worker ao = null;
if (ctl.equals("Add")) {
ao = new AddCommand();
}
else if (ctl.equals("Modify")) {
ao = new ModifyCommand();
}
else {
throw new UnknownActionError();
}
ao.doAction(request);

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

String ctl = request.getParameter("ctl");
Class cmdClass = Class.forName(ctl + "Command");
Worker ao = (Worker) cmdClass.newInstance();
ao.doAction(request);

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 fail to 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:

Java Example:
String ctl = request.getParameter("ctl");
Class cmdClass = Class.forName(ctl + "Command");
Worker ao = (Worker) cmdClass.newInstance();
ao.checkAccessControl(request);
ao.doAction(request);

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 applications, in larger applications where complexity grows exponentially it is not unreasonable that an attacker could find a constructor to leverage as part of an attack.

Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfWeakness ClassWeakness ClassWeakness Class398Indicator of Poor Code Quality
Research Concepts1000
ChildOfWeakness ClassWeakness ClassWeakness Class610Externally Controlled Reference to a Resource in Another Sphere
Research Concepts (primary)1000
ChildOfWeakness ClassWeakness ClassWeakness Class20Insufficient Input Validation
Development Concepts (primary)699
Seven Pernicious Kingdoms (primary)700
Taxonomy Mappings
Mapped Taxonomy NameMapped Node Name
7 Pernicious KingdomsUnsafe Reflection
Applicable Platforms
Languages
Java
PHP
Interpreted languages
Time of Introduction
* Architecture and Design
* Implementation
White Box Definitions

A weakness where code path has:

1. start statement that accepts input

2. end statement that performs reflective operation and where the input is part of the target name of the reflective operation

Content History
Submissions
7 Pernicious Kingdoms. (Externally Mined)
Modifications
Eric Dalci. Cigital. 2008-07-01. (External)
updated Potential_Mitigations, Time_of_Introduction
KDM Analytics. 2008-08-01. (External)
added/updated white box definitions
CWE Content Team. MITRE. 2008-09-08. (Internal)
updated Description, Relationships, Other_Notes, Taxonomy_Mappings
CWE Content Team. MITRE. 2008-10-14. (Internal)
updated Applicable_Platforms, Demonstrative_Examples, Description, Other_Notes
Previous Entry Names
* Unsafe Reflection (changed 2008-04-11)
Page Last Updated: October 16, 2008