@@ -9,7 +9,7 @@ private import semmle.code.java.frameworks.apache.Lang
99abstract class Sink extends DataFlow:: ExprNode { }
1010
1111/** 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 { }
1313
1414private class RegexInjectionSink extends Sink {
1515 RegexInjectionSink ( ) {
@@ -26,10 +26,9 @@ private class RegexInjectionSink extends Sink {
2626 }
2727}
2828
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 ( ) {
3332 exists ( string calleeName , string sanitize , string regexp |
3433 calleeName = this .asExpr ( ) .( Call ) .getCallee ( ) .getName ( ) and
3534 sanitize = "(?:escape|saniti[sz]e)" and
@@ -39,19 +38,32 @@ private class RegexInjectionSanitizer extends Sanitizer {
3938 .regexpMatch ( "(?i)(" + sanitize + ".*" + regexp + ".*)" + "|(" + regexp + ".*" + sanitize +
4039 ".*)" )
4140 )
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 ( ) {
4450 exists ( MethodAccess ma , Method m | m = ma .getMethod ( ) |
4551 ma .getArgument ( 0 ) = this .asExpr ( ) and
4652 m instanceof PatternQuoteMethod
4753 )
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 ( ) {
5063 exists ( MethodAccess ma , Method m , Field field | m = ma .getMethod ( ) |
5164 ma .getArgument ( 0 ) = this .asExpr ( ) and
5265 m instanceof PatternRegexMethod and
5366 m .hasName ( "compile" ) and
54- //ma.getArgument(1).toString() = "Pattern.LITERAL" and
5567 field instanceof PatternLiteral and
5668 ma .getArgument ( 1 ) = field .getAnAccess ( )
5769 )
0 commit comments