Description
The product compares object references instead of the contents of the objects themselves, preventing it from detecting equivalent objects.
For example, in Java, comparing objects using == usually produces deceptive results, since the == operator compares object references rather than values; often, this means that using == for strings is actually comparing the strings' references, not their values.
Potential Impact
Other
Varies by Context
Demonstrative Examples
String str1 = new String("Hello");String str2 = new String("Hello");if (str1 == str2) {System.out.println("str1 == str2");}if (str1.equals(str2)) {System.out.println("str1 equals str2");}public boolean isSameAccount(BankAccount accountA, BankAccount accountB) {return accountA == accountB;}public boolean isSameAccount(BankAccount accountA, BankAccount accountB) {return accountA.equals(accountB);}Mitigations & Prevention
In Java, use the equals() method to compare objects instead of the == operator. If using ==, it is important for performance reasons that your objects are created by a static factory, not by a constructor.
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
- The CERT Oracle Secure Coding Standard for Java (2011): EXP02-J — Use the two-argument Arrays.equals() method to compare the contents of arrays
- The CERT Oracle Secure Coding Standard for Java (2011): EXP02-J — Use the two-argument Arrays.equals() method to compare the contents of arrays
- The CERT Oracle Secure Coding Standard for Java (2011): EXP03-J — Do not use the equality operators when comparing values of boxed primitives
Frequently Asked Questions
What is CWE-595?
CWE-595 (Comparison of Object References Instead of Object Contents) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. The product compares object references instead of the contents of the objects themselves, preventing it from detecting equivalent objects.
How can CWE-595 be exploited?
Attackers can exploit CWE-595 (Comparison of Object References Instead of Object Contents) to varies by context. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-595?
Key mitigations include: In Java, use the equals() method to compare objects instead of the == operator. If using ==, it is important for performance reasons that your objects are created by a static factory, not by a constru
What is the severity of CWE-595?
CWE-595 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.