-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjava-net-URL-equality-check.ql
More file actions
176 lines (153 loc) · 5.88 KB
/
Copy pathjava-net-URL-equality-check.ql
File metadata and controls
176 lines (153 loc) · 5.88 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Finds cases where an equality check in the form of an
* (indirect) `equals` or `hashCode` call is performed on a
* `java.net.URL`. The documentation of `URL.equals` says:
* > Two hosts are considered equivalent if both host names can be resolved into the same IP addresses
*
* Most often this is undesired and has the following disadvantages:
* - `equals` and `hashCode` calls could block while the
* host name is being resolved.
* - Different hosts (e.g. `example.net` and `example.com`)
* are considered equal if they have the same IP. This makes
* code dependent on internal network layouts and also makes
* its behavior inconsistent, for example when the internet
* connection is lost preventing resolution of host names.
* - It leaks the local machine IP address when trying to
* resolve host names.
*
* Therefore `java.net.URI` or the external String form of the
* URL should be used instead for equality checks.
*
* See also https://rules.sonarsource.com/java/RSPEC-2112
*/
import java
import semmle.code.java.dataflow.DataFlow
class TypeUrl extends Class {
TypeUrl() {
hasQualifiedName("java.net", "URL")
}
}
abstract class EqualityCheckingExpr extends Expr {
abstract Expr getAEqualityCheckedArg();
}
class HashCodeCall extends MethodAccess, EqualityCheckingExpr {
HashCodeCall() {
getMethod() instanceof HashCodeMethod
}
override Expr getAEqualityCheckedArg() {
result = getQualifier()
}
}
class EqualsCall extends MethodAccess, EqualityCheckingExpr {
EqualsCall() {
getMethod() instanceof EqualsMethod
}
override Expr getAEqualityCheckedArg() {
result = getQualifier()
or result = getArgument(0)
}
}
class TypeObjects extends Class {
TypeObjects() {
hasQualifiedName("java.util", "Objects")
}
}
class ObjectsHashCodeCall extends MethodAccess, EqualityCheckingExpr {
ObjectsHashCodeCall() {
exists (Method m | m = getMethod() |
m.getDeclaringType() instanceof TypeObjects
and m.hasName(["hash", "hashCode"])
)
}
override Expr getAEqualityCheckedArg() {
result = getAnArgument()
}
}
class ObjectsEqualsCall extends MethodAccess, EqualityCheckingExpr {
ObjectsEqualsCall() {
exists (Method m | m = getMethod() |
m.getDeclaringType() instanceof TypeObjects
and m.hasName(["equals", "deepEquals"])
)
}
override Expr getAEqualityCheckedArg() {
result = getAnArgument()
}
}
class CollectionMethodCall extends MethodAccess, EqualityCheckingExpr {
CollectionMethodCall() {
exists (Method m | getMethod().getSourceDeclaration().overridesOrInstantiates*(m) |
m.getDeclaringType().hasQualifiedName("java.util", "Collection")
and m.hasStringSignature(["contains(Object)", "remove(Object)"])
)
}
override Expr getAEqualityCheckedArg() {
result = getArgument(0)
}
}
class CollectionsMethodCall extends MethodAccess, EqualityCheckingExpr {
CollectionsMethodCall() {
exists (Method m, int paramsCount | m = getMethod() and paramsCount = m.getNumberOfParameters() |
m.getDeclaringType().hasQualifiedName("java.util", "Collections")
and (
m.hasName(["binarySearch", "frequency"]) and paramsCount = 2
or m.hasName("replaceAll") and paramsCount = 3
)
)
}
override Expr getAEqualityCheckedArg() {
result = getArgument(1)
}
}
class SetAddMethodCall extends MethodAccess, EqualityCheckingExpr {
SetAddMethodCall() {
exists (Method m | getMethod().getSourceDeclaration().overridesOrInstantiates*(m) |
m.getDeclaringType().hasQualifiedName("java.util", "Set")
and m.hasStringSignature(["add(E)", "remove(Object)"])
)
}
override Expr getAEqualityCheckedArg() {
result = getArgument(0)
}
}
class ListIndexMethodCall extends MethodAccess, EqualityCheckingExpr {
ListIndexMethodCall() {
exists (Method m | getMethod().getSourceDeclaration().overridesOrInstantiates*(m) |
m.getDeclaringType().hasQualifiedName("java.util", "List")
and m.hasName(["indexOf", "lastIndexOf"])
)
}
override Expr getAEqualityCheckedArg() {
result = getArgument(0)
}
}
class MapMethodCall extends MethodAccess, EqualityCheckingExpr {
MapMethodCall() {
exists (Method m | getMethod().getSourceDeclaration().overridesOrInstantiates*(m) |
m.getDeclaringType().hasQualifiedName("java.util", "Map")
and m.hasName(["compute", "computeIfAbsent", "computeIfPresent", "containsKey", "containsValue", "get", "getOrDefault", "merge", "put", "putIfAbsent", "remove", "replace"])
)
}
override Expr getAEqualityCheckedArg() {
result = getArgument(0)
}
}
from Expr urlEqualityCheck
where
urlEqualityCheck.(EqualityCheckingExpr).getAEqualityCheckedArg().getType() instanceof TypeUrl
// Or method which checks equality is called with URL argument
or exists (Callable callee, Callable called, int paramIndex, Parameter param |
urlEqualityCheck.(Call).getCallee().getSourceDeclaration() = callee
and if callee instanceof Method then callee.(Method).overridesOrInstantiates(called)
else called = callee
and urlEqualityCheck.(Call).getArgument(paramIndex).getType() instanceof TypeUrl
and param = called.getParameter(paramIndex)
|
// Ignore if parameter type is URL because then `called` will already be flagged
not param.getType() instanceof TypeUrl
and DataFlow::localFlow(
DataFlow::parameterNode(called.getParameter(paramIndex)),
DataFlow::exprNode(any (EqualityCheckingExpr e).getAEqualityCheckedArg())
)
)
select urlEqualityCheck, "Checks equality of java.net.URL"