Skip to content

Commit 80ee92a

Browse files
committed
Java: Add support for FastJson in unsafe deserialization.
1 parent 09cfb24 commit 80ee92a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lgtm,codescanning
2+
* The "Deserialization of user-controlled data" (`java/unsafe-deserialization`) query
3+
now recognizes `FastJson` deserialization.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Provides classes and predicates for working with the FastJson framework.
3+
*/
4+
5+
import java
6+
7+
/**
8+
* The class `com.alibaba.fastjson.JSON` or `com.alibaba.fastjson.JSONObject`.
9+
*/
10+
class FastJson extends RefType {
11+
FastJson() {
12+
this.hasQualifiedName("com.alibaba.fastjson", "JSON") or
13+
this.hasQualifiedName("com.alibaba.fastjson", "JSONObject")
14+
}
15+
}
16+
17+
/**
18+
* A FastJson parse method. This is either `parse` or `parseObject` on either
19+
* `com.alibaba.fastjson.JSON` or `com.alibaba.fastjson.JSONObject`.
20+
*/
21+
class FastJsonParseMethod extends Method {
22+
FastJsonParseMethod() {
23+
this.getDeclaringType() instanceof FastJson and
24+
this.hasName(["parse", "parseObject"])
25+
}
26+
}
27+
28+
/**
29+
* A call to `ParserConfig.setSafeMode`.
30+
*/
31+
class FastJsonSetSafeMode extends MethodAccess {
32+
FastJsonSetSafeMode() {
33+
exists(Method m |
34+
this.getMethod() = m and
35+
m.hasName("setSafeMode") and
36+
m.getDeclaringType().hasQualifiedName("com.alibaba.fastjson.parser", "ParserConfig")
37+
)
38+
}
39+
40+
/** Gets the constant value passed to this call, if any. */
41+
boolean getMode() { result = this.getArgument(0).(CompileTimeConstantExpr).getBooleanValue() }
42+
}
43+
44+
/**
45+
* Holds if there is some call to `ParserConfig.setSafeMode` that does not
46+
* explicitly disable safe mode.
47+
*/
48+
predicate fastJsonLooksSafe() {
49+
exists(FastJsonSetSafeMode setsafe | not setsafe.getMode() = false)
50+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import semmle.code.java.frameworks.Kryo
22
import semmle.code.java.frameworks.XStream
33
import semmle.code.java.frameworks.SnakeYaml
4+
import semmle.code.java.frameworks.FastJson
45
import semmle.code.java.frameworks.apache.Lang
56

67
class ObjectInputStreamReadObjectMethod extends Method {
@@ -77,6 +78,10 @@ predicate unsafeDeserialization(MethodAccess ma, Expr sink) {
7778
or
7879
ma instanceof UnsafeSnakeYamlParse and
7980
sink = ma.getArgument(0)
81+
or
82+
ma.getMethod() instanceof FastJsonParseMethod and
83+
not fastJsonLooksSafe() and
84+
sink = ma.getArgument(0)
8085
)
8186
}
8287

0 commit comments

Comments
 (0)