Skip to content

Commit be39883

Browse files
committed
Change the class name and comment,Use .(CompileTimeConstantExpr).getStringValue()
1 parent 1b948ac commit be39883

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

java/ql/src/experimental/Security/CWE/CWE-352/JsonStringLib.qll

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import semmle.code.java.dataflow.FlowSources
44
import DataFlow::PathGraph
55

66
/** Json string type data. */
7-
abstract class JsonpStringSource extends DataFlow::Node { }
7+
abstract class JsonStringSource extends DataFlow::Node { }
88

9-
/** Convert to String using Gson library. */
10-
private class GsonString extends JsonpStringSource {
9+
/**
10+
* Convert to String using Gson library. *
11+
*
12+
* For example, in the method access `Gson.toJson(...)`,
13+
* the `Object` type data is converted to the `String` type data.
14+
*/
15+
private class GsonString extends JsonStringSource {
1116
GsonString() {
1217
exists(MethodAccess ma, Method m | ma.getMethod() = m |
1318
m.hasName("toJson") and
@@ -17,8 +22,13 @@ private class GsonString extends JsonpStringSource {
1722
}
1823
}
1924

20-
/** Convert to String using Fastjson library. */
21-
private class FastjsonString extends JsonpStringSource {
25+
/**
26+
* Convert to String using Fastjson library.
27+
*
28+
* For example, in the method access `JSON.toJSONString(...)`,
29+
* the `Object` type data is converted to the `String` type data.
30+
*/
31+
private class FastjsonString extends JsonStringSource {
2232
FastjsonString() {
2333
exists(MethodAccess ma, Method m | ma.getMethod() = m |
2434
m.hasName("toJSONString") and
@@ -28,8 +38,13 @@ private class FastjsonString extends JsonpStringSource {
2838
}
2939
}
3040

31-
/** Convert to String using Jackson library. */
32-
private class JacksonString extends JsonpStringSource {
41+
/**
42+
* Convert to String using Jackson library.
43+
*
44+
* For example, in the method access `ObjectMapper.writeValueAsString(...)`,
45+
* the `Object` type data is converted to the `String` type data.
46+
*/
47+
private class JacksonString extends JsonStringSource {
3348
JacksonString() {
3449
exists(MethodAccess ma, Method m | ma.getMethod() = m |
3550
m.hasName("writeValueAsString") and

java/ql/src/experimental/Security/CWE/CWE-352/JsonpInjection.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ where
6969
conf.hasFlowPath(source, sink) and
7070
exists(JsonpInjectionFlowConfig jhfc | jhfc.hasFlowTo(sink.getNode()))
7171
select sink.getNode(), source, sink, "Jsonp response might include code from $@.", source.getNode(),
72-
"this user input"
72+
"this user input"

java/ql/src/experimental/Security/CWE/CWE-352/JsonpInjectionLib.qll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,21 @@ class SpringControllerRequestMappingGetMethod extends SpringControllerGetMethod
8686
}
8787
}
8888

89-
/** A concatenate expression using `(` and `)` or `);`. */
89+
/**
90+
* A concatenate expression using `(` and `)` or `);`.
91+
*
92+
* E.g: `functionName + "(" + json + ")"` or `functionName + "(" + json + ");"`
93+
*/
9094
class JsonpBuilderExpr extends AddExpr {
9195
JsonpBuilderExpr() {
92-
getRightOperand().toString().regexpMatch("\"\\);?\"") and
96+
getRightOperand().(CompileTimeConstantExpr).getStringValue().regexpMatch("\\);?") and
9397
getLeftOperand()
9498
.(AddExpr)
9599
.getLeftOperand()
96100
.(AddExpr)
97101
.getRightOperand()
98-
.toString()
99-
.regexpMatch("\"\\(\"")
102+
.(CompileTimeConstantExpr)
103+
.getStringValue() = "("
100104
}
101105

102106
/** Get the jsonp function name of this expression. */
@@ -123,7 +127,7 @@ class RemoteFlowConfig extends DataFlow2::Configuration {
123127
class JsonDataFlowConfig extends DataFlow2::Configuration {
124128
JsonDataFlowConfig() { this = "JsonDataFlowConfig" }
125129

126-
override predicate isSource(DataFlow::Node src) { src instanceof JsonpStringSource }
130+
override predicate isSource(DataFlow::Node src) { src instanceof JsonStringSource }
127131

128132
override predicate isSink(DataFlow::Node sink) {
129133
exists(JsonpBuilderExpr jhe | jhe.getJsonExpr() = sink.asExpr())

0 commit comments

Comments
 (0)