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.)
Comparison of Object References Instead of Object Contents
This vulnerability occurs when code incorrectly checks if two object references point to the same memory location, rather than comparing the actual data or values contained within the objects. This…
What is CWE-595?
Real-world CVEs caused by CWE-595
Aucune référence CVE publique n'est liée à ce CWE dans le catalogue MITRE pour le moment.
Parcours de l'attaquant étape par étape
- 1
In the example below, two Java String objects are declared and initialized with the same string values. An if statement is used to determine if the strings are equivalent.
- 2
However, the if statement will not be executed as the strings are compared using the "==" operator. For Java objects, such as String objects, the "==" operator compares object references, not object values. While the two String objects above contain the same string values, they refer to different object references, so the System.out.println statement will not be executed. To compare object values, the previous code could be modified to use the equals method:
- 3
In the following Java example, two BankAccount objects are compared in the isSameAccount method using the == operator.
- 4
Using the == operator to compare objects may produce incorrect or deceptive results by comparing object references rather than values. The equals() method should be used to ensure correct results or objects should contain a member variable that uniquely identifies the object.
- 5
The following example shows the use of the equals() method to compare the BankAccount objects and the next example uses a class get method to retrieve the bank account number that uniquely identifies the BankAccount object to compare the objects.
Vulnerable Java
In the example below, two Java String objects are declared and initialized with the same string values. An if statement is used to determine if the strings are equivalent.
String str1 = new String("Hello");
String str2 = new String("Hello");
if (str1 == str2) {
System.out.println("str1 == str2");
} Secure Java
However, the if statement will not be executed as the strings are compared using the "==" operator. For Java objects, such as String objects, the "==" operator compares object references, not object values. While the two String objects above contain the same string values, they refer to different object references, so the System.out.println statement will not be executed. To compare object values, the previous code could be modified to use the equals method:
if (str1.equals(str2)) {
System.out.println("str1 equals str2");
} How to prevent CWE-595
- Implementation 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.
How to detect CWE-595
Plexicus détecte automatiquement CWE-595 et ouvre une PR de correction en moins de 60 secondes.
Codex Remedium analyse chaque commit, identifie cette faiblesse précise et livre une pull request prête à être relue avec le correctif. Pas de tickets. Pas de transferts.
Frequently asked questions
Qu'est-ce que CWE-595 ?
This vulnerability occurs when code incorrectly checks if two object references point to the same memory location, rather than comparing the actual data or values contained within the objects. This mistake prevents the software from correctly identifying when two separate objects hold equivalent content.
Quelle est la gravité de CWE-595 ?
MITRE n'a pas publié de note de probabilité d'exploitation pour cette faiblesse. Traitez-la comme un impact moyen jusqu'à ce que votre modèle de menace prouve le contraire.
Quels langages ou plateformes sont affectés par CWE-595 ?
MITRE lists the following affected platforms: Java, JavaScript, PHP.
Comment puis-je prévenir CWE-595 ?
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.
Comment Plexicus détecte et corrige CWE-595 ?
Le moteur SAST de Plexicus reconnaît la signature de flux de données de CWE-595 à chaque commit. Lorsqu'une correspondance est trouvée, notre agent Codex Remedium ouvre une PR de correction avec le code corrigé, les tests et un résumé d'une ligne pour le relecteur.
Où puis-je en savoir plus sur CWE-595 ?
MITRE publie la définition canonique à https://cwe.mitre.org/data/definitions/595.html. Vous pouvez également consulter la documentation OWASP et NIST pour des conseils adjacents.
Weaknesses related to CWE-595
Comparison Using Wrong Factors
This weakness occurs when a program compares two items but checks the wrong properties or attributes. This flawed comparison leads to…
Comparison of Classes by Name
This vulnerability occurs when an application determines an object's trustworthiness or behavior solely by checking its class name. Since…
Persistent Storable Data Element without Associated Comparison Control Element
This weakness occurs when a persistent data object lacks the necessary methods to be properly compared, which can lead to inconsistent or…
Use of Wrong Operator in String Comparison
This vulnerability occurs when a developer incorrectly compares string values, typically by using reference equality operators (like == or…
Arrêtez de payer par développeur.
Commencez à fermer la boucle.
Plexicus est l'ASPM natif IA qui scanne, filtre, corrige, penteste et explique — de façon autonome. Développeurs illimités, dépôts illimités, actions IA à usage équitable. Vrai niveau gratuit, €269/mo annuel quand vous êtes prêt.