CWE-597: Use of Wrong Operator in String Comparison

Description

The product uses the wrong operator when comparing a string, such as using "==" when the .equals() method should be used instead.

Submission Date :

Dec. 15, 2006, midnight

Modification Date :

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

Organization :

MITRE
Extended Description

In Java, using == or != to compare two strings for equality actually compares two objects for equality rather than their string values for equality. Chances are good that the two references will never be equal. While this weakness often only affects program correctness, if the equality is used for a security decision, the unintended comparison result could be leveraged to affect program security.

Example Vulnerable Codes

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


System.out.println("str1 == str2");String str1 = new String("Hello");String str2 = new String("Hello");if (str1 == str2) {}

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:

System.out.println("str1 equals str2");if (str1.equals(str2)) {}

Example - 2

In the example below, three JavaScript variables are declared and initialized with the same values. Note that JavaScript will change a value between numeric and string as needed, which is the reason an integer is included with the strings. An if statement is used to determine whether the values are the same.


document.getElementById("ieq3s1").innerHTML = "(i === s1) is TRUE";
document.getElementById("s4eq3i").innerHTML = "(s4 === i) is TRUE";
document.getElementById("s4eq3s1").innerHTML = "(s4 === s1) is TRUE";
<p id="ieq3s1" type="text">(i === s1) is FALSE</p><p id="s4eq3i" type="text">(s4 === i) is FALSE</p><p id="s4eq3s1" type="text">(s4 === s1) is FALSE</p>var i = 65;var s1 = '65';var s4 = new String('65');if (i === s1){}if (s4 === i){}if (s4 === s1){}

However, the body of the if statement will not be executed, as the "===" compares both the type of the variable AND the value. As the types of the first comparison are number and string, it fails. The types in the second are int and reference, so this one fails as well. The types in the third are reference and string, so it also fails.While the variables above contain the same values, they are contained in different types, so the document.getElementById... statement will not be executed in any of the cases.To compare object values, the previous code is modified and shown below to use the "==" for value comparison so the comparison in this example executes the HTML statement:


document.getElementById("ieq2s1").innerHTML = "(i == s1) is TRUE";
document.getElementById("s4eq2i").innerHTML = "(s4 == i) is TRUE";
document.getElementById("s4eq2s1").innerHTML = "(s4 == s1) is TRUE";
<p id="ieq2s1" type="text">(i == s1) is FALSE</p><p id="s4eq2i" type="text">(s4 == i) is FALSE</p><p id="s4eq2s1" type="text">(s4 == s1) is FALSE</p>var i = 65;var s1 = '65';var s4 = new String('65');if (i == s1){}if (s4 == i){}if (s4 == s1){}

Example - 3

In the example below, two PHP variables are declared and initialized with the same numbers - one as a string, the other as an integer. Note that PHP will change the string value to a number for a comparison. An if statement is used to determine whether the values are the same.


echo '($i === $s1) is TRUE'. "\n";
echo '($i === $s1) is FALSE'. "\n";
var $i = 65;var $s1 = "65";if ($i === $s1){}else{}

However, the body of the if statement will not be executed, as the "===" compares both the type of the variable AND the value. As the types of the first comparison are number and string, it fails.While the variables above contain the same values, they are contained in different types, so the TRUE portion of the if statement will not be executed.To compare object values, the previous code is modified and shown below to use the "==" for value comparison (string converted to number) so the comparison in this example executes the TRUE statement:


echo '($i == $s1) is TRUE'. "\n";
echo '($i == $s1) is FALSE'. "\n";
var $i = 65;var $s1 = "65";if ($i == $s1){}else{}

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.

© cvefeed.io
Latest DB Update: Nov. 17, 2024 22:45