|
3 | 3 | */
|
4 | 4 |
|
5 | 5 | import java
|
| 6 | +import semmle.code.xml.MyBatisMapperXML |
6 | 7 | import semmle.code.java.dataflow.FlowSources
|
7 | 8 | import semmle.code.java.frameworks.MyBatis
|
8 | 9 | import semmle.code.java.frameworks.Properties
|
@@ -42,3 +43,136 @@ class ListType extends RefType {
|
42 | 43 | this.getSourceDeclaration().getASourceSupertype*().hasQualifiedName("java.util", "List")
|
43 | 44 | }
|
44 | 45 | }
|
| 46 | + |
| 47 | +/** Gets a call to the MyBatis mapper xml method. */ |
| 48 | +MethodAccess getMyBatisMapperXmlMethodAccess(XMLElement xmle) { |
| 49 | + exists(MyBatisMapperSqlOperation mbmxe | |
| 50 | + mbmxe.getMapperMethod() = result.getMethod() and |
| 51 | + ( |
| 52 | + mbmxe.getAChild*() = xmle |
| 53 | + or |
| 54 | + exists(MyBatisMapperSql mbms | |
| 55 | + mbmxe.getInclude().getRefid() = mbms.getId() and |
| 56 | + mbms.getAChild*() = xmle |
| 57 | + ) |
| 58 | + ) |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +/** Gets a call to the MyBatis SQL operation annotation method. */ |
| 63 | +MethodAccess getMyBatisSqlOperationAnnotationMethodAccess(IbatisSqlOperationAnnotation isoa) { |
| 64 | + exists(MyBatisSqlOperationAnnotationMethod msoam | |
| 65 | + msoam = result.getMethod() and |
| 66 | + msoam.getAnAnnotation() = isoa |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +/** Get the #{...} or ${...} parameters in the Mybatis mapper xml file. */ |
| 71 | +private string getAnMybatiXmlSetValue(XMLElement xmle) { |
| 72 | + result = xmle.getTextValue().trim().regexpFind("(#|\\$)\\{[^\\}]*\\}", _, _) |
| 73 | +} |
| 74 | + |
| 75 | +/** Get the #{...} or ${...} parameters in the Mybatis sql operation annotation value. */ |
| 76 | +private string getAnMybatiAnnotationSetValue(IbatisSqlOperationAnnotation isoa) { |
| 77 | + result = isoa.getSqlValue().trim().regexpFind("(#|\\$)\\{[^\\}]*\\}", _, _) |
| 78 | +} |
| 79 | + |
| 80 | +/** Holds if it is SQL injection of MyBatis xml or MyBatis annotation. */ |
| 81 | +predicate isMybatisXmlOrAnnotationSqlInjection( |
| 82 | + DataFlow::Node node, XMLElement xmle, IbatisSqlOperationAnnotation isoa |
| 83 | +) { |
| 84 | + exists(MethodAccess ma, string setValue | |
| 85 | + ( |
| 86 | + ma = getMyBatisMapperXmlMethodAccess(xmle) and |
| 87 | + setValue = getAnMybatiXmlSetValue(xmle) |
| 88 | + or |
| 89 | + ma = getMyBatisSqlOperationAnnotationMethodAccess(isoa) and |
| 90 | + setValue = getAnMybatiAnnotationSetValue(isoa) |
| 91 | + ) and |
| 92 | + ( |
| 93 | + // The method parameters use `@Param` annotation. Due to improper use of this parameter, SQL injection vulnerabilities are caused. |
| 94 | + // e.g. |
| 95 | + // |
| 96 | + // ```java |
| 97 | + // @Select(select id,name from test order by ${orderby,jdbcType=VARCHAR}) |
| 98 | + // void test(@Param("orderby") String name); |
| 99 | + // ``` |
| 100 | + exists(int i, Annotation annotation | |
| 101 | + setValue |
| 102 | + .matches("%${" + annotation.getValue("value").(CompileTimeConstantExpr).getStringValue() |
| 103 | + + "%}") and |
| 104 | + annotation.getType() instanceof TypeParam and |
| 105 | + ma.getArgument(i) = node.asExpr() |
| 106 | + ) |
| 107 | + or |
| 108 | + // MyBatis default parameter sql injection vulnerabilities.the default parameter form of the method is arg[0...n] or param[1...n]. |
| 109 | + // e.g. |
| 110 | + // |
| 111 | + // ```java |
| 112 | + // @Select(select id,name from test order by ${arg0,jdbcType=VARCHAR}) |
| 113 | + // void test(String name); |
| 114 | + // ``` |
| 115 | + exists(int i | |
| 116 | + not ma.getMethod().getParameter(i).getAnAnnotation().getType() instanceof TypeParam and |
| 117 | + ( |
| 118 | + setValue.matches("%${param" + (i + 1) + "%}") |
| 119 | + or |
| 120 | + setValue.matches("%${arg" + i + "%}") |
| 121 | + ) and |
| 122 | + not setValue = "${" + getAnMybatisConfigurationVariableKey() + "}" and |
| 123 | + ma.getArgument(i) = node.asExpr() |
| 124 | + ) |
| 125 | + or |
| 126 | + // SQL injection vulnerability caused by improper use of MyBatis instance class fields. |
| 127 | + // e.g. |
| 128 | + // |
| 129 | + // ```java |
| 130 | + // @Select(select id,name from test order by ${name,jdbcType=VARCHAR}) |
| 131 | + // void test(Test test); |
| 132 | + // ``` |
| 133 | + exists(int i, RefType t | |
| 134 | + not ma.getMethod().getParameter(i).getAnAnnotation().getType() instanceof TypeParam and |
| 135 | + ma.getMethod().getParameterType(i).getName() = t.getName() and |
| 136 | + setValue.matches("%${" + t.getAField().getName() + "%}") and |
| 137 | + ma.getArgument(i) = node.asExpr() |
| 138 | + ) |
| 139 | + or |
| 140 | + // The parameter type of the MyBatis method parameter is Map or List or Array. |
| 141 | + // SQL injection vulnerability caused by improper use of this parameter. |
| 142 | + // e.g. |
| 143 | + // |
| 144 | + // ```java |
| 145 | + // @Select(select id,name from test where name like '%${value}%') |
| 146 | + // Test test(Map map); |
| 147 | + // ``` |
| 148 | + exists(int i, MyBatisMapperForeach mbmf | |
| 149 | + mbmf = xmle and |
| 150 | + not ma.getMethod().getParameter(i).getAnAnnotation().getType() instanceof TypeParam and |
| 151 | + ( |
| 152 | + ma.getMethod().getParameterType(i) instanceof MapType or |
| 153 | + ma.getMethod().getParameterType(i) instanceof ListType or |
| 154 | + ma.getMethod().getParameterType(i) instanceof Array |
| 155 | + ) and |
| 156 | + setValue.matches("%${%}") and |
| 157 | + not setValue = "${" + getAnMybatisConfigurationVariableKey() + "}" and |
| 158 | + ma.getArgument(i) = node.asExpr() |
| 159 | + ) |
| 160 | + or |
| 161 | + // This method has only one parameter and the parameter is not annotated with `@Param`. |
| 162 | + // Improper use of this parameter has a SQL injection vulnerability. |
| 163 | + // e.g. |
| 164 | + // |
| 165 | + // ```java |
| 166 | + // @Select(select id,name from test where name like '%${value}%') |
| 167 | + // Test test(String name); |
| 168 | + // ``` |
| 169 | + exists(int i | i = 1 | |
| 170 | + ma.getMethod().getNumberOfParameters() = i and |
| 171 | + not ma.getMethod().getAParameter().getAnAnnotation().getType() instanceof TypeParam and |
| 172 | + setValue.matches("%${%}") and |
| 173 | + not setValue = "${" + getAnMybatisConfigurationVariableKey() + "}" and |
| 174 | + ma.getAnArgument() = node.asExpr() |
| 175 | + ) |
| 176 | + ) |
| 177 | + ) |
| 178 | +} |
0 commit comments