@@ -9,7 +9,7 @@ private import semmle.code.java.frameworks.apache.Lang
9
9
abstract class Sink extends DataFlow:: ExprNode { }
10
10
11
11
/** A sanitizer for untrusted user input used to construct regular expressions. */
12
- abstract class Sanitizer extends DataFlow:: ExprNode { }
12
+ abstract class RegexInjectionSanitizer extends DataFlow:: ExprNode { }
13
13
14
14
private class RegexInjectionSink extends Sink {
15
15
RegexInjectionSink ( ) {
@@ -26,10 +26,9 @@ private class RegexInjectionSink extends Sink {
26
26
}
27
27
}
28
28
29
- /** A call to a function which escapes regular expression meta-characters. */
30
- private class RegexInjectionSanitizer extends Sanitizer {
31
- RegexInjectionSanitizer ( ) {
32
- // a function whose name suggests that it escapes regular expression meta-characters
29
+ /** A call to a function whose name suggests that it escapes regular expression meta-characters. */
30
+ private class RegexSanitizationCall extends RegexInjectionSanitizer {
31
+ RegexSanitizationCall ( ) {
33
32
exists ( string calleeName , string sanitize , string regexp |
34
33
calleeName = this .asExpr ( ) .( Call ) .getCallee ( ) .getName ( ) and
35
34
sanitize = "(?:escape|saniti[sz]e)" and
@@ -39,19 +38,32 @@ private class RegexInjectionSanitizer extends Sanitizer {
39
38
.regexpMatch ( "(?i)(" + sanitize + ".*" + regexp + ".*)" + "|(" + regexp + ".*" + sanitize +
40
39
".*)" )
41
40
)
42
- or
43
- // a call to the `Pattern.quote` method, which gives metacharacters or escape sequences no special meaning
41
+ }
42
+ }
43
+
44
+ /**
45
+ * A call to the `Pattern.quote` method, which gives metacharacters or escape sequences
46
+ * no special meaning.
47
+ */
48
+ private class PatternQuoteCall extends RegexInjectionSanitizer {
49
+ PatternQuoteCall ( ) {
44
50
exists ( MethodAccess ma , Method m | m = ma .getMethod ( ) |
45
51
ma .getArgument ( 0 ) = this .asExpr ( ) and
46
52
m instanceof PatternQuoteMethod
47
53
)
48
- or
49
- // use of Pattern.LITERAL flag with `Pattern.compile` which gives metacharacters or escape sequences no special meaning
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Use of the `Pattern.LITERAL` flag with `Pattern.compile`, which gives metacharacters
59
+ * or escape sequences no special meaning.
60
+ */
61
+ private class PatternLiteralFlag extends RegexInjectionSanitizer {
62
+ PatternLiteralFlag ( ) {
50
63
exists ( MethodAccess ma , Method m , Field field | m = ma .getMethod ( ) |
51
64
ma .getArgument ( 0 ) = this .asExpr ( ) and
52
65
m instanceof PatternRegexMethod and
53
66
m .hasName ( "compile" ) and
54
- //ma.getArgument(1).toString() = "Pattern.LITERAL" and
55
67
field instanceof PatternLiteral and
56
68
ma .getArgument ( 1 ) = field .getAnAccess ( )
57
69
)
0 commit comments