3
3
import java
4
4
private import semmle.code.java.dataflow.DataFlow
5
5
private import semmle.code.java.frameworks.Regex
6
- private import semmle.code.java.frameworks.apache.Lang
6
+ //private import semmle.code.java.frameworks.apache.Lang
7
+ private import semmle.code.java.regex.RegexFlowModels
7
8
8
9
/** A data flow sink for untrusted user input used to construct regular expressions. */
9
10
abstract class RegexInjectionSink extends DataFlow:: ExprNode { }
@@ -14,36 +15,41 @@ abstract class RegexInjectionSanitizer extends DataFlow::ExprNode { }
14
15
/** A method call that takes a regular expression as an argument. */
15
16
private class DefaultRegexInjectionSink extends RegexInjectionSink {
16
17
DefaultRegexInjectionSink ( ) {
17
- exists ( MethodAccess ma , Method m | m = ma .getMethod ( ) |
18
- ma .getArgument ( 0 ) = this .asExpr ( ) and
19
- (
20
- m instanceof StringRegexMethod or
21
- m instanceof PatternRegexMethod
22
- )
23
- or
24
- ma .getArgument ( 1 ) = this .asExpr ( ) and
25
- m instanceof ApacheRegExUtilsMethod
18
+ exists ( string kind |
19
+ kind .matches ( [
20
+ "regex-use[]" , "regex-use[f1]" , "regex-use[f-1]" , "regex-use[-1]" , "regex-use[0]"
21
+ ] ) and
22
+ sinkNode ( this , kind )
26
23
)
27
24
}
28
25
}
29
26
30
27
/** A call to a function whose name suggests that it escapes regular expression meta-characters. */
31
28
private class RegexSanitizationCall extends RegexInjectionSanitizer {
32
29
RegexSanitizationCall ( ) {
33
- exists ( string calleeName , string sanitize , string regexp |
30
+ // original
31
+ // exists(string calleeName, string sanitize, string regexp |
32
+ // calleeName = this.asExpr().(Call).getCallee().getName() and
33
+ // sanitize = "(?:escape|saniti[sz]e)" and
34
+ // regexp = "regexp?"
35
+ // |
36
+ // calleeName
37
+ // .regexpMatch("(?i)(" + sanitize + ".*" + regexp + ".*)" + "|(" + regexp + ".*" + sanitize +
38
+ // ".*)")
39
+ // )
40
+ // without regexp
41
+ exists ( string calleeName , string sanitize |
34
42
calleeName = this .asExpr ( ) .( Call ) .getCallee ( ) .getName ( ) and
35
- sanitize = "(?:escape|saniti[sz]e)" and
36
- regexp = "regexp?"
43
+ sanitize = "(?:escape|saniti[sz]e)"
37
44
|
38
- calleeName
39
- .regexpMatch ( "(?i)(" + sanitize + ".*" + regexp + ".*)" + "|(" + regexp + ".*" + sanitize +
40
- ".*)" )
45
+ calleeName .regexpMatch ( "(?i)(.*" + sanitize + ".*)" )
46
+ //calleeName.matches("handleEscapes")
41
47
)
42
48
}
43
49
}
44
50
45
51
/**
46
- * A call to the `Pattern.quote` method, which gives meta-characters or escape sequences
52
+ * A call to the `Pattern.quote` method, which gives metacharacters or escape sequences
47
53
* no special meaning.
48
54
*/
49
55
private class PatternQuoteCall extends RegexInjectionSanitizer {
@@ -56,54 +62,17 @@ private class PatternQuoteCall extends RegexInjectionSanitizer {
56
62
}
57
63
58
64
/**
59
- * Use of the `Pattern.LITERAL` flag with `Pattern.compile`, which gives meta-characters
65
+ * Use of the `Pattern.LITERAL` flag with `Pattern.compile`, which gives metacharacters
60
66
* or escape sequences no special meaning.
61
67
*/
62
68
private class PatternLiteralFlag extends RegexInjectionSanitizer {
63
69
PatternLiteralFlag ( ) {
64
70
exists ( MethodAccess ma , Method m , Field field | m = ma .getMethod ( ) |
65
71
ma .getArgument ( 0 ) = this .asExpr ( ) and
66
- m instanceof PatternRegexMethod and
72
+ m . getDeclaringType ( ) instanceof TypeRegexPattern and
67
73
m .hasName ( "compile" ) and
68
- field instanceof PatternLiteral and
74
+ field instanceof PatternLiteralField and
69
75
ma .getArgument ( 1 ) = field .getAnAccess ( )
70
76
)
71
77
}
72
78
}
73
-
74
- /**
75
- * A method of the class `java.lang.String` that takes a regular expression
76
- * as a parameter.
77
- */
78
- private class StringRegexMethod extends Method {
79
- StringRegexMethod ( ) {
80
- this .getDeclaringType ( ) instanceof TypeString and
81
- this .hasName ( [ "matches" , "split" , "replaceFirst" , "replaceAll" ] )
82
- }
83
- }
84
-
85
- /**
86
- * A method of the class `java.util.regex.Pattern` that takes a regular
87
- * expression as a parameter.
88
- */
89
- private class PatternRegexMethod extends Method {
90
- PatternRegexMethod ( ) {
91
- this .getDeclaringType ( ) instanceof TypeRegexPattern and
92
- this .hasName ( [ "compile" , "matches" ] )
93
- }
94
- }
95
-
96
- /**
97
- * A methods of the class `org.apache.commons.lang3.RegExUtils` that takes
98
- * a regular expression of type `String` as a parameter.
99
- */
100
- private class ApacheRegExUtilsMethod extends Method {
101
- ApacheRegExUtilsMethod ( ) {
102
- this .getDeclaringType ( ) instanceof TypeApacheRegExUtils and
103
- // only handles String param here because the other param option, Pattern, is already handled by `java.util.regex.Pattern`
104
- this .getParameterType ( 1 ) instanceof TypeString and
105
- this .hasName ( [
106
- "removeAll" , "removeFirst" , "removePattern" , "replaceAll" , "replaceFirst" , "replacePattern"
107
- ] )
108
- }
109
- }
0 commit comments