@@ -3,7 +3,9 @@ overlay[local?]
33module ;
44
55import java
6+ private import semmle.code.java.controlflow.Guards
67private import semmle.code.java.dataflow.DataFlow
8+ private import semmle.code.java.frameworks.Regex
79
810/**
911 * A node whose type is a simple type unlikely to carry taint, such as primitives and their boxed counterparts,
@@ -29,3 +31,44 @@ class SimpleTypeSanitizer extends DataFlow::Node {
2931 this .getType ( ) instanceof EnumType
3032 }
3133}
34+
35+ /**
36+ * Holds if `guard` holds with branch `branch` if `e` matches a regular expression.
37+ *
38+ * This is overapproximate: we do not attempt to reason about the correctness of the regexp.
39+ *
40+ * Use this if you want to define a derived `DataFlow::BarrierGuard` without
41+ * make the type recursive. Otherwise use `RegexpCheckBarrier`.
42+ */
43+ predicate regexpMatchGuardChecks ( Guard guard , Expr e , boolean branch ) {
44+ exists ( Method method , MethodCall mc |
45+ method = mc .getMethod ( ) and
46+ guard = mc and
47+ branch = true
48+ |
49+ // `String.matches` and other `matches` methods.
50+ method .getName ( ) = "matches" and
51+ e = mc .getQualifier ( )
52+ or
53+ method instanceof PatternMatchesMethod and
54+ e = mc .getArgument ( 1 )
55+ or
56+ method instanceof MatcherMatchesMethod and
57+ exists ( MethodCall matcherCall |
58+ matcherCall .getMethod ( ) instanceof PatternMatcherMethod and
59+ e = matcherCall .getArgument ( 0 ) and
60+ DataFlow:: localExprFlow ( matcherCall , mc .getQualifier ( ) )
61+ )
62+ )
63+ }
64+
65+ /**
66+ * A check against a regular expression, considered as a barrier guard.
67+ *
68+ * This is overapproximate: we do not attempt to reason about the correctness of the regexp.
69+ */
70+ class RegexpCheckBarrier extends DataFlow:: Node {
71+ RegexpCheckBarrier ( ) {
72+ this = DataFlow:: BarrierGuard< regexpMatchGuardChecks / 3 > :: getABarrierNode ( )
73+ }
74+ }
0 commit comments