|
| 1 | +/** Provides classes related to security-centered regular expression matching. */ |
| 2 | + |
| 3 | +import java |
| 4 | +private import semmle.code.java.dataflow.ExternalFlow |
| 5 | +private import semmle.code.java.dataflow.FlowSources |
| 6 | +import experimental.semmle.code.java.security.SpringUrlRedirect |
| 7 | +import semmle.code.java.controlflow.Guards |
| 8 | +import semmle.code.java.security.UrlRedirect |
| 9 | +import Regex |
| 10 | + |
| 11 | +/** A string that ends with `.*` not prefixed with `\`. */ |
| 12 | +class PermissiveDotStr extends StringLiteral { |
| 13 | + PermissiveDotStr() { |
| 14 | + exists(string s, int i | this.getValue() = s | |
| 15 | + s.indexOf(".*") = i and |
| 16 | + not s.charAt(i - 1) = "\\" and |
| 17 | + s.length() = i + 2 |
| 18 | + ) |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/** Source model of remote flow source with servlets. */ |
| 23 | +private class GetServletUriSource extends SourceModelCsv { |
| 24 | + override predicate row(string row) { |
| 25 | + row = |
| 26 | + [ |
| 27 | + "javax.servlet.http;HttpServletRequest;false;getPathInfo;();;ReturnValue;uri-path;manual", |
| 28 | + "javax.servlet.http;HttpServletRequest;false;getPathTranslated;();;ReturnValue;uri-path;manual", |
| 29 | + "javax.servlet.http;HttpServletRequest;false;getRequestURI;();;ReturnValue;uri-path;manual", |
| 30 | + "javax.servlet.http;HttpServletRequest;false;getRequestURL;();;ReturnValue;uri-path;manual", |
| 31 | + "javax.servlet.http;HttpServletRequest;false;getServletPath;();;ReturnValue;uri-path;manual" |
| 32 | + ] |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** Sink of servlet dispatcher. */ |
| 37 | +private class UrlDispatchSink extends UrlRedirectSink { |
| 38 | + UrlDispatchSink() { |
| 39 | + exists(MethodAccess ma | |
| 40 | + ma.getMethod() instanceof RequestDispatchMethod and |
| 41 | + this.asExpr() = ma.getQualifier() |
| 42 | + ) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** The `doFilter` method of `javax.servlet.FilterChain`. */ |
| 47 | +private class ServletFilterMethod extends Method { |
| 48 | + ServletFilterMethod() { |
| 49 | + this.getDeclaringType().getASupertype*().hasQualifiedName("javax.servlet", "FilterChain") and |
| 50 | + this.hasName("doFilter") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** Sink of servlet filter. */ |
| 55 | +private class UrlFilterSink extends UrlRedirectSink { |
| 56 | + UrlFilterSink() { |
| 57 | + exists(MethodAccess ma | |
| 58 | + ma.getMethod() instanceof ServletFilterMethod and |
| 59 | + this.asExpr() = ma.getQualifier() |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** A Spring framework annotation indicating remote uri user input. */ |
| 65 | +class SpringUriInputAnnotation extends Annotation { |
| 66 | + SpringUriInputAnnotation() { |
| 67 | + this.getType() |
| 68 | + .hasQualifiedName("org.springframework.web.bind.annotation", |
| 69 | + ["PathVariable", "RequestParam"]) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class SpringUriInputParameterSource extends DataFlow::Node { |
| 74 | + SpringUriInputParameterSource() { |
| 75 | + this.asParameter() = |
| 76 | + any(SpringRequestMappingParameter srmp | |
| 77 | + srmp.getAnAnnotation() instanceof SpringUriInputAnnotation |
| 78 | + ) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * A data flow sink to construct regular expressions. |
| 84 | + */ |
| 85 | +class CompileRegexSink extends DataFlow::ExprNode { |
| 86 | + CompileRegexSink() { |
| 87 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 88 | + ( |
| 89 | + ma.getArgument(0) = this.asExpr() and |
| 90 | + ( |
| 91 | + m instanceof StringMatchMethod // input.matches(regexPattern) |
| 92 | + or |
| 93 | + m instanceof PatternCompileMethod // p = Pattern.compile(regexPattern) |
| 94 | + or |
| 95 | + m instanceof PatternMatchMethod // p = Pattern.matches(regexPattern, input) |
| 96 | + ) |
| 97 | + ) |
| 98 | + ) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * A flow configuration for permissive dot regex. |
| 104 | + */ |
| 105 | +class PermissiveDotRegexConfig extends DataFlow2::Configuration { |
| 106 | + PermissiveDotRegexConfig() { this = "PermissiveDotRegex::PermissiveDotRegexConfig" } |
| 107 | + |
| 108 | + override predicate isSource(DataFlow2::Node src) { src.asExpr() instanceof PermissiveDotStr } |
| 109 | + |
| 110 | + override predicate isSink(DataFlow2::Node sink) { sink instanceof CompileRegexSink } |
| 111 | + |
| 112 | + override predicate isBarrier(DataFlow2::Node node) { |
| 113 | + exists( |
| 114 | + MethodAccess ma, Field f // Pattern.compile(PATTERN, Pattern.DOTALL) |
| 115 | + | |
| 116 | + ma.getMethod() instanceof PatternCompileMethod and |
| 117 | + ma.getArgument(1) = f.getAnAccess() and |
| 118 | + f.hasName("DOTALL") and |
| 119 | + f.getDeclaringType() instanceof Pattern and |
| 120 | + node.asExpr() = ma.getArgument(0) |
| 121 | + ) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * A taint-tracking configuration for untrusted user input used to match regular expressions. |
| 127 | + */ |
| 128 | +class MatchRegexConfiguration extends TaintTracking::Configuration { |
| 129 | + MatchRegexConfiguration() { this = "PermissiveDotRegex::MatchRegexConfiguration" } |
| 130 | + |
| 131 | + override predicate isSource(DataFlow::Node source) { |
| 132 | + sourceNode(source, "uri-path") or // Servlet uri source |
| 133 | + source instanceof SpringUriInputParameterSource // Spring uri source |
| 134 | + } |
| 135 | + |
| 136 | + override predicate isSink(DataFlow::Node sink) { |
| 137 | + sink instanceof MatchRegexSink and |
| 138 | + exists( |
| 139 | + Guard guard, Expr se, Expr ce // used in a condition to control url redirect, which is a typical security enforcement |
| 140 | + | |
| 141 | + ( |
| 142 | + sink.asExpr() = ce.(MethodAccess).getQualifier() or |
| 143 | + sink.asExpr() = ce.(MethodAccess).getAnArgument() or |
| 144 | + sink.asExpr() = ce |
| 145 | + ) and |
| 146 | + ( |
| 147 | + DataFlow::localExprFlow(ce, guard.(MethodAccess).getQualifier()) or |
| 148 | + DataFlow::localExprFlow(ce, guard.(MethodAccess).getAnArgument()) |
| 149 | + ) and |
| 150 | + ( |
| 151 | + DataFlow::exprNode(se) instanceof UrlRedirectSink or |
| 152 | + DataFlow::exprNode(se) instanceof SpringUrlRedirectSink |
| 153 | + ) and |
| 154 | + guard.controls(se.getBasicBlock(), true) |
| 155 | + ) and |
| 156 | + exists(MethodAccess ma | any(PermissiveDotRegexConfig conf2).hasFlowToExpr(ma.getArgument(0)) | |
| 157 | + // input.matches(regexPattern) |
| 158 | + ma.getMethod() instanceof StringMatchMethod and |
| 159 | + ma.getQualifier() = sink.asExpr() |
| 160 | + or |
| 161 | + // p = Pattern.compile(regexPattern); p.matcher(input) |
| 162 | + ma.getMethod() instanceof PatternCompileMethod and |
| 163 | + exists(MethodAccess pma | |
| 164 | + pma.getMethod() instanceof PatternMatcherMethod and |
| 165 | + sink.asExpr() = pma.getArgument(0) and |
| 166 | + DataFlow::localExprFlow(ma, pma.getQualifier()) |
| 167 | + ) |
| 168 | + or |
| 169 | + // p = Pattern.matches(regexPattern, input) |
| 170 | + ma.getMethod() instanceof PatternMatchMethod and |
| 171 | + sink.asExpr() = ma.getArgument(1) |
| 172 | + ) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +abstract class MatchRegexSink extends DataFlow::ExprNode { } |
| 177 | + |
| 178 | +/** |
| 179 | + * A data flow sink to string match regular expressions. |
| 180 | + */ |
| 181 | +class StringMatchRegexSink extends MatchRegexSink { |
| 182 | + StringMatchRegexSink() { |
| 183 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 184 | + ( |
| 185 | + m instanceof StringMatchMethod and |
| 186 | + ma.getQualifier() = this.asExpr() |
| 187 | + ) |
| 188 | + ) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +/** |
| 193 | + * A data flow sink to `pattern.matches` regular expressions. |
| 194 | + */ |
| 195 | +class PatternMatchRegexSink extends MatchRegexSink { |
| 196 | + PatternMatchRegexSink() { |
| 197 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 198 | + ( |
| 199 | + m instanceof PatternMatchMethod and |
| 200 | + ma.getArgument(1) = this.asExpr() |
| 201 | + ) |
| 202 | + ) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +/** |
| 207 | + * A data flow sink to `pattern.matcher` match regular expressions. |
| 208 | + */ |
| 209 | +class PatternMatcherRegexSink extends MatchRegexSink { |
| 210 | + PatternMatcherRegexSink() { |
| 211 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 212 | + ( |
| 213 | + m instanceof PatternMatcherMethod and |
| 214 | + ma.getArgument(0) = this.asExpr() |
| 215 | + ) |
| 216 | + ) |
| 217 | + } |
| 218 | +} |
0 commit comments