|
| 1 | +/** |
| 2 | + * Provides public classes for MyBatis SQL injection detection. |
| 3 | + */ |
| 4 | + |
| 5 | +import java |
| 6 | +import semmle.code.xml.MyBatisMapperXML |
| 7 | +import semmle.code.java.dataflow.FlowSources |
| 8 | +import semmle.code.java.frameworks.MyBatis |
| 9 | +import semmle.code.java.frameworks.Properties |
| 10 | + |
| 11 | +private predicate propertiesKey(DataFlow::Node prop, string key) { |
| 12 | + exists(MethodAccess m | |
| 13 | + m.getMethod() instanceof PropertiesSetPropertyMethod and |
| 14 | + key = m.getArgument(0).(CompileTimeConstantExpr).getStringValue() and |
| 15 | + prop.asExpr() = m.getQualifier() |
| 16 | + ) |
| 17 | +} |
| 18 | + |
| 19 | +/** A data flow configuration tracing flow from ibatis `Configuration.getVariables()` to a store into a `Properties` object. */ |
| 20 | +private class PropertiesFlowConfig extends DataFlow2::Configuration { |
| 21 | + PropertiesFlowConfig() { this = "PropertiesFlowConfig" } |
| 22 | + |
| 23 | + override predicate isSource(DataFlow::Node src) { |
| 24 | + exists(MethodAccess ma | ma.getMethod() instanceof IbatisConfigurationGetVariablesMethod | |
| 25 | + src.asExpr() = ma |
| 26 | + ) |
| 27 | + } |
| 28 | + |
| 29 | + override predicate isSink(DataFlow::Node sink) { propertiesKey(sink, _) } |
| 30 | +} |
| 31 | + |
| 32 | +/** Gets a `Properties` key that may map onto a Mybatis `Configuration` variable. */ |
| 33 | +string getAMybatisConfigurationVariableKey() { |
| 34 | + exists(PropertiesFlowConfig conf, DataFlow::Node n | |
| 35 | + propertiesKey(n, result) and |
| 36 | + conf.hasFlowTo(n) |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +/** A reference type that extends a parameterization of `java.util.List`. */ |
| 41 | +class ListType extends RefType { |
| 42 | + ListType() { |
| 43 | + this.getSourceDeclaration().getASourceSupertype*().hasQualifiedName("java.util", "List") |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** Holds if the specified `method` uses MyBatis Mapper XMLElement `mmxx`. */ |
| 48 | +predicate myBatisMapperXMLElementFromMethod(Method method, MyBatisMapperXMLElement mmxx) { |
| 49 | + exists(MyBatisMapperSqlOperation mbmxe | mbmxe.getMapperMethod() = method | |
| 50 | + mbmxe.getAChild*() = mmxx |
| 51 | + or |
| 52 | + exists(MyBatisMapperSql mbms | |
| 53 | + mbmxe.getInclude().getRefid() = mbms.getId() and |
| 54 | + mbms.getAChild*() = mmxx |
| 55 | + ) |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +/** Holds if the specified `method` has Ibatis Sql operation annotation `isoa`. */ |
| 60 | +predicate myBatisSqlOperationAnnotationFromMethod(Method method, IbatisSqlOperationAnnotation isoa) { |
| 61 | + exists(MyBatisSqlOperationAnnotationMethod msoam | |
| 62 | + msoam = method and |
| 63 | + msoam.getAnAnnotation() = isoa |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +/** Gets a `#{...}` or `${...}` expression argument in XML element `xmle`. */ |
| 68 | +string getAMybatisXmlSetValue(XMLElement xmle) { |
| 69 | + result = xmle.getTextValue().regexpFind("(#|\\$)\\{[^\\}]*\\}", _, _) |
| 70 | +} |
| 71 | + |
| 72 | +/** Gets a `#{...}` or `${...}` expression argument in annotation `isoa`. */ |
| 73 | +string getAMybatisAnnotationSqlValue(IbatisSqlOperationAnnotation isoa) { |
| 74 | + result = isoa.getSqlValue().regexpFind("(#|\\$)\\{[^\\}]*\\}", _, _) |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Holds if `node` is an argument to `ma` that is vulnerable to SQL injection attacks if `unsafeExpression` occurs in a MyBatis SQL expression. |
| 79 | + * |
| 80 | + * This case currently assumes all `${...}` expressions are potentially dangerous when there is a non-`@Param` annotated, collection-typed parameter to `ma`. |
| 81 | + */ |
| 82 | +bindingset[unsafeExpression] |
| 83 | +predicate isMybatisCollectionTypeSqlInjection( |
| 84 | + DataFlow::Node node, MethodAccess ma, string unsafeExpression |
| 85 | +) { |
| 86 | + not unsafeExpression.regexpMatch("\\$\\{" + getAMybatisConfigurationVariableKey() + "\\}") and |
| 87 | + // The parameter type of the MyBatis method parameter is Map or List or Array. |
| 88 | + // SQL injection vulnerability caused by improper use of this parameter. |
| 89 | + // e.g. |
| 90 | + // |
| 91 | + // ```java |
| 92 | + // @Select(select id,name from test where name like '%${value}%') |
| 93 | + // Test test(Map map); |
| 94 | + // ``` |
| 95 | + exists(int i | |
| 96 | + not ma.getMethod().getParameter(i).getAnAnnotation().getType() instanceof TypeParam and |
| 97 | + ( |
| 98 | + ma.getMethod().getParameterType(i) instanceof MapType or |
| 99 | + ma.getMethod().getParameterType(i) instanceof ListType or |
| 100 | + ma.getMethod().getParameterType(i) instanceof Array |
| 101 | + ) and |
| 102 | + unsafeExpression.matches("${%}") and |
| 103 | + ma.getArgument(i) = node.asExpr() |
| 104 | + ) |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Holds if `node` is an argument to `ma` that is vulnerable to SQL injection attacks if `unsafeExpression` occurs in a MyBatis SQL expression. |
| 109 | + * |
| 110 | + * This accounts for: |
| 111 | + * - arguments referred to by a name given in a `@Param` annotation, |
| 112 | + * - arguments referred to by ordinal position, like `${param1}` |
| 113 | + * - references to class instance fields |
| 114 | + * - any `${}` expression where there is a single, non-`@Param`-annotated argument to `ma`. |
| 115 | + */ |
| 116 | +bindingset[unsafeExpression] |
| 117 | +predicate isMybatisXmlOrAnnotationSqlInjection( |
| 118 | + DataFlow::Node node, MethodAccess ma, string unsafeExpression |
| 119 | +) { |
| 120 | + not unsafeExpression.regexpMatch("\\$\\{" + getAMybatisConfigurationVariableKey() + "\\}") and |
| 121 | + ( |
| 122 | + // The method parameters use `@Param` annotation. Due to improper use of this parameter, SQL injection vulnerabilities are caused. |
| 123 | + // e.g. |
| 124 | + // |
| 125 | + // ```java |
| 126 | + // @Select(select id,name from test order by ${orderby,jdbcType=VARCHAR}) |
| 127 | + // void test(@Param("orderby") String name); |
| 128 | + // ``` |
| 129 | + exists(Annotation annotation | |
| 130 | + unsafeExpression |
| 131 | + .matches("${" + annotation.getValue("value").(CompileTimeConstantExpr).getStringValue() + |
| 132 | + "%}") and |
| 133 | + annotation.getType() instanceof TypeParam and |
| 134 | + ma.getAnArgument() = node.asExpr() |
| 135 | + ) |
| 136 | + or |
| 137 | + // MyBatis default parameter sql injection vulnerabilities.the default parameter form of the method is arg[0...n] or param[1...n]. |
| 138 | + // e.g. |
| 139 | + // |
| 140 | + // ```java |
| 141 | + // @Select(select id,name from test order by ${arg0,jdbcType=VARCHAR}) |
| 142 | + // void test(String name); |
| 143 | + // ``` |
| 144 | + exists(int i | |
| 145 | + not ma.getMethod().getParameter(i).getAnAnnotation().getType() instanceof TypeParam and |
| 146 | + ( |
| 147 | + unsafeExpression.matches("${param" + (i + 1) + "%}") |
| 148 | + or |
| 149 | + unsafeExpression.matches("${arg" + i + "%}") |
| 150 | + ) and |
| 151 | + ma.getArgument(i) = node.asExpr() |
| 152 | + ) |
| 153 | + or |
| 154 | + // SQL injection vulnerability caused by improper use of MyBatis instance class fields. |
| 155 | + // e.g. |
| 156 | + // |
| 157 | + // ```java |
| 158 | + // @Select(select id,name from test order by ${name,jdbcType=VARCHAR}) |
| 159 | + // void test(Test test); |
| 160 | + // ``` |
| 161 | + exists(int i, RefType t | |
| 162 | + not ma.getMethod().getParameter(i).getAnAnnotation().getType() instanceof TypeParam and |
| 163 | + ma.getMethod().getParameterType(i).getName() = t.getName() and |
| 164 | + unsafeExpression.matches("${" + t.getAField().getName() + "%}") and |
| 165 | + ma.getArgument(i) = node.asExpr() |
| 166 | + ) |
| 167 | + or |
| 168 | + // This method has only one parameter and the parameter is not annotated with `@Param`. The parameter can be named arbitrarily in the SQL statement. |
| 169 | + // If the number of method variables is greater than one, they cannot be named arbitrarily. |
| 170 | + // Improper use of this parameter has a SQL injection vulnerability. |
| 171 | + // e.g. |
| 172 | + // |
| 173 | + // ```java |
| 174 | + // @Select(select id,name from test where name like '%${value}%') |
| 175 | + // Test test(String name); |
| 176 | + // ``` |
| 177 | + exists(int i | i = 1 | |
| 178 | + ma.getMethod().getNumberOfParameters() = i and |
| 179 | + not ma.getMethod().getAParameter().getAnAnnotation().getType() instanceof TypeParam and |
| 180 | + unsafeExpression.matches("${%}") and |
| 181 | + ma.getAnArgument() = node.asExpr() |
| 182 | + ) |
| 183 | + ) |
| 184 | +} |
0 commit comments