|
| 1 | +import java |
| 2 | +import MyBatisCommonLib |
| 3 | +import semmle.code.xml.MyBatisMapperXML |
| 4 | +import semmle.code.java.dataflow.FlowSources |
| 5 | +import semmle.code.java.frameworks.Properties |
| 6 | + |
| 7 | +/** A sink for MyBatis annotation method call an argument. */ |
| 8 | +class MyBatisAnnotationMethodCallAnArgument extends DataFlow::Node { |
| 9 | + MyBatisAnnotationMethodCallAnArgument() { |
| 10 | + exists(MybatisSqlOperationAnnotationMethod msoam, MethodAccess ma | ma.getMethod() = msoam | |
| 11 | + ma.getAnArgument() = this.asExpr() |
| 12 | + ) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/** Get the #{...} or ${...} parameters in the Mybatis annotation value. */ |
| 17 | +private string getAnMybatiAnnotationSetValue(IbatisSqlOperationAnnotation isoa) { |
| 18 | + result = isoa.getSqlValue().trim().regexpFind("(#|\\$)(\\{([^\\}]*\\}))", _, _) |
| 19 | +} |
| 20 | + |
| 21 | +predicate isMybatisAnnotationSqlInjection(DataFlow::Node node, IbatisSqlOperationAnnotation isoa) { |
| 22 | + // MyBatis uses an annotation method to perform SQL operations. This method has only one parameter and |
| 23 | + // the parameter is not annotated with `@Param`. Improper use of this parameter has a SQL injection vulnerability. |
| 24 | + // e.g. |
| 25 | + // |
| 26 | + // ```java |
| 27 | + // @Select(select id,name from test where name like '%${value}%') |
| 28 | + // Test test(String name); |
| 29 | + // ``` |
| 30 | + exists(MybatisSqlOperationAnnotationMethod msoam, MethodAccess ma, string res | |
| 31 | + msoam = ma.getMethod() |
| 32 | + | |
| 33 | + msoam.getAnAnnotation() = isoa and |
| 34 | + res = getAnMybatiAnnotationSetValue(isoa) and |
| 35 | + msoam.getNumberOfParameters() = 1 and |
| 36 | + not ma.getMethod().getAParameter().hasAnnotation() and |
| 37 | + res.matches("%${%}") and |
| 38 | + not res.matches("${" + getAnMybatisConfigurationVariableKey() + "}") and |
| 39 | + ma.getAnArgument() = node.asExpr() |
| 40 | + ) |
| 41 | + or |
| 42 | + // MyBatis uses an annotation method to perform SQL operations. The parameter type of the method parameter |
| 43 | + // is Map or List or Array. SQL injection vulnerability caused by improper use of this parameter. |
| 44 | + // e.g. |
| 45 | + // |
| 46 | + // ```java |
| 47 | + // @Select(select id,name from test where name like '%${value}%') |
| 48 | + // Test test(Map map); |
| 49 | + // ``` |
| 50 | + exists(MybatisSqlOperationAnnotationMethod msoam, MethodAccess ma, int i, string res | |
| 51 | + msoam = ma.getMethod() |
| 52 | + | |
| 53 | + msoam.getAnAnnotation() = isoa and |
| 54 | + not ma.getMethod().getParameter(i).hasAnnotation() and |
| 55 | + ( |
| 56 | + ma.getMethod().getParameterType(i) instanceof MapType or |
| 57 | + ma.getMethod().getParameterType(i) instanceof ListType or |
| 58 | + ma.getMethod().getParameterType(i) instanceof Array |
| 59 | + ) and |
| 60 | + res = getAnMybatiAnnotationSetValue(isoa) and |
| 61 | + res.matches("%${%}") and |
| 62 | + not res.matches("${" + getAnMybatisConfigurationVariableKey() + "}") and |
| 63 | + ma.getArgument(i) = node.asExpr() |
| 64 | + ) |
| 65 | + or |
| 66 | + // MyBatis uses annotation methods to perform SQL operations, and SQL injection vulnerabilities caused by |
| 67 | + // improper use of instance class fields. |
| 68 | + // e.g. |
| 69 | + // |
| 70 | + // ```java |
| 71 | + // @Select(select id,name from test order by ${name,jdbcType=VARCHAR}) |
| 72 | + // void test(Test test); |
| 73 | + // ``` |
| 74 | + exists(MybatisSqlOperationAnnotationMethod msoam, MethodAccess ma, int i, Class c | |
| 75 | + msoam = ma.getMethod() |
| 76 | + | |
| 77 | + msoam.getAnAnnotation() = isoa and |
| 78 | + not ma.getMethod().getParameter(i).hasAnnotation() and |
| 79 | + ma.getMethod().getParameterType(i).getName() = c.getName() and |
| 80 | + getAnMybatiAnnotationSetValue(isoa).matches("%${" + c.getAField().getName() + "%}") and |
| 81 | + ma.getArgument(i) = node.asExpr() |
| 82 | + ) |
| 83 | + or |
| 84 | + // MyBatis uses annotations to perform SQL operations. The method parameters use `@Param` annotation. |
| 85 | + // Due to improper use of this parameter, SQL injection vulnerabilities are caused. |
| 86 | + // e.g. |
| 87 | + // |
| 88 | + // ```java |
| 89 | + // @Select(select id,name from test order by ${orderby,jdbcType=VARCHAR}) |
| 90 | + // void test(@Param("orderby") String name); |
| 91 | + // ``` |
| 92 | + exists(MybatisSqlOperationAnnotationMethod msoam, MethodAccess ma, int i, Annotation annotation | |
| 93 | + msoam = ma.getMethod() |
| 94 | + | |
| 95 | + msoam.getAnAnnotation() = isoa and |
| 96 | + ma.getMethod().getParameter(i).hasAnnotation() and |
| 97 | + ma.getMethod().getParameter(i).getAnAnnotation() = annotation and |
| 98 | + annotation.getType() instanceof TypeParam and |
| 99 | + getAnMybatiAnnotationSetValue(isoa) |
| 100 | + .matches("%${" + annotation.getValue("value").(CompileTimeConstantExpr).getStringValue() + |
| 101 | + "%}") and |
| 102 | + ma.getArgument(i) = node.asExpr() |
| 103 | + ) |
| 104 | + or |
| 105 | + // MyBatis Mapper method default parameter sql injection vulnerabilities.the default parameter form of the method is arg[0...n] or param[1...n]. |
| 106 | + // e.g. |
| 107 | + // |
| 108 | + // ```java |
| 109 | + // @Select(select id,name from test order by ${arg0,jdbcType=VARCHAR}) |
| 110 | + // void test(String name); |
| 111 | + // ``` |
| 112 | + exists(MybatisSqlOperationAnnotationMethod msoam, MethodAccess ma, int i, string res | |
| 113 | + msoam = ma.getMethod() |
| 114 | + | |
| 115 | + msoam.getAnAnnotation() = isoa and |
| 116 | + not ma.getMethod().getParameter(i).hasAnnotation() and |
| 117 | + res = getAnMybatiAnnotationSetValue(isoa) and |
| 118 | + ( |
| 119 | + res.matches("%${param" + (i + 1) + "%}") |
| 120 | + or |
| 121 | + res.matches("%${arg" + i + "%}") |
| 122 | + ) and |
| 123 | + not res.matches("${" + getAnMybatisConfigurationVariableKey() + "}") and |
| 124 | + ma.getArgument(i) = node.asExpr() |
| 125 | + ) |
| 126 | +} |
0 commit comments