forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringComparison.ql
More file actions
66 lines (60 loc) · 2.17 KB
/
StringComparison.ql
File metadata and controls
66 lines (60 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* @name Reference equality test on strings
* @description Comparing two strings using the == or != operator
* compares object identity, which may not be intended.
* @kind problem
* @problem.severity warning
* @precision medium
* @id java/reference-equality-on-strings
* @suites security-and-quality
* @tags reliability
* external/cwe/cwe-597
*/
import java
/** An expression of type `java.lang.String`. */
class StringValue extends Expr {
StringValue() { this.getType() instanceof TypeString }
predicate isInterned() {
// A call to `String.intern()`.
exists(Method intern |
intern.getDeclaringType() instanceof TypeString and
intern.hasName("intern") and
this.(MethodCall).getMethod() = intern
)
or
// Ternary conditional operator.
this.(ConditionalExpr).getTrueExpr().(StringValue).isInterned() and
this.(ConditionalExpr).getFalseExpr().(StringValue).isInterned()
or
// Values of type `String` that are compile-time constant expressions (JLS 15.28).
this instanceof CompileTimeConstantExpr
or
// Variables that are only ever assigned an interned `StringValue`.
variableValuesInterned(this.(VarAccess).getVariable())
or
// Method accesses whose results are all interned.
forex(ReturnStmt rs | rs.getEnclosingCallable() = this.(MethodCall).getMethod() |
rs.getResult().(StringValue).isInterned()
)
}
}
pragma[noinline]
predicate candidateVariable(Variable v) {
v.fromSource() and
// For parameters, assume they could be non-interned.
not v instanceof Parameter and
// If the string is modified with `+=`, then the new string is not interned
// even if the components are.
not exists(AssignOp append | append.getDest() = v.getAnAccess())
}
predicate variableValuesInterned(Variable v) {
candidateVariable(v) and
// All assignments to variables are interned.
forall(StringValue sv | sv = v.getAnAssignedValue() | sv.isInterned())
}
from ReferenceEqualityTest e, StringValue lhs, StringValue rhs
where
e.getLeftOperand() = lhs and
e.getRightOperand() = rhs and
not (lhs.isInterned() and rhs.isInterned())
select e, "String values compared with " + e.getOp() + "."