Skip to content

Commit d2140eb

Browse files
author
retanoj
committed
MyBatisAnnotationSqlInjection no @param case
1 parent b171dc9 commit d2140eb

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

java/ql/src/experimental/Security/CWE/CWE-089/MyBatisCommonLib.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,21 @@ predicate isMybatisXmlOrAnnotationSqlInjection(
185185
unsafeExpression.matches("${%}") and
186186
ma.getAnArgument() = node.asExpr()
187187
)
188+
or
189+
// Some of method parameters are not annotated with `@Param`, which named in the SQL statement as their name.
190+
// Improper use of these parameters has a SQL injection vulnerability.
191+
// e.g.
192+
//
193+
// ```java
194+
// @Select(select id,name from test where id = #{id} or name = '${name}')
195+
// Test test(Integer id, String name);
196+
// ```
197+
exists(Parameter param, int idx |
198+
param = ma.getMethod().getParameter(idx)
199+
|
200+
not param.getAnAnnotation().getType() instanceof TypeParam and
201+
unsafeExpression.matches("${" + param.getName() + "}") and
202+
ma.getArgument(idx) = node.asExpr()
203+
)
188204
)
189205
}

java/ql/test/experimental/query-tests/security/CWE-089/src/main/MybatisSqlInjection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public void bad9(@RequestParam String name) {
6363
mybatisSqlInjectionService.bad9(name);
6464
}
6565

66+
@GetMapping(value = "msi10")
67+
public void bad10(@RequestParam Integer id, @RequestParam String name) {
68+
mybatisSqlInjectionService.bad10(id, name);
69+
}
70+
6671
@GetMapping(value = "good1")
6772
public List<Test> good1(Integer id) {
6873
List<Test> result = mybatisSqlInjectionService.good1(id);
@@ -99,4 +104,9 @@ public void good2(@RequestParam String name, @RequestParam Integer age) {
99104
public void good3(@RequestParam String age) {
100105
mybatisSqlInjectionService.good3(age);
101106
}
107+
108+
@GetMapping(value = "good4")
109+
public void bad10(@RequestParam Integer id, @RequestParam String name) {
110+
mybatisSqlInjectionService.good4(id, name);
111+
}
102112
}

java/ql/test/experimental/query-tests/security/CWE-089/src/main/MybatisSqlInjectionService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public void bad9(String name) {
5151
sqlInjectionMapper.bad9(hashMap);
5252
}
5353

54+
public void bad9(Integer id, String name) {
55+
sqlInjectionMapper.bad10(id, name);
56+
}
57+
5458
public List<Test> good1(Integer id) {
5559
List<Test> result = sqlInjectionMapper.good1(id);
5660
return result;
@@ -80,4 +84,8 @@ public void good2(String name, Integer age){
8084
public void good3(String age){
8185
sqlInjectionMapper.good3(age);
8286
}
87+
88+
public void good4(Integer id, String name) {
89+
sqlInjectionMapper.good4(id, name);
90+
}
8391
}

java/ql/test/experimental/query-tests/security/CWE-089/src/main/SqlInjectionMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public interface SqlInjectionMapper {
3333
@Select({"select * from test", "where id = ${name}"})
3434
public Test bad9(HashMap<String, Object> map);
3535

36+
@Select({"select * from test where id = #{id} and name = '${name}'"})
37+
String bad10(Integer id, String name);
38+
3639
List<Test> good1(Integer id);
3740

3841
//using providers
@@ -66,4 +69,6 @@ public interface SqlInjectionMapper {
6669
@Select("select * from user_info where age = #{age}")
6770
String good3(@Param("age") String age);
6871

72+
@Select({"select * from test where id = #{id} and name = #{name}"})
73+
String good4(Integer id, String name);
6974
}

0 commit comments

Comments
 (0)