CWE-595 Variante Incompleto

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…

Definición

What is 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.
In many programming languages, the default comparison operator (like `==` in Java or `is` in Python) checks for *reference equality*. This means it only returns true if both variables point to the exact same object in memory. To properly compare the internal state or data of two objects, you must use a method designed for *value equality*, such as `.equals()` in Java, `.equals()` or value comparators in C#, or overriding the `==` operator in languages that support it. This is a common pitfall, especially with strings and collections. For instance, using `==` on two Java `String` objects with the same text will often fail because they are distinct objects in memory. Developers must be intentional about their comparison logic, choosing the correct method based on whether they need to verify object identity or object content, as using the wrong check leads to flawed program logic and unexpected behavior.
Impacto en el mundo real

Real-world CVEs caused by CWE-595

Todavía no hay CVEs públicos enlazados a esta CWE en el catálogo de MITRE.

Cómo lo explotan los atacantes

Ruta del atacante paso a paso

  1. 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. 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. 3

    In the following Java example, two BankAccount objects are compared in the isSameAccount method using the == operator.

  4. 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. 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.

Ejemplo de código vulnerable

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.

Vulnerable Java
String str1 = new String("Hello");
  String str2 = new String("Hello");
  if (str1 == str2) {
  	System.out.println("str1 == str2");
  }
Ejemplo de código seguro

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:

Seguro Java
if (str1.equals(str2)) {
  	System.out.println("str1 equals str2");
  }
What changed: the unsafe sink is replaced (or the input is validated/escaped) so the same payload no longer triggers the weakness.
Lista de prevención

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.
Señales de detección

How to detect CWE-595

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 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.)

Auto-corrección de Plexicus

Plexicus detecta automáticamente CWE-595 y abre un PR de corrección en menos de 60 segundos.

Codex Remedium escanea cada commit, identifica esta debilidad concreta y entrega un pull request listo para revisión con el parche. Sin tickets. Sin traspasos.

Preguntas frecuentes

Frequently asked questions

¿Qué es 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.

¿Qué gravedad tiene CWE-595?

MITRE no ha publicado una calificación de probabilidad de explotación para esta debilidad. Trátala como de impacto medio hasta que tu modelo de amenazas demuestre lo contrario.

¿Qué lenguajes o plataformas se ven afectados por CWE-595?

MITRE lists the following affected platforms: Java, JavaScript, PHP.

¿Cómo puedo prevenir 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.

¿Cómo detecta y corrige Plexicus CWE-595?

El motor SAST de Plexicus detecta la firma de flujo de datos para CWE-595 en cada commit. Cuando hay coincidencia, nuestro agente Codex Remedium abre un PR de corrección con el código corregido, las pruebas y un resumen de una línea para el revisor.

¿Dónde puedo aprender más sobre CWE-595?

MITRE publica la definición canónica en https://cwe.mitre.org/data/definitions/595.html. También puedes consultar la documentación de OWASP y NIST para guías relacionadas.

Listo cuando tú lo estés

Deja de pagar por desarrollador.
Empieza a cerrar el bucle.

Plexicus es el ASPM nativo de IA que escanea, filtra, corrige, pentestea y explica — de forma autónoma. Desarrolladores ilimitados, repos ilimitados, acciones de IA de uso justo. Nivel gratuito real, €269/mo anual cuando estés listo.