Skip to content

Commit 037a05c

Browse files
Jami CogswellJami Cogswell
authored andcommitted
add classes for Pattern, Matcher, and RegExUtils
1 parent 6ba7449 commit 037a05c

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,46 @@ import java
22
import semmle.code.java.dataflow.FlowSources
33
import semmle.code.java.dataflow.TaintTracking
44

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
522
/**
623
* A data flow sink for untrusted user input used to construct regular expressions.
724
*/
825
class RegexSink extends DataFlow::ExprNode {
926
RegexSink() {
1027
exists(MethodAccess ma, Method m | m = ma.getMethod() |
28+
ma.getArgument(0) = this.asExpr() and
1129
(
1230
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"])
2732
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+
])
3945
)
4046
)
4147
}
@@ -67,7 +73,7 @@ class RegExpSanitizationCall extends Sanitizer {
6773
// adds Pattern.quote() as a sanitizer
6874
// see https://rules.sonarsource.com/java/RSPEC-2631 and https://sensei.securecodewarrior.com/recipes/scw:java:regex-injection
6975
exists(MethodAccess ma, Method m | m = ma.getMethod() |
70-
m.getDeclaringType().hasQualifiedName("java.util.regex", "Pattern") and
76+
m.getDeclaringType() instanceof RegexPattern and
7177
(
7278
ma.getArgument(0) = this.asExpr() and
7379
m.hasName("quote")

0 commit comments

Comments
 (0)