Description
The product compares classes by name, which can cause it to use the wrong class when multiple classes can have the same name.
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.
Potential Impact
Integrity, Confidentiality, Availability
Execute Unauthorized Code or Commands
Demonstrative Examples
if (inputClass.getClass().getName().equals("TrustedClassName")) {
// Do something assuming you trust inputClass
// ...
}if (inputClass.getClass() == TrustedClass.class) {
// Do something assuming you trust inputClass
// ...
}public class TrustedClass {
...
@Overridepublic boolean equals(Object obj) {
boolean isEquals = false;
// first check to see if the object is of the same class
if (obj.getClass().getName().equals(this.getClass().getName())) {
// then compare object fields
...if (...) {isEquals = true;}
}
return isEquals;
}
...
}public boolean equals(Object obj) {
...
// first check to see if the object is of the same class
if (obj.getClass() == this.getClass()) {...}
...
}Mitigations & Prevention
Use class equivalency to determine type. Rather than use the class name to determine if an object is of a given type, use the getClass() method, and == operator.
Detection Methods
- Automated Static Analysis High — 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 sea
Related Weaknesses
Taxonomy Mappings
- 7 Pernicious Kingdoms: — Comparing Classes by Name
- CLASP: — Comparing classes by name
- The CERT Oracle Secure Coding Standard for Java (2011): OBJ09-J — Compare classes and not class names
- Software Fault Patterns: SFP1 — Glitch in computation
Frequently Asked Questions
What is CWE-486?
CWE-486 (Comparison of Classes by Name) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. The product compares classes by name, which can cause it to use the wrong class when multiple classes can have the same name.
How can CWE-486 be exploited?
Attackers can exploit CWE-486 (Comparison of Classes by Name) to execute unauthorized code or commands. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-486?
Key mitigations include: Use class equivalency to determine type. Rather than use the class name to determine if an object is of a given type, use the getClass() method, and == operator.
What is the severity of CWE-486?
CWE-486 is classified as a Variant-level weakness (Low-Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.