Skip to content

Commit 97aaf4c

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: handle MyBatis annotations for insert/update/delete
1 parent df77d49 commit 97aaf4c

File tree

7 files changed

+98
-6
lines changed

7 files changed

+98
-6
lines changed

java/ql/lib/semmle/code/java/security/CsrfUnprotectedRequestTypeQuery.qll

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ private class SpringCsrfUnprotectedMethod extends CsrfUnprotectedMethod instance
3636
/** A method that updates a database. */
3737
abstract class DatabaseUpdateMethod extends Method { }
3838

39-
/** A MyBatis Mapper method that updates a database. */
40-
private class MyBatisMapperDatabaseUpdateMethod extends DatabaseUpdateMethod {
41-
MyBatisMapperDatabaseUpdateMethod() {
39+
/** A MyBatis method that updates a database. */
40+
private class MyBatisDatabaseUpdateMethod extends DatabaseUpdateMethod {
41+
MyBatisDatabaseUpdateMethod() {
4242
exists(MyBatisMapperSqlOperation mapperXml |
4343
(
4444
mapperXml instanceof MyBatisMapperInsert or
@@ -47,6 +47,14 @@ private class MyBatisMapperDatabaseUpdateMethod extends DatabaseUpdateMethod {
4747
) and
4848
this = mapperXml.getMapperMethod()
4949
)
50+
or
51+
exists(MyBatisSqlOperationAnnotationMethod m | this = m |
52+
not m.getAnAnnotation().getType().hasQualifiedName("org.apache.ibatis.annotations", "Select")
53+
)
54+
or
55+
exists(Method m | this = m |
56+
m.hasAnnotation("org.apache.ibatis.annotations", ["Delete", "Update", "Insert"] + "Provider")
57+
)
5058
}
5159
}
5260

java/ql/test/query-tests/security/CWE-352/CsrfUnprotectedRequestTypeTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,39 @@ public void bad7(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType
138138
myBatisService.bad7(name);
139139
}
140140

141-
// BAD: uses GET request when updating a database with `@DeleteProvider`
141+
// BAD: uses GET request when updating a database with MyBatis `@DeleteProvider`
142142
@GetMapping(value = "badDelete")
143143
public void badDelete(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType
144144
myBatisService.badDelete(name);
145145
}
146146

147-
// BAD: uses GET request when updating a database with `@UpdateProvider`
147+
// BAD: uses GET request when updating a database with MyBatis `@UpdateProvider`
148148
@GetMapping(value = "badUpdate")
149149
public void badUpdate(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType
150150
myBatisService.badUpdate(name);
151151
}
152152

153-
// BAD: uses GET request when updating a database with `@InsertProvider`
153+
// BAD: uses GET request when updating a database with MyBatis `@InsertProvider`
154154
@GetMapping(value = "badInsert")
155155
public void badInsert(@RequestParam String name) { // $ hasCsrfUnprotectedRequestType
156156
myBatisService.badInsert(name);
157157
}
158+
159+
// BAD: uses GET request when updating a database with MyBatis `@Delete`
160+
@GetMapping(value = "bad8")
161+
public void bad8(@RequestParam int id) { // $ hasCsrfUnprotectedRequestType
162+
myBatisService.bad8(id);
163+
}
164+
165+
// BAD: uses GET request when updating a database with MyBatis `@Insert`
166+
@GetMapping(value = "bad9")
167+
public void bad9(@RequestParam String user) { // $ hasCsrfUnprotectedRequestType
168+
myBatisService.bad9(user);
169+
}
170+
171+
// BAD: uses GET request when updating a database with MyBatis `@Update`
172+
@GetMapping(value = "bad10")
173+
public void bad10(@RequestParam String user) { // $ hasCsrfUnprotectedRequestType
174+
myBatisService.bad10(user);
175+
}
158176
}

java/ql/test/query-tests/security/CWE-352/MyBatisMapper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.apache.ibatis.annotations.DeleteProvider;
44
import org.apache.ibatis.annotations.UpdateProvider;
55
import org.apache.ibatis.annotations.InsertProvider;
6+
import org.apache.ibatis.annotations.Delete;
7+
import org.apache.ibatis.annotations.Update;
8+
import org.apache.ibatis.annotations.Insert;
69

710
@Mapper
811
@Repository
@@ -28,4 +31,13 @@ public interface MyBatisMapper {
2831
method = "badInsert"
2932
)
3033
void badInsert(String input);
34+
35+
@Delete("DELETE FROM users WHERE id = #{id}")
36+
boolean bad8(int id);
37+
38+
@Insert("INSERT INTO users (id, name) VALUES(#{id}, #{name})")
39+
void bad9(String user);
40+
41+
@Update("UPDATE users SET name = #{name} WHERE id = #{id}")
42+
boolean bad10(String user);
3143
}

java/ql/test/query-tests/security/CWE-352/MyBatisService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ public void badUpdate(String input) {
2222
public void badInsert(String input) {
2323
myBatisMapper.badInsert(input);
2424
}
25+
26+
public void bad8(int id){
27+
myBatisMapper.bad8(id);
28+
}
29+
30+
public void bad9(String user){
31+
myBatisMapper.bad9(user);
32+
}
33+
34+
public void bad10(String user){
35+
myBatisMapper.bad10(user);
36+
}
2537
}

java/ql/test/stubs/org.mybatis-3.5.4/org/apache/ibatis/annotations/Delete.java

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/org.mybatis-3.5.4/org/apache/ibatis/annotations/Insert.java

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java/ql/test/stubs/org.mybatis-3.5.4/org/apache/ibatis/annotations/Update.java

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)