|
| 1 | +/** |
| 2 | + * @name Unsafe URI Check |
| 3 | + * @description Checking a URL against an allow/block list in Java may be unsafe. |
| 4 | + * @kind path-problem |
| 5 | + * @problem.severity error |
| 6 | + * @precision high |
| 7 | + * @id githubsecuritylab/unsafe-uri-check |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-22 |
| 10 | + */ |
| 11 | + |
| 12 | +import java |
| 13 | +import semmle.code.java.dataflow.FlowSources |
| 14 | +import UnsafeURICheckFlow::PathGraph |
| 15 | + |
| 16 | +// Reference: https://mail-archives.apache.org/mod_mbox/ambari-user/202102.mbox/%3CCAEJYuxEQZ_aPwJdAaSxPu-Dva%3Dhc7zZUx3-pzBORbd23g%2BGH1A%40mail.gmail.com%3E |
| 17 | +class ServletFilterInterface extends Interface { |
| 18 | + ServletFilterInterface() { this.hasQualifiedName("javax.servlet", "Filter") } |
| 19 | +} |
| 20 | + |
| 21 | +class ServletRequestInterface extends Interface { |
| 22 | + ServletRequestInterface() { this.hasQualifiedName("javax.servlet.http", "HttpServletRequest") } |
| 23 | +} |
| 24 | + |
| 25 | +class GetRequestURIMethodAccess extends MethodAccess { |
| 26 | + GetRequestURIMethodAccess() { |
| 27 | + this.getMethod().getName() = "getRequestURI" and |
| 28 | + this.getMethod().getDeclaringType() instanceof ServletRequestInterface |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +private module UnsafeURICheckConfig implements DataFlow::ConfigSig { |
| 33 | + predicate isSource(DataFlow::Node source) { |
| 34 | + exists(GetRequestURIMethodAccess ma | |
| 35 | + ma.getEnclosingCallable().getDeclaringType().getASourceSupertype*() instanceof |
| 36 | + ServletFilterInterface and |
| 37 | + source.asExpr() = ma |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + predicate isSink(DataFlow::Node sink) { |
| 42 | + exists(MethodAccess ma | |
| 43 | + // java.util.regex.Pattern.matcher("aaaaab"); |
| 44 | + ma.getMethod().getName() = "matcher" and |
| 45 | + ma.getMethod().getDeclaringType().hasQualifiedName("java.util.regex", "Pattern") and |
| 46 | + sink.asExpr() = ma.getArgument(0) |
| 47 | + or |
| 48 | + // java.util.regex.Pattern.matches("a*b", "aaaaab"); |
| 49 | + ma.getMethod().getName() = "matches" and |
| 50 | + ma.getMethod().getDeclaringType().hasQualifiedName("java.util.regex", "Pattern") and |
| 51 | + sink.asExpr() = ma.getArgument(1) |
| 52 | + or |
| 53 | + ma.getMethod().getName() = "matches" and |
| 54 | + ma.getMethod().getDeclaringType() instanceof TypeString and |
| 55 | + sink.asExpr() = ma.getQualifier() |
| 56 | + or |
| 57 | + ma.getMethod().getName() = ["startsWith", "endsWith"] and |
| 58 | + ma.getMethod().getDeclaringType() instanceof TypeString and |
| 59 | + not ma.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "/" and |
| 60 | + sink.asExpr() = ma.getQualifier() |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +module UnsafeURICheckFlow = TaintTracking::Global<UnsafeURICheckConfig>; |
| 66 | + |
| 67 | +from UnsafeURICheckFlow::PathNode source, UnsafeURICheckFlow::PathNode sink |
| 68 | +where UnsafeURICheckFlow::flowPath(source, sink) |
| 69 | +select sink.getNode(), source, sink, "Unsafe URI check" |
0 commit comments