Skip to content

Commit f6f26fe

Browse files
Jami CogswellJami Cogswell
authored andcommitted
refactor code; add change note
1 parent 037a05c commit f6f26fe

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

java/ql/lib/semmle/code/java/regex/RegexFlowConfigs.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ private predicate regexSinkKindInfo(string kind, boolean full, int strArg) {
3939
}
4040

4141
/** A sink that is relevant for regex flow. */
42-
private class RegexFlowSink extends DataFlow::Node {
42+
class RegexFlowSink extends DataFlow::Node {
43+
// ! switch back to private!!! - just testing if this sink is useful for regex injection as well
4344
boolean full;
4445
int strArg;
4546

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java
22
import semmle.code.java.dataflow.FlowSources
33
import semmle.code.java.dataflow.TaintTracking
4+
import semmle.code.java.regex.RegexFlowConfigs
45

56
/** The Java class `java.util.regex.Pattern`. */
67
private class RegexPattern extends RefType {
@@ -17,7 +18,7 @@ private class ApacheRegExUtils extends RefType {
1718
ApacheRegExUtils() { this.hasQualifiedName("java.util.regex", "Matcher") }
1819
}
1920

20-
// TODO: Are there already classes for any of below(above) in a pre-existing regex library?
21+
// TODO: Look for above in pre-existing regex libraries again.
2122
// TODO: look into further: Pattern.matcher, .pattern() and .toString() as taint steps, .split and .splitAsStream
2223
/**
2324
* A data flow sink for untrusted user input used to construct regular expressions.
@@ -37,7 +38,7 @@ class RegexSink extends DataFlow::ExprNode {
3738
m.getDeclaringType() instanceof ApacheRegExUtils and
3839
(
3940
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.getParameterType(1) instanceof TypeString and // only does String here because other option is Pattern, but that's already handled by `java.util.regex.Pattern` above
4142
m.hasName([
4243
"removeAll", "removeFirst", "removePattern", "replaceAll", "replaceFirst",
4344
"replacePattern"
@@ -92,5 +93,8 @@ class RegexInjectionConfiguration extends TaintTracking::Configuration {
9293

9394
override predicate isSink(DataFlow::Node sink) { sink instanceof RegexSink }
9495

96+
// ! testing below RegexFlowSink from RegexFlowConfigs.qll
97+
// ! extra results from jfinal with this... look into further...
98+
// override predicate isSink(DataFlow::Node sink) { sink instanceof RegexFlowSink }
9599
override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer }
96100
}

java/ql/src/Security/CWE/CWE-730/RegexInjection.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/**
22
* @name Regular expression injection
3-
* @description User input should not be used in regular expressions without first being sanitized,
3+
* @description User input should not be used in regular expressions without first being escaped,
44
* otherwise a malicious user may be able to provide a regex that could require
55
* exponential time on certain inputs.
66
* @kind path-problem
77
* @problem.severity error
8+
* @security-severity 7.5
89
* @precision high
910
* @id java/regex-injection
1011
* @tags security
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: newQuery
3+
---
4+
* Added a new query, `java/regex-injection`, to detect unescaped user input used in regular expressions.

0 commit comments

Comments
 (0)