CWE-486: Comparison of Classes by Name

Description

The product compares classes by name, which can cause it to use the wrong class when multiple classes can have the same name.

Submission Date :

July 19, 2006, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

MITRE
Extended Description

If the decision to trust the methods and data of an object is based on the name of a class, it is possible for malicious users to send objects of the same name as trusted classes and thereby gain the trust afforded to known classes and types.

Example Vulnerable Codes

Example - 1

In this example, the expression in the if statement compares the class of the inputClass object to a trusted class by comparing the class names.


// // Do something assuming you trust inputClass// 
// // ...// 
if (inputClass.getClass().getName().equals("TrustedClassName")) {}

However, multiple classes can have the same name therefore comparing an object's class by name can allow untrusted classes of the same name as the trusted class to be use to execute unintended or incorrect code. To compare the class of an object to the intended class the getClass() method and the comparison operator "==" should be used to ensure the correct trusted class is used, as shown in the following example.


// // Do something assuming you trust inputClass// 
// // ...// 
if (inputClass.getClass() == TrustedClass.class) {}

Example - 2

In this example, the Java class, TrustedClass, overrides the equals method of the parent class Object to determine equivalence of objects of the class. The overridden equals method first determines if the object, obj, is the same class as the TrustedClass object and then compares the object's fields to determine if the objects are equivalent.



// // first check to see if the object is of the same class// 

// // then compare object fields// 
isEquals = true;...if (...) {}
boolean isEquals = false;if (obj.getClass().getName().equals(this.getClass().getName())) {}return isEquals;
...@Overridepublic boolean equals(Object obj) {}...public class TrustedClass {}

However, the equals method compares the class names of the object, obj, and the TrustedClass object to determine if they are the same class. As with the previous example using the name of the class to compare the class of objects can lead to the execution of unintended or incorrect code if the object passed to the equals method is of another class with the same name. To compare the class of an object to the intended class, the getClass() method and the comparison operator "==" should be used to ensure the correct trusted class is used, as shown in the following example.


// // first check to see if the object is of the same class// 
...
...if (obj.getClass() == this.getClass()) {}...public boolean equals(Object obj) {}

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.