Skip to content

Commit 2b16a8e

Browse files
Minor improvements for Jackson in UnsafeDeserialization.qll
1 parent 905bfa8 commit 2b16a8e

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
lgtm,codescanning
22
* The "Deserialization of user-controlled data" (`java/unsafe-deserialization`) query
33
now recognizes `Jackson` deserialization.
4-

java/ql/src/semmle/code/java/security/UnsafeDeserialization.qll

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,57 @@ class XMLDecoderReadObjectMethod extends Method {
2020
}
2121
}
2222

23-
class ObjectMapperReadMethod extends Method {
23+
private class ObjectMapperReadMethod extends Method {
2424
ObjectMapperReadMethod() {
2525
this.getDeclaringType() instanceof ObjectMapper and
2626
this.hasName(["readValue", "readValues", "treeToValue"])
2727
}
2828
}
2929

30-
class ObjectMapper extends RefType {
30+
private class ObjectMapper extends RefType {
3131
ObjectMapper() {
3232
getASupertype*().hasQualifiedName("com.fasterxml.jackson.databind", "ObjectMapper")
3333
}
3434
}
3535

36-
class MapperBuilder extends RefType {
36+
private class MapperBuilder extends RefType {
3737
MapperBuilder() {
3838
hasQualifiedName("com.fasterxml.jackson.databind.cfg", "MapperBuilder<JsonMapper,Builder>")
3939
}
4040
}
4141

42-
class JsonFactory extends RefType {
42+
private class JsonFactory extends RefType {
4343
JsonFactory() { hasQualifiedName("com.fasterxml.jackson.core", "JsonFactory") }
4444
}
4545

46-
class JsonParser extends RefType {
46+
private class JsonParser extends RefType {
4747
JsonParser() { hasQualifiedName("com.fasterxml.jackson.core", "JsonParser") }
4848
}
4949

50-
class JacksonType extends RefType {
51-
JacksonType() {
50+
private class JacksonTypeDescriptorType extends RefType {
51+
JacksonTypeDescriptorType() {
5252
this instanceof TypeClass or
5353
hasQualifiedName("com.fasterxml.jackson.databind", "JavaType") or
5454
hasQualifiedName("com.fasterxml.jackson.core.type", "TypeReference")
5555
}
5656
}
5757

58-
class EnableJacksonDefaultTyping extends MethodAccess {
58+
private class EnableJacksonDefaultTyping extends MethodAccess {
5959
EnableJacksonDefaultTyping() {
6060
this.getMethod().getDeclaringType() instanceof ObjectMapper and
6161
this.getMethod().hasName("enableDefaultTyping")
6262
}
6363
}
6464

65-
class ObjectMapperReadSink extends DataFlow::ExprNode {
65+
private class ObjectMapperReadSink extends DataFlow::ExprNode {
6666
ObjectMapperReadSink() {
6767
exists(MethodAccess ma | ma.getQualifier() = this.asExpr() |
6868
ma.getMethod() instanceof ObjectMapperReadMethod
6969
)
7070
}
7171
}
7272

73-
class SetPolymorphicTypeValidatorSource extends DataFlow::ExprNode {
73+
private class SetPolymorphicTypeValidatorSource extends DataFlow::ExprNode {
7474
SetPolymorphicTypeValidatorSource() {
7575
exists(MethodAccess ma, Method m, Expr q | m = ma.getMethod() and q = ma.getQualifier() |
7676
(
@@ -176,8 +176,8 @@ class SafeKryo extends DataFlow2::Configuration {
176176
}
177177
}
178178

179-
class EnabledJacksonDefaultTyping extends DataFlow2::Configuration {
180-
EnabledJacksonDefaultTyping() { this = "EnabledJacksonDefaultTyping" }
179+
private class EnableJacksonDefaultTypingConfig extends DataFlow2::Configuration {
180+
EnableJacksonDefaultTypingConfig() { this = "EnableJacksonDefaultTypingConfig" }
181181

182182
override predicate isSource(DataFlow::Node src) {
183183
any(EnableJacksonDefaultTyping ma).getQualifier() = src.asExpr()
@@ -186,8 +186,8 @@ class EnabledJacksonDefaultTyping extends DataFlow2::Configuration {
186186
override predicate isSink(DataFlow::Node sink) { sink instanceof ObjectMapperReadSink }
187187
}
188188

189-
class SafeObjectMapper extends DataFlow2::Configuration {
190-
SafeObjectMapper() { this = "SafeObjectMapper" }
189+
private class SafeObjectMapperConfig extends DataFlow2::Configuration {
190+
SafeObjectMapperConfig() { this = "SafeObjectMapperConfig" }
191191

192192
override predicate isSource(DataFlow::Node src) {
193193
src instanceof SetPolymorphicTypeValidatorSource
@@ -200,27 +200,27 @@ class SafeObjectMapper extends DataFlow2::Configuration {
200200
* that configures or creates an `ObjectMapper` via a builder.
201201
*/
202202
override predicate isAdditionalFlowStep(DataFlow::Node fromNode, DataFlow::Node toNode) {
203-
exists(MethodAccess ma, Method m, Expr q | m = ma.getMethod() and q = ma.getQualifier() |
203+
exists(MethodAccess ma, Method m | m = ma.getMethod() |
204204
m.getDeclaringType() instanceof MapperBuilder and
205205
m.getReturnType()
206206
.(RefType)
207207
.hasQualifiedName("com.fasterxml.jackson.databind.json",
208208
["JsonMapper$Builder", "JsonMapper"]) and
209-
fromNode.asExpr() = q and
209+
fromNode.asExpr() = ma.getQualifier() and
210210
ma = toNode.asExpr()
211211
)
212212
}
213213
}
214214

215-
class UnsafeType extends TaintTracking2::Configuration {
216-
UnsafeType() { this = "UnsafeType" }
215+
private class UnsafeTypeConfig extends TaintTracking2::Configuration {
216+
UnsafeTypeConfig() { this = "UnsafeTypeConfig" }
217217

218218
override predicate isSource(DataFlow::Node src) { src instanceof RemoteFlowSource }
219219

220220
override predicate isSink(DataFlow::Node sink) {
221221
exists(MethodAccess ma, int i, Expr arg | i > 0 and ma.getArgument(i) = arg |
222222
ma.getMethod() instanceof ObjectMapperReadMethod and
223-
arg.getType() instanceof JacksonType and
223+
arg.getType() instanceof JacksonTypeDescriptorType and
224224
arg = sink.asExpr()
225225
)
226226
}
@@ -230,7 +230,7 @@ class UnsafeType extends TaintTracking2::Configuration {
230230
*/
231231
override predicate isAdditionalTaintStep(DataFlow::Node fromNode, DataFlow::Node toNode) {
232232
exists(MethodAccess ma, RefType returnType | returnType = ma.getMethod().getReturnType() |
233-
returnType instanceof JacksonType and
233+
returnType instanceof JacksonTypeDescriptorType and
234234
ma.getAnArgument() = fromNode.asExpr() and
235235
ma = toNode.asExpr()
236236
)
@@ -272,7 +272,7 @@ predicate createJacksonTreeNodeStep(DataFlow::Node fromNode, DataFlow::Node toNo
272272
* Holds if `type` or one of its supertypes has a field with `JsonTypeInfo` annotation
273273
* that enables polymorphic type handling.
274274
*/
275-
predicate hasJsonTypeInfoAnnotation(RefType type) {
275+
private predicate hasJsonTypeInfoAnnotation(RefType type) {
276276
hasFieldWithJsonTypeAnnotation(type.getASupertype*()) or
277277
hasFieldWithJsonTypeAnnotation(type.getAField().getType())
278278
}
@@ -281,7 +281,7 @@ predicate hasJsonTypeInfoAnnotation(RefType type) {
281281
* Holds if `type` has a field with `JsonTypeInfo` annotation
282282
* that enables polymorphic type handling.
283283
*/
284-
predicate hasFieldWithJsonTypeAnnotation(RefType type) {
284+
private predicate hasFieldWithJsonTypeAnnotation(RefType type) {
285285
exists(Annotation a |
286286
type.getAField().getAnAnnotation() = a and
287287
a.getType().hasQualifiedName("com.fasterxml.jackson.annotation", "JsonTypeInfo") and
@@ -324,15 +324,15 @@ predicate unsafeDeserialization(MethodAccess ma, Expr sink) {
324324
ma.getMethod() instanceof ObjectMapperReadMethod and
325325
sink = ma.getArgument(0) and
326326
(
327-
exists(UnsafeType config | config.hasFlowToExpr(ma.getAnArgument()))
327+
exists(UnsafeTypeConfig config | config.hasFlowToExpr(ma.getAnArgument()))
328328
or
329-
exists(EnabledJacksonDefaultTyping config | config.hasFlowToExpr(ma.getQualifier()))
329+
exists(EnableJacksonDefaultTypingConfig config | config.hasFlowToExpr(ma.getQualifier()))
330330
or
331331
exists(RefType argType, int i | i > 0 and argType = ma.getArgument(i).getType() |
332332
hasJsonTypeInfoAnnotation(argType.(ParameterizedType).getATypeArgument())
333333
)
334334
) and
335-
not exists(SafeObjectMapper config | config.hasFlowToExpr(ma.getQualifier()))
335+
not exists(SafeObjectMapperConfig config | config.hasFlowToExpr(ma.getQualifier()))
336336
)
337337
}
338338

0 commit comments

Comments
 (0)