|
| 1 | +/** |
| 2 | + * @name Potentially unguarded protocol handler invocation |
| 3 | + * @id tob/java/unguarded-protocol-handler |
| 4 | + * @description Detects calls to URL protocol handlers with untrusted input that may not be properly validated for dangerous protocols |
| 5 | + * @kind path-problem |
| 6 | + * @tags security |
| 7 | + * external/cwe/cwe-939 |
| 8 | + * @precision medium |
| 9 | + * @problem.severity warning |
| 10 | + * @security-severity 6.5 |
| 11 | + * @group security |
| 12 | + */ |
| 13 | + |
| 14 | +import java |
| 15 | +import semmle.code.java.dataflow.TaintTracking |
| 16 | +import semmle.code.java.dataflow.FlowSources |
| 17 | +import semmle.code.java.controlflow.Guards |
| 18 | + |
| 19 | +/** |
| 20 | + * A call to Desktop.browse() method for handling |
| 21 | + * TODO(alan): also support legacy/generic cases like invoking "rundll32 url.dll,FileProtocolHandler" |
| 22 | + */ |
| 23 | +class DesktopBrowseCall extends MethodCall { |
| 24 | + DesktopBrowseCall() { |
| 25 | + this.getMethod().hasName("browse") and |
| 26 | + this.getMethod().getDeclaringType().hasQualifiedName("java.awt", "Desktop") |
| 27 | + } |
| 28 | + |
| 29 | + Expr getUrlArgument() { result = this.getArgument(0) } |
| 30 | + |
| 31 | + string getDescription() { result = "Desktop.browse()" } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * A guard that checks the URL scheme/protocol signifying sanitization before launch |
| 36 | + */ |
| 37 | +predicate isUrlSchemeCheck(Guard g, Expr e, boolean branch) { |
| 38 | + exists(MethodCall mc | |
| 39 | + mc = g and |
| 40 | + e = mc.getQualifier*() and |
| 41 | + branch = true and |
| 42 | + ( |
| 43 | + // getScheme() or getProtocol() check |
| 44 | + mc.getMethod().hasName(["equals", "equalsIgnoreCase", "startsWith", "matches"]) and |
| 45 | + exists(MethodCall getScheme | |
| 46 | + getScheme.getParent*() = mc and |
| 47 | + getScheme.getMethod().hasName(["getScheme", "getProtocol"]) |
| 48 | + ) |
| 49 | + or |
| 50 | + // URL string contains check like: url.contains("http://") or url.startsWith("https://") |
| 51 | + mc.getMethod().hasName(["contains", "startsWith", "matches", "indexOf"]) and |
| 52 | + exists(StringLiteral sl | sl = mc.getAnArgument() | sl.getValue().regexpMatch(".*://.*")) |
| 53 | + or |
| 54 | + // Pattern matching on the string representation |
| 55 | + mc.getMethod().hasName(["matches", "find"]) and |
| 56 | + mc.getQualifier().getType().(RefType).hasQualifiedName("java.util.regex", "Matcher") |
| 57 | + ) |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Configuration for tracking untrusted data to protocol handler invocations |
| 63 | + */ |
| 64 | +module PotentiallyUnguardedProtocolHandlerConfig implements DataFlow::ConfigSig { |
| 65 | + predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } |
| 66 | + |
| 67 | + predicate isSink(DataFlow::Node sink) { |
| 68 | + exists(DesktopBrowseCall call | sink.asExpr() = call.getUrlArgument()) |
| 69 | + } |
| 70 | + |
| 71 | + predicate isBarrier(DataFlow::Node node) { |
| 72 | + // Consider sanitized if there's a guard checking the scheme |
| 73 | + node = DataFlow::BarrierGuard<isUrlSchemeCheck/3>::getABarrierNode() |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +module PotentiallyUnguardedProtocolHandlerFlow = |
| 78 | + TaintTracking::Global<PotentiallyUnguardedProtocolHandlerConfig>; |
| 79 | + |
| 80 | +import PotentiallyUnguardedProtocolHandlerFlow::PathGraph |
| 81 | + |
| 82 | +from |
| 83 | + PotentiallyUnguardedProtocolHandlerFlow::PathNode source, |
| 84 | + PotentiallyUnguardedProtocolHandlerFlow::PathNode sink, DesktopBrowseCall call |
| 85 | +where |
| 86 | + PotentiallyUnguardedProtocolHandlerFlow::flowPath(source, sink) and |
| 87 | + sink.getNode().asExpr() = call.getUrlArgument() |
| 88 | +select call, source, sink, |
| 89 | + call.getDescription() + |
| 90 | + " is called with untrusted input from $@ without proper URL scheme validation.", |
| 91 | + source.getNode(), "this source" |
0 commit comments