CWE-597: Use of Wrong Operator in String Comparison
The product uses the wrong operator when comparing a string, such as using "==" when the .equals() method should be used instead.
Last updated
Overview
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.
Real-world CVEs
3 recorded CVEs are caused by CWE-597 (Use of Wrong Operator in String Comparison). The highest-severity and most recent are shown first.
Common consequences
What can happen when CWE-597 is exploited.
Other
Affects: Other
How it happens
When it is introduced
Typically introduced during these phases of the software lifecycle.
Applies to
Languages
How to detect it
Automated Static Analysis
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.)
Effectiveness: High
Code examples
Illustrative examples from MITRE showing how the weakness appears in code.
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 example
String str1 = new String("Hello");Safe example
if (str1.equals(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:
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.
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:
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.
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:
Terminology & mappings
Mapped taxonomies
- The CERT Oracle Secure Coding Standard for Java (2011): Do not use the equality operators when comparing values of boxed primitives (EXP03-J)
- The CERT Oracle Secure Coding Standard for Java (2011): Do not use the equality operators when comparing values of boxed primitives (EXP03-J)
- SEI CERT Perl Coding Standard: Use the correct operator type for comparing values (EXP35-PL) — CWE More Specific fit
- Software Fault Patterns: Glitch in computation (SFP1)
Frequently asked questions
Common questions about CWE-597.
- What is CWE-597?
- The product uses the wrong operator when comparing a string, such as using "==" when the .equals() method should be used instead.
- What CVEs are caused by CWE-597?
- 3 recorded CVEs are attributed to CWE-597, including CVE-2021-4259, CVE-2021-3797, CVE-2022-36072.
- How is CWE-597 detected?
- Automated Static Analysis: 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.)
- What are the consequences of CWE-597?
- Exploiting CWE-597 can lead to: Other.
- Is CWE-597 actively exploited?
- 3 recorded CVEs are caused by CWE-597; none are currently in CISA's KEV catalog of actively exploited flaws.
References
- MITRE CWE definition (CWE-597) (opens in a new tab)
- CWE-597 vulnerabilities on NVD (opens in a new tab)
- Learn: What is a CWE?
Weakness data is sourced from the MITRE CWE catalog (v4.20). CVE associations are aggregated and kept current by RadicalNotion.AI.
Stay ahead of CWE-597
Get alerted the moment a new CWE-597 vulnerability affects your stack, with AI-written analysis, severity context, and remediation guidance.