|
| 1 | +/** |
| 2 | + * @name Unsafe resource fetching in Android webview |
| 3 | + * @id java/android/unsafe-android-webview-fetch |
| 4 | + * @description JavaScript rendered inside WebViews can access any protected application file and web resource from any origin |
| 5 | + * @kind path-problem |
| 6 | + * @tags security |
| 7 | + * external/cwe/cwe-749 |
| 8 | + * external/cwe/cwe-079 |
| 9 | + */ |
| 10 | + |
| 11 | +import java |
| 12 | +import semmle.code.java.frameworks.android.Intent |
| 13 | +import semmle.code.java.frameworks.android.WebView |
| 14 | +import semmle.code.java.dataflow.FlowSources |
| 15 | +import DataFlow::PathGraph |
| 16 | + |
| 17 | +/** |
| 18 | + * Methods allowing any-local-file and cross-origin access in the WebSettings class |
| 19 | + */ |
| 20 | +class CrossOriginAccessMethod extends Method { |
| 21 | + CrossOriginAccessMethod() { |
| 22 | + this.getDeclaringType() instanceof TypeWebSettings and |
| 23 | + ( |
| 24 | + this.hasName("setAllowUniversalAccessFromFileURLs") or |
| 25 | + this.hasName("setAllowFileAccessFromFileURLs") |
| 26 | + ) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * `setJavaScriptEnabled` method for the webview |
| 32 | + */ |
| 33 | +class AllowJavaScriptMethod extends Method { |
| 34 | + AllowJavaScriptMethod() { |
| 35 | + this.getDeclaringType() instanceof TypeWebSettings and |
| 36 | + this.hasName("setJavaScriptEnabled") |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Holds if a call to `v.setJavaScriptEnabled(true)` exists |
| 42 | + */ |
| 43 | +predicate isJSEnabled(Variable v) { |
| 44 | + exists(MethodAccess jsa | |
| 45 | + v.getAnAccess() = jsa.getQualifier() and |
| 46 | + jsa.getMethod() instanceof AllowJavaScriptMethod and |
| 47 | + jsa.getArgument(0).(BooleanLiteral).getBooleanValue() = true |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Fetch URL method call on the `android.webkit.WebView` object |
| 53 | + */ |
| 54 | +class FetchResourceMethodAccess extends MethodAccess { |
| 55 | + FetchResourceMethodAccess() { |
| 56 | + this.getMethod().getDeclaringType() instanceof TypeWebView and |
| 57 | + this.getMethod().hasName(["loadUrl", "postUrl"]) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Method access to external inputs of `android.content.Intent` object |
| 63 | + */ |
| 64 | +class IntentGetExtraMethodAccess extends MethodAccess { |
| 65 | + IntentGetExtraMethodAccess() { |
| 66 | + this.getMethod().getName().regexpMatch("get\\w+Extra") and |
| 67 | + this.getMethod().getDeclaringType() instanceof TypeIntent |
| 68 | + or |
| 69 | + this.getMethod().getName().regexpMatch("get\\w+") and |
| 70 | + this.getQualifier().(MethodAccess).getMethod().hasName("getExtras") and |
| 71 | + this.getQualifier().(MethodAccess).getMethod().getDeclaringType() instanceof TypeIntent |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Source of fetching URLs from intent extras |
| 77 | + */ |
| 78 | +class UntrustedResourceSource extends DataFlow::ExprNode { |
| 79 | + UntrustedResourceSource() { this.asExpr() instanceof IntentGetExtraMethodAccess } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Holds if `ma` loads URL `sink` |
| 84 | + */ |
| 85 | +predicate fetchResource(FetchResourceMethodAccess ma, Expr sink) { sink = ma.getArgument(0) } |
| 86 | + |
| 87 | +/** |
| 88 | + * A URL argument to a `loadUrl` or `postUrl` call, considered as a sink. |
| 89 | + */ |
| 90 | +class UrlResourceSink extends DataFlow::ExprNode { |
| 91 | + UrlResourceSink() { fetchResource(_, this.getExpr()) } |
| 92 | + |
| 93 | + /** Gets the fetch method that fetches this sink URL. */ |
| 94 | + FetchResourceMethodAccess getMethodAccess() { fetchResource(result, this.getExpr()) } |
| 95 | + |
| 96 | + /** |
| 97 | + * Holds if cross-origin access is enabled for this resource fetch. |
| 98 | + * |
| 99 | + * Specifically this looks for code like |
| 100 | + * `webView.getSettings().setAllow[File|Universal]AccessFromFileURLs(true);` |
| 101 | + */ |
| 102 | + predicate crossOriginAccessEnabled() { |
| 103 | + exists(MethodAccess ma, MethodAccess getSettingsMa | |
| 104 | + ma.getMethod() instanceof CrossOriginAccessMethod and |
| 105 | + ma.getArgument(0).(BooleanLiteral).getBooleanValue() = true and |
| 106 | + ma.getQualifier().(VarAccess).getVariable().getAnAssignedValue() = getSettingsMa and |
| 107 | + getSettingsMa.getMethod() instanceof WebViewGetSettingsMethod and |
| 108 | + getSettingsMa.getQualifier().(VarAccess).getVariable().getAnAccess() = |
| 109 | + getMethodAccess().getQualifier() |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns a description of this vulnerability, assuming Javascript is enabled and |
| 115 | + * the fetched URL is attacker-controlled. |
| 116 | + */ |
| 117 | + string getSinkType() { |
| 118 | + if crossOriginAccessEnabled() |
| 119 | + then result = "user input vulnerable to cross-origin and sensitive resource disclosure attacks" |
| 120 | + else result = "user input vulnerable to XSS attacks" |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * Taint configuration tracking flow from untrusted inputs to `loadUrl` or `postUrl` calls. |
| 126 | + */ |
| 127 | +class FetchUntrustedResourceConfiguration extends TaintTracking::Configuration { |
| 128 | + FetchUntrustedResourceConfiguration() { this = "FetchUntrustedResourceConfiguration" } |
| 129 | + |
| 130 | + override predicate isSource(DataFlow::Node source) { source instanceof UntrustedResourceSource } |
| 131 | + |
| 132 | + override predicate isSink(DataFlow::Node sink) { |
| 133 | + sink instanceof UrlResourceSink and |
| 134 | + exists(VarAccess webviewVa, MethodAccess getSettingsMa, Variable v | |
| 135 | + sink.(UrlResourceSink).getMethodAccess().getQualifier() = webviewVa and |
| 136 | + getSettingsMa.getMethod() instanceof WebViewGetSettingsMethod and |
| 137 | + webviewVa.getVariable().getAnAccess() = getSettingsMa.getQualifier() and |
| 138 | + v.getAnAssignedValue() = getSettingsMa and |
| 139 | + isJSEnabled(v) |
| 140 | + ) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +from DataFlow::PathNode source, DataFlow::PathNode sink, FetchUntrustedResourceConfiguration conf |
| 145 | +where conf.hasFlowPath(source, sink) |
| 146 | +select sink.getNode().(UrlResourceSink).getMethodAccess(), source, sink, |
| 147 | + "Unsafe resource fetching in Android webview due to $@.", source.getNode(), |
| 148 | + sink.getNode().(UrlResourceSink).getSinkType() |
0 commit comments