Use of Wrong Operator in String Comparison

Draft Variant
Structure: Simple
Description

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

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.

Common Consequences 1
Scope: Other

Impact: Other

Detection Methods 1
Automated Static AnalysisHigh
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.)
Potential Mitigations 1
Phase: Implementation
Within Java, use .equals() to compare string values. Within JavaScript, use == to compare string values. Within PHP, use == to compare a numeric value to a string value. (PHP converts the string to a number.)

Effectiveness: High

Demonstrative Examples 3

ID : DX-60

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.

Code Example:

Bad
Java
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:

Code Example:

Good
Java
java
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.

Code Example:

Bad
JavaScript

(i === s1) is FALSE

(s4 === i) is FALSE

(s4 === s1) is FALSE

var i = 65; var s1 = '65'; var s4 = new String('65');

if (i === s1) {

javascript
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:

Code Example:

Good
JavaScript

(i == s1) is FALSE

(s4 == i) is FALSE

(s4 == s1) is FALSE

var i = 65; var s1 = '65'; var s4 = new String('65');

if (i == s1) {

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

Code Example:

Bad
PHP

var $i = 65; var $s1 = "65";

if ($i === $s1) {

php
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:

Code Example:

Good
PHP

var $i = 65; var $s1 = "65";

if ($i == $s1) {

php
References 1
The Art of Software Security Assessment
Mark Dowd, John McDonald, and Justin Schuh
Addison Wesley
2006
ID: REF-62
Modes of Introduction
Implementation
Taxonomy Mapping
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • SEI CERT Perl Coding Standard
  • Software Fault Patterns