Skip to content

Commit 5b089bb

Browse files
Jami CogswellJami Cogswell
authored andcommitted
split sanitizer into three
1 parent 91491d9 commit 5b089bb

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

java/ql/lib/semmle/code/java/security/RegexInjection.qll

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private import semmle.code.java.frameworks.apache.Lang
99
abstract 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

1414
private 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
)

java/ql/lib/semmle/code/java/security/RegexInjectionQuery.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ class RegexInjectionConfiguration extends TaintTracking::Configuration {
1313

1414
override predicate isSink(DataFlow::Node sink) { sink instanceof Sink }
1515

16-
override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer }
16+
override predicate isSanitizer(DataFlow::Node node) { node instanceof RegexInjectionSanitizer }
1717
}

java/ql/test/query-tests/security/CWE-730/RegexInjectionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ public boolean apache7(javax.servlet.http.HttpServletRequest request) {
183183
return RegExUtils.replacePattern(input, pattern, "").length() > 0; // $ hasRegexInjection
184184
}
185185

186-
// test `Pattern.quote` as safe
186+
// test `Pattern.quote` sanitizer
187187
public boolean quoteTest(javax.servlet.http.HttpServletRequest request) {
188188
String regex = request.getParameter("regex");
189189
String input = request.getParameter("input");
190190

191191
return input.matches(Pattern.quote(regex)); // Safe
192192
}
193193

194-
// test `Pattern.LITERAL` as safe
194+
// test `Pattern.LITERAL` sanitizer
195195
public boolean literalTest(javax.servlet.http.HttpServletRequest request) {
196196
String pattern = request.getParameter("pattern");
197197
String input = request.getParameter("input");

0 commit comments

Comments
 (0)