@@ -2,40 +2,46 @@ import java
2
2
import semmle.code.java.dataflow.FlowSources
3
3
import semmle.code.java.dataflow.TaintTracking
4
4
5
+ /** The Java class `java.util.regex.Pattern`. */
6
+ private class RegexPattern extends RefType {
7
+ RegexPattern ( ) { this .hasQualifiedName ( "java.util.regex" , "Pattern" ) }
8
+ }
9
+
10
+ /** The Java class `java.util.regex.Matcher`. */
11
+ private class RegexMatcher extends RefType {
12
+ RegexMatcher ( ) { this .hasQualifiedName ( "java.util.regex" , "Matcher" ) }
13
+ }
14
+
15
+ /** The Java class `org.apache.commons.lang3.RegExUtils`. */
16
+ private class ApacheRegExUtils extends RefType {
17
+ ApacheRegExUtils ( ) { this .hasQualifiedName ( "java.util.regex" , "Matcher" ) }
18
+ }
19
+
20
+ // TODO: Are there already classes for any of below(above) in a pre-existing regex library?
21
+ // TODO: look into further: Pattern.matcher, .pattern() and .toString() as taint steps, .split and .splitAsStream
5
22
/**
6
23
* A data flow sink for untrusted user input used to construct regular expressions.
7
24
*/
8
25
class RegexSink extends DataFlow:: ExprNode {
9
26
RegexSink ( ) {
10
27
exists ( MethodAccess ma , Method m | m = ma .getMethod ( ) |
28
+ ma .getArgument ( 0 ) = this .asExpr ( ) and
11
29
(
12
30
m .getDeclaringType ( ) instanceof TypeString and
13
- (
14
- ma .getArgument ( 0 ) = this .asExpr ( ) and // ! combine this line with the below at least? e.g. TypeString and TypePattern both use it
15
- // ! test below more?
16
- // ! (are there already classes for these methods in a regex library?)
17
- m .hasName ( [ "matches" , "split" , "replaceFirst" , "replaceAll" ] )
18
- )
19
- or
20
- // ! make class for the below? (is there already a class for this and its methods in a regex library?)
21
- m .getDeclaringType ( ) .hasQualifiedName ( "java.util.regex" , "Pattern" ) and
22
- (
23
- ma .getArgument ( 0 ) = this .asExpr ( ) and
24
- // ! look into further: Pattern.matcher, .pattern() and .toString() as taint steps, .split and .splitAsStream
25
- m .hasName ( [ "compile" , "matches" ] )
26
- )
31
+ m .hasName ( [ "matches" , "split" , "replaceFirst" , "replaceAll" ] )
27
32
or
28
- // ! make class for the below? (is there already a class for this and its methods in a regex library?)
29
- m .getDeclaringType ( ) .hasQualifiedName ( "org.apache.commons.lang3" , "RegExUtils" ) and
30
- (
31
- ma .getArgument ( 1 ) = this .asExpr ( ) and
32
- m .getParameterType ( 1 ) instanceof TypeString and
33
- // ! test below more?
34
- m .hasName ( [
35
- "removeAll" , "removeFirst" , "removePattern" , "replaceAll" , "replaceFirst" ,
36
- "replacePattern"
37
- ] )
38
- )
33
+ m .getDeclaringType ( ) instanceof RegexPattern and
34
+ m .hasName ( [ "compile" , "matches" ] )
35
+ )
36
+ or
37
+ m .getDeclaringType ( ) instanceof ApacheRegExUtils and
38
+ (
39
+ ma .getArgument ( 1 ) = this .asExpr ( ) and
40
+ m .getParameterType ( 1 ) instanceof TypeString and // only does String here because other option is Patter, but that's already handled by `java.util.regex.Pattern` above
41
+ m .hasName ( [
42
+ "removeAll" , "removeFirst" , "removePattern" , "replaceAll" , "replaceFirst" ,
43
+ "replacePattern"
44
+ ] )
39
45
)
40
46
)
41
47
}
@@ -67,7 +73,7 @@ class RegExpSanitizationCall extends Sanitizer {
67
73
// adds Pattern.quote() as a sanitizer
68
74
// see https://rules.sonarsource.com/java/RSPEC-2631 and https://sensei.securecodewarrior.com/recipes/scw:java:regex-injection
69
75
exists ( MethodAccess ma , Method m | m = ma .getMethod ( ) |
70
- m .getDeclaringType ( ) . hasQualifiedName ( "java.util.regex" , "Pattern" ) and
76
+ m .getDeclaringType ( ) instanceof RegexPattern and
71
77
(
72
78
ma .getArgument ( 0 ) = this .asExpr ( ) and
73
79
m .hasName ( "quote" )
0 commit comments