Skip to content

Commit e879ae7

Browse files
committed
Add the sanitizer of Flexjson
1 parent ce94c86 commit e879ae7

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

java/ql/src/semmle/code/java/frameworks/Flexjson.qll

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@ class FlexjsonSerializer extends RefType {
1717
/** The deserialization method `deserialize`. */
1818
class FlexjsonDeserializeMethod extends Method {
1919
FlexjsonDeserializeMethod() {
20-
(
21-
this.getDeclaringType().(ParameterizedType).getGenericType() instanceof FlexjsonDeserializer or
22-
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof
23-
FlexjsonDeserializer
24-
) and
20+
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof
21+
FlexjsonDeserializer and
2522
this.getName().matches("deserialize%")
2623
}
2724
}
2825

2926
/** The serialization method `serialize`. */
3027
class FlexjsonSerializeMethod extends Method {
3128
FlexjsonSerializeMethod() {
32-
(
33-
this.getDeclaringType().(ParameterizedType).getGenericType() instanceof FlexjsonSerializer or
34-
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof
35-
FlexjsonSerializer
36-
) and
29+
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof
30+
FlexjsonSerializer and
3731
this.hasName(["serialize", "deepSerialize"])
3832
}
3933
}
34+
35+
/** The method `use` to configure allowed class type. */
36+
class DeserializerUseMethod extends Method {
37+
DeserializerUseMethod() {
38+
this.getDeclaringType().getSourceDeclaration().getASourceSupertype*() instanceof
39+
FlexjsonDeserializer and
40+
this.hasName("use")
41+
}
42+
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,18 @@ predicate unsafeDeserialization(MethodAccess ma, Expr sink) {
264264
or
265265
m instanceof FlexjsonDeserializeMethod and
266266
sink = ma.getArgument(0) and
267-
not exists(TypeLiteral tl |
268-
ma.getArgument(1) = tl and
269-
tl.getType().(ParameterizedType).getATypeArgument().(Class).isFinal()
267+
(
268+
not exists(TypeLiteral tl |
269+
ma.getArgument(1) = tl and
270+
tl.getType().(ParameterizedType).getATypeArgument().(Class).isFinal()
271+
) and
272+
not exists(MethodAccess dma |
273+
dma.getMethod() instanceof DeserializerUseMethod and
274+
(
275+
ma.getQualifier() = dma or
276+
ma.getQualifier().(VarAccess).getVariable().getAnAccess() = dma.getQualifier()
277+
)
278+
)
270279
)
271280
or
272281
m instanceof JoddJsonParseMethod and

java/ql/test/query-tests/security/CWE-502/FlexjsonServlet.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ public void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOExc
5050
JSONDeserializer deserializer = new JSONDeserializer();
5151
User user = (User) deserializer.deserialize(req.getReader(), Object.class);
5252
}
53+
54+
private Person fromJsonToPerson(String json) {
55+
return new JSONDeserializer<Person>().use(null, Person.class).deserialize(json);
56+
}
57+
58+
// GOOD: Specify the class to deserialize with `use`
59+
public void doPut2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
60+
String json = req.getParameter("json");
61+
Person person = fromJsonToPerson(json);
62+
}
5363
}

0 commit comments

Comments
 (0)