File tree 5 files changed +62
-0
lines changed
Filter options
5 files changed +62
-0
lines changed
Original file line number Diff line number Diff line change
1
+ private import java
2
+ private import semmle.code.java.dataflow.DataFlow
3
+ private import codeql.util.Unit
4
+
5
+ /**
6
+ * An extension point to allow a query to detect only the regular expressions
7
+ * it needs in diff-informed incremental mode. The data-flow analysis that's
8
+ * modified by this class has its sources as (certain) string literals and its
9
+ * sinks as regular-expression matches.
10
+ */
11
+ class RegexDiffInformedConfig instanceof Unit {
12
+ /**
13
+ * Holds if discovery of regular expressions should be diff-informed, which
14
+ * is possible when there cannot be any elements selected by the query in the
15
+ * diff range except the regular expressions and (locations derived from) the
16
+ * places where they are matched against.
17
+ */
18
+ abstract predicate observeDiffInformedIncrementalMode ( ) ;
19
+
20
+ /**
21
+ * Gets a location of a regex match that will be part of the query results.
22
+ * If the query does not select the match locations, this predicate can be
23
+ * `none()` for performance.
24
+ */
25
+ abstract Location getASelectedSinkLocation ( DataFlow:: Node sink ) ;
26
+
27
+ string toString ( ) { result = "RegexDiffInformedConfig" }
28
+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import java
6
6
import semmle.code.java.dataflow.ExternalFlow
7
7
private import semmle.code.java.dataflow.DataFlow
8
8
private import semmle.code.java.security.SecurityTests
9
+ private import RegexDiffInformed
9
10
10
11
private class ExploitableStringLiteral extends StringLiteral {
11
12
ExploitableStringLiteral ( ) { this .getValue ( ) .matches ( [ "%+%" , "%*%" , "%{%}%" ] ) }
@@ -157,6 +158,14 @@ private module RegexFlowConfig implements DataFlow::ConfigSig {
157
158
}
158
159
159
160
int fieldFlowBranchLimit ( ) { result = 1 }
161
+
162
+ predicate observeDiffInformedIncrementalMode ( ) {
163
+ exists ( RegexDiffInformedConfig c | c .observeDiffInformedIncrementalMode ( ) )
164
+ }
165
+
166
+ Location getASelectedSinkLocation ( DataFlow:: Node sink ) {
167
+ exists ( RegexDiffInformedConfig c | result = c .getASelectedSinkLocation ( sink ) )
168
+ }
160
169
}
161
170
162
171
private module RegexFlow = DataFlow:: Global< RegexFlowConfig > ;
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ private import semmle.code.java.regex.RegexTreeView::RegexTreeView as TreeView
4
4
import codeql.regex.nfa.SuperlinearBackTracking:: Make< TreeView > as SuperlinearBackTracking
5
5
import semmle.code.java.dataflow.DataFlow
6
6
import semmle.code.java.regex.RegexFlowConfigs
7
+ import semmle.code.java.regex.RegexDiffInformed
7
8
import semmle.code.java.dataflow.FlowSources
8
9
private import semmle.code.java.security.Sanitizers
9
10
@@ -33,6 +34,14 @@ private class LengthRestrictedMethod extends Method {
33
34
}
34
35
}
35
36
37
+ class PolynomialRedDosDiffInformed extends RegexDiffInformedConfig {
38
+ override predicate observeDiffInformedIncrementalMode ( ) {
39
+ not PolynomialRedosFlow:: hasSourceInDiffRange ( )
40
+ }
41
+
42
+ override Location getASelectedSinkLocation ( DataFlow:: Node sink ) { result = sink .getLocation ( ) }
43
+ }
44
+
36
45
/** A configuration for Polynomial ReDoS queries. */
37
46
module PolynomialRedosConfig implements DataFlow:: ConfigSig {
38
47
predicate isSource ( DataFlow:: Node src ) { src instanceof ActiveThreatModelSource }
Original file line number Diff line number Diff line change 12
12
* external/cwe/cwe-020
13
13
*/
14
14
15
+ import semmle.code.java.regex.RegexDiffInformed
16
+ import semmle.code.java.dataflow.DataFlow
15
17
private import semmle.code.java.regex.RegexTreeView:: RegexTreeView as TreeView
16
18
import codeql.regex.OverlyLargeRangeQuery:: Make< TreeView >
17
19
20
+ class OverlyLargeRangeDiffInformed extends RegexDiffInformedConfig {
21
+ override predicate observeDiffInformedIncrementalMode ( ) { any ( ) }
22
+
23
+ override Location getASelectedSinkLocation ( DataFlow:: Node sink ) { none ( ) }
24
+ }
25
+
18
26
TreeView:: RegExpCharacterClass potentialMisparsedCharClass ( ) {
19
27
// nested char classes are currently misparsed
20
28
result .getAChild ( ) .( TreeView:: RegExpNormalChar ) .getValue ( ) = "["
Original file line number Diff line number Diff line change 14
14
* external/cwe/cwe-400
15
15
*/
16
16
17
+ import semmle.code.java.regex.RegexDiffInformed
18
+ import semmle.code.java.dataflow.DataFlow
17
19
private import semmle.code.java.regex.RegexTreeView:: RegexTreeView as TreeView
18
20
import codeql.regex.nfa.ExponentialBackTracking:: Make< TreeView > as ExponentialBackTracking
19
21
22
+ class ReDoSDiffInformed extends RegexDiffInformedConfig {
23
+ override predicate observeDiffInformedIncrementalMode ( ) { any ( ) }
24
+
25
+ override Location getASelectedSinkLocation ( DataFlow:: Node sink ) { none ( ) }
26
+ }
27
+
20
28
from TreeView:: RegExpTerm t , string pump , ExponentialBackTracking:: State s , string prefixMsg
21
29
where
22
30
ExponentialBackTracking:: hasReDoSResult ( t , pump , s , prefixMsg ) and
You can’t perform that action at this time.
0 commit comments