|
| 1 | +/** |
| 2 | + * @name Unsafe resource loading in Android webview |
| 3 | + * @description JavaScript rendered inside WebViews can access any protected application file and web resource from any origin |
| 4 | + * @kind path-problem |
| 5 | + * @tags security |
| 6 | + * external/cwe/cwe-749 |
| 7 | + * external/cwe/cwe-079 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | +import semmle.code.java.frameworks.android.Intent |
| 12 | +import semmle.code.java.frameworks.android.WebView |
| 13 | +import semmle.code.java.dataflow.FlowSources |
| 14 | + |
| 15 | +/** |
| 16 | + * Allow universal access methods in the WebSettings class |
| 17 | + */ |
| 18 | +class AllowUniversalAccessMethod extends Method { |
| 19 | + AllowUniversalAccessMethod() { |
| 20 | + this.getDeclaringType() instanceof TypeWebSettings and |
| 21 | + ( |
| 22 | + this.hasName("setAllowUniversalAccessFromFileURLs") or |
| 23 | + this.hasName("setAllowFileAccessFromFileURLs") |
| 24 | + ) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * `setJavaScriptEnabled` method for the webview |
| 30 | + */ |
| 31 | +class AllowJavaScriptMethod extends Method { |
| 32 | + AllowJavaScriptMethod() { |
| 33 | + this.getDeclaringType() instanceof TypeWebSettings and |
| 34 | + this.hasName("setJavaScriptEnabled") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Check whether JavaScript is enabled in the webview with universal resource access |
| 40 | + */ |
| 41 | +predicate isJSEnabled(MethodAccess ma) { |
| 42 | + exists(VarAccess va, MethodAccess jsa | |
| 43 | + ma.getQualifier() = va and |
| 44 | + jsa.getQualifier() = va.getVariable().getAnAccess() and |
| 45 | + jsa.getMethod() instanceof AllowJavaScriptMethod and |
| 46 | + jsa.getArgument(0).(BooleanLiteral).getBooleanValue() = true |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Load URL method call on the `android.webkit.WebView` object |
| 52 | + */ |
| 53 | +class LoadResourceMethodAccess extends MethodAccess { |
| 54 | + LoadResourceMethodAccess() { |
| 55 | + this.getMethod().getDeclaringType() instanceof TypeWebView and |
| 56 | + ( |
| 57 | + this.getMethod().hasName("loadUrl") or |
| 58 | + this.getMethod().hasName("postUrl") |
| 59 | + ) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Method access to external inputs of `android.content.Intent` object |
| 65 | + */ |
| 66 | +class IntentGetExtraMethodAccess extends MethodAccess { |
| 67 | + IntentGetExtraMethodAccess() { |
| 68 | + this.getMethod().getName().regexpMatch("get\\w+Extra") and |
| 69 | + this.getMethod().getDeclaringType() instanceof TypeIntent |
| 70 | + or |
| 71 | + this.getMethod().getName().regexpMatch("get\\w+") and |
| 72 | + this.getQualifier().(MethodAccess).getMethod().hasName("getExtras") and |
| 73 | + this.getQualifier().(MethodAccess).getMethod().getDeclaringType() instanceof TypeIntent |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Source of loading urls |
| 79 | + */ |
| 80 | +class UntrustedResourceSource extends RemoteFlowSource { |
| 81 | + UntrustedResourceSource() { |
| 82 | + exists(MethodAccess ma | |
| 83 | + ma instanceof IntentGetExtraMethodAccess and |
| 84 | + this.asExpr().(VarAccess).getVariable().getAnAssignedValue() = ma |
| 85 | + ) |
| 86 | + } |
| 87 | + |
| 88 | + override string getSourceType() { |
| 89 | + result = "user input vulnerable to XSS and sensitive resource disclosure attacks" and |
| 90 | + exists(MethodAccess ma | |
| 91 | + ma.getMethod() instanceof AllowUniversalAccessMethod and //High precision match of unsafe resource loading |
| 92 | + ma.getArgument(0).(BooleanLiteral).getBooleanValue() = true |
| 93 | + ) |
| 94 | + or |
| 95 | + result = "user input potentially vulnerable to XSS and sensitive resource disclosure attacks" and |
| 96 | + not exists(MethodAccess ma | |
| 97 | + ma.getMethod() instanceof AllowUniversalAccessMethod and //High precision match of unsafe resource loading |
| 98 | + ma.getArgument(0).(BooleanLiteral).getBooleanValue() = true |
| 99 | + ) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * load externally controlled data from loadUntrustedResource |
| 105 | + */ |
| 106 | +predicate loadUntrustedResource(MethodAccess ma, Expr sink) { |
| 107 | + ma instanceof LoadResourceMethodAccess and |
| 108 | + sink = ma.getArgument(0) |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Sink of loading urls |
| 113 | + */ |
| 114 | +class UntrustedResourceSink extends DataFlow::ExprNode { |
| 115 | + UntrustedResourceSink() { loadUntrustedResource(_, this.getExpr()) } |
| 116 | + |
| 117 | + MethodAccess getMethodAccess() { loadUntrustedResource(result, this.getExpr()) } |
| 118 | +} |
| 119 | + |
| 120 | +class LoadUntrustedResourceConfiguration extends TaintTracking::Configuration { |
| 121 | + LoadUntrustedResourceConfiguration() { this = "LoadUntrustedResourceConfiguration" } |
| 122 | + |
| 123 | + override predicate isSource(DataFlow::Node source) { source instanceof UntrustedResourceSource } |
| 124 | + |
| 125 | + override predicate isSink(DataFlow::Node sink) { sink instanceof UntrustedResourceSink } |
| 126 | +} |
| 127 | + |
| 128 | +from DataFlow::PathNode source, DataFlow::PathNode sink, LoadUntrustedResourceConfiguration conf |
| 129 | +where |
| 130 | + exists(VarAccess webviewVa, MethodAccess getSettingsMa, MethodAccess ma | |
| 131 | + conf.hasFlowPath(source, sink) and |
| 132 | + sink.getNode().(UntrustedResourceSink).getMethodAccess().getQualifier() = webviewVa and |
| 133 | + webviewVa.getVariable().getAnAccess() = getSettingsMa.getQualifier() and |
| 134 | + ma.getQualifier().(VarAccess).getVariable().getAnAssignedValue() = getSettingsMa and |
| 135 | + isJSEnabled(ma) |
| 136 | + ) |
| 137 | +select sink.getNode().(UntrustedResourceSink).getMethodAccess(), source, sink, |
| 138 | + "Unsafe resource loading in Android webview due to $@.", source.getNode(), |
| 139 | + source.getNode().(UntrustedResourceSource).getSourceType() |
0 commit comments