Skip to content

Commit daf6a4c

Browse files
committed
Partial modification 2
1 parent 6c6113b commit daf6a4c

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

java/ql/lib/semmle/code/java/frameworks/MyBatis.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private class IbatisUpdateAnnotationType extends AnnotationType {
7070
}
7171

7272
/**
73-
* Ibatis sql operation annotation.
73+
* An Ibatis SQL operation annotation.
7474
*/
7575
class IbatisSqlOperationAnnotation extends Annotation {
7676
IbatisSqlOperationAnnotation() {
@@ -84,9 +84,7 @@ class IbatisSqlOperationAnnotation extends Annotation {
8484
* Get the SQL statement string.
8585
*/
8686
string getSqlValue() {
87-
result = this.getValue("value").(CompileTimeConstantExpr).getStringValue() or
88-
result =
89-
this.getValue("value").(ArrayInit).getInit(_).(CompileTimeConstantExpr).getStringValue()
87+
result = this.getAValue("value").(CompileTimeConstantExpr).getStringValue()
9088
}
9189
}
9290

java/ql/src/experimental/Security/CWE/CWE-089/MyBatisAnnotationSqlInjection.qhelp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
"qhelp.dtd">
55
<qhelp>
66
<overview>
7-
<p>MyBatis allows operating the database by annotating a method with the annotations <code>@Select</code>, <code>@Insert</code>, etc. to construct dynamic SQL statements.
7+
<p>MyBatis uses methods with the annotations <code>@Select</code>, <code>@Insert</code>, etc. to construct dynamic SQL statements.
88
If the syntax <code>${param}</code> is used in those statements, and <code>param</code> is a parameter of the annotated method, attackers can exploit this to tamper with the SQL statements or execute arbitrary SQL commands.</p>
99
</overview>
1010

1111
<<recommendation>
1212
<p>
13-
When writing MyBatis mapping statements, try to use the syntax <code>#{xxx}</code>. If the syntax <code>${xxx}</code> must be used, any parameters included in it should be sanitized to prevent SQL injection attacks.
13+
When writing MyBatis mapping statements, use the syntax <code>#{xxx}</code> whenever possible. If the syntax <code>${xxx}</code> must be used, any parameters included in it should be sanitized to prevent SQL injection attacks.
1414
</p>
1515
</recommendation>
1616

1717
<example>
1818
<p>
1919
The following sample shows a bad and a good example of MyBatis annotations usage. The <code>bad1</code> method uses <code>$(name)</code>
2020
in the <code>@Select</code> annotation to dynamically build a SQL statement, which causes a SQL injection vulnerability.
21-
The <code>good1</code> method uses <code>#{name}</code> in the <code>@Select</code> annotation to to dynamically include the parameter in a SQL statement, which allows the MyBatis framework to handle the sanitization, preventing the vulnerability.
21+
The <code>good1</code> method uses <code>#{name}</code> in the <code>@Select</code> annotation to dynamically include the parameter in a SQL statement, which causes the MyBatis framework to sanitize the input provided, preventing the vulnerability.
2222
</p>
2323
<sample src="MyBatisAnnotationSqlInjection.java" />
2424
</example>

java/ql/src/experimental/Security/CWE/CWE-089/MyBatisAnnotationSqlInjection.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private class MyBatisAnnotationSqlInjectionConfiguration extends TaintTracking::
2323
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
2424

2525
override predicate isSink(DataFlow::Node sink) {
26-
sink instanceof MyBatisAnnotationMethodCallAnArgument
26+
sink instanceof MyBatisAnnotatedMethodCallArgument
2727
}
2828

2929
override predicate isSanitizer(DataFlow::Node node) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
/**
2-
* Provides classes for SQL injection detection in MyBatis annotation.
2+
* Provides classes for SQL injection detection regarding MyBatis annotated methods.
33
*/
44

55
import java
66
import MyBatisCommonLib
77
import semmle.code.java.dataflow.FlowSources
88
import semmle.code.java.frameworks.Properties
99

10-
/** A sink for MyBatis annotation method call an argument. */
11-
class MyBatisAnnotationMethodCallAnArgument extends DataFlow::Node {
12-
MyBatisAnnotationMethodCallAnArgument() {
10+
/** An argument of a MyBatis annotated method. */
11+
class MyBatisAnnotatedMethodCallArgument extends DataFlow::Node {
12+
MyBatisAnnotatedMethodCallArgument() {
1313
exists(MyBatisSqlOperationAnnotationMethod msoam, MethodAccess ma | ma.getMethod() = msoam |
1414
ma.getAnArgument() = this.asExpr()
1515
)

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ MethodAccess getMyBatisSqlOperationAnnotationMethodAccess(IbatisSqlOperationAnno
6868
}
6969

7070
/** Get the #{...} or ${...} parameters in the Mybatis mapper xml file. */
71-
private string getAnMybatiXmlSetValue(XMLElement xmle) {
71+
private string getAnMybatisXmlSetValue(XMLElement xmle) {
7272
result = xmle.getTextValue().trim().regexpFind("(#|\\$)\\{[^\\}]*\\}", _, _)
7373
}
7474

7575
/** Get the #{...} or ${...} parameters in the Mybatis sql operation annotation value. */
76-
private string getAnMybatiAnnotationSetValue(IbatisSqlOperationAnnotation isoa) {
76+
private string getAMybatisAnnotationSqlValue(IbatisSqlOperationAnnotation isoa) {
7777
result = isoa.getSqlValue().trim().regexpFind("(#|\\$)\\{[^\\}]*\\}", _, _)
7878
}
7979

@@ -84,11 +84,12 @@ predicate isMybatisXmlOrAnnotationSqlInjection(
8484
exists(MethodAccess ma, string setValue |
8585
(
8686
ma = getMyBatisMapperXmlMethodAccess(xmle) and
87-
setValue = getAnMybatiXmlSetValue(xmle)
87+
setValue = getAnMybatisXmlSetValue(xmle)
8888
or
8989
ma = getMyBatisSqlOperationAnnotationMethodAccess(isoa) and
90-
setValue = getAnMybatiAnnotationSetValue(isoa)
90+
setValue = getAMybatisAnnotationSqlValue(isoa)
9191
) and
92+
not setValue.regexpMatch("\\$\\{" + getAnMybatisConfigurationVariableKey() + "\\}") and
9293
(
9394
// The method parameters use `@Param` annotation. Due to improper use of this parameter, SQL injection vulnerabilities are caused.
9495
// e.g.
@@ -154,7 +155,6 @@ predicate isMybatisXmlOrAnnotationSqlInjection(
154155
ma.getMethod().getParameterType(i) instanceof Array
155156
) and
156157
setValue.matches("%${%}") and
157-
not setValue = "${" + getAnMybatisConfigurationVariableKey() + "}" and
158158
ma.getArgument(i) = node.asExpr()
159159
)
160160
or
@@ -170,7 +170,6 @@ predicate isMybatisXmlOrAnnotationSqlInjection(
170170
ma.getMethod().getNumberOfParameters() = i and
171171
not ma.getMethod().getAParameter().getAnAnnotation().getType() instanceof TypeParam and
172172
setValue.matches("%${%}") and
173-
not setValue = "${" + getAnMybatisConfigurationVariableKey() + "}" and
174173
ma.getAnArgument() = node.asExpr()
175174
)
176175
)

0 commit comments

Comments
 (0)