|
| 1 | +/** |
| 2 | + * @name URL matched by permissive `.` in the regular expression |
| 3 | + * @description URL validated with permissive `.` in regex are possibly vulnerable |
| 4 | + * to an authorization bypass. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity warning |
| 7 | + * @precision high |
| 8 | + * @id java/permissive-dot-regex |
| 9 | + * @tags security |
| 10 | + * external/cwe-625 |
| 11 | + * external/cwe-863 |
| 12 | + */ |
| 13 | + |
| 14 | +import java |
| 15 | +import semmle.code.java.dataflow.ExternalFlow |
| 16 | +import semmle.code.java.dataflow.FlowSources |
| 17 | +import DataFlow::PathGraph |
| 18 | +import Regex |
| 19 | + |
| 20 | +/** |
| 21 | + * `.` without a `\` prefix, which is likely not a character literal in regex |
| 22 | + */ |
| 23 | +class PermissiveDotStr extends StringLiteral { |
| 24 | + PermissiveDotStr() { |
| 25 | + // Find `.` in a string that is not prefixed with `\` |
| 26 | + exists(string s, int i | this.getValue() = s | |
| 27 | + s.indexOf(".") = i and |
| 28 | + not s.charAt(i - 1) = "\\" |
| 29 | + ) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Permissive `.` in a regular expression. |
| 35 | + */ |
| 36 | +class PermissiveDotEx extends Expr { |
| 37 | + PermissiveDotEx() { this instanceof PermissiveDotStr } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * A data flow sink to construct regular expressions. |
| 42 | + */ |
| 43 | +class CompileRegexSink extends DataFlow::ExprNode { |
| 44 | + CompileRegexSink() { |
| 45 | + exists(MethodAccess ma, Method m | m = ma.getMethod() | |
| 46 | + ( |
| 47 | + ma.getArgument(0) = this.asExpr() and |
| 48 | + ( |
| 49 | + m instanceof StringMatchMethod // input.matches(regexPattern) |
| 50 | + or |
| 51 | + m instanceof PatternCompileMethod // p = Pattern.compile(regexPattern) |
| 52 | + or |
| 53 | + m instanceof PatternMatchMethod // p = Pattern.matches(regexPattern, input) |
| 54 | + ) |
| 55 | + ) |
| 56 | + ) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * A flow configuration for permissive dot regex. |
| 62 | + */ |
| 63 | +class PermissiveDotRegexConfig extends DataFlow::Configuration { |
| 64 | + PermissiveDotRegexConfig() { this = "PermissiveDotRegex::PermissiveDotRegexConfig" } |
| 65 | + |
| 66 | + override predicate isSource(DataFlow::Node src) { src.asExpr() instanceof PermissiveDotEx } |
| 67 | + |
| 68 | + override predicate isSink(DataFlow::Node sink) { sink instanceof CompileRegexSink } |
| 69 | + |
| 70 | + override predicate isBarrier(DataFlow::Node node) { |
| 71 | + exists( |
| 72 | + MethodAccess ma, Field f // Pattern.compile(PATTERN, Pattern.DOTALL) |
| 73 | + | |
| 74 | + ma.getMethod() instanceof PatternCompileMethod and |
| 75 | + ma.getArgument(1) = f.getAnAccess() and |
| 76 | + f.hasName("DOTALL") and |
| 77 | + f.getDeclaringType() instanceof Pattern and |
| 78 | + node.asExpr() = ma.getArgument(0) |
| 79 | + ) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * A taint-tracking configuration for untrusted user input used to match regular expressions. |
| 85 | + */ |
| 86 | +class MatchRegexConfiguration extends TaintTracking::Configuration { |
| 87 | + MatchRegexConfiguration() { this = "PermissiveDotRegex::MatchRegexConfiguration" } |
| 88 | + |
| 89 | + override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 90 | + |
| 91 | + override predicate isSink(DataFlow::Node sink) { sink instanceof MatchRegexSink } |
| 92 | +} |
| 93 | + |
| 94 | +from |
| 95 | + DataFlow::PathNode source, DataFlow::PathNode sink, MatchRegexConfiguration conf, |
| 96 | + DataFlow::PathNode source2, DataFlow::PathNode sink2, PermissiveDotRegexConfig conf2 |
| 97 | +where |
| 98 | + conf.hasFlowPath(source, sink) and |
| 99 | + conf2.hasFlowPath(source2, sink2) and |
| 100 | + exists(MethodAccess ma | ma.getArgument(0) = sink2.getNode().asExpr() | |
| 101 | + // input.matches(regexPattern) |
| 102 | + ma.getMethod() instanceof StringMatchMethod and |
| 103 | + ma.getQualifier() = sink.getNode().asExpr() |
| 104 | + or |
| 105 | + // p = Pattern.compile(regexPattern); p.matcher(input) |
| 106 | + ma.getMethod() instanceof PatternCompileMethod and |
| 107 | + exists(MethodAccess pma | |
| 108 | + pma.getMethod() instanceof PatternMatcherMethod and |
| 109 | + sink.getNode().asExpr() = pma.getArgument(0) and |
| 110 | + DataFlow::localExprFlow(ma, pma.getQualifier()) |
| 111 | + ) |
| 112 | + or |
| 113 | + // p = Pattern.matches(regexPattern, input) |
| 114 | + ma.getMethod() instanceof PatternMatchMethod and |
| 115 | + sink.getNode().asExpr() = ma.getArgument(1) |
| 116 | + ) |
| 117 | +select sink.getNode(), source, sink, "Potentially authentication bypass due to $@.", |
| 118 | + source.getNode(), "user-provided value" |
0 commit comments