1
- /**
2
- * @name Unsafe resource fetching in Android webview
3
- * @description JavaScript rendered inside WebViews can access any protected
4
- * application file and web resource from any origin
5
- * @kind path-problem
6
- * @problem.severity warning
7
- * @precision medium
8
- * @id java/android/unsafe-android-webview-fetch
9
- * @tags security
10
- * external/cwe/cwe-749
11
- * external/cwe/cwe-079
12
- */
13
-
14
1
import java
15
- import semmle.code.java.frameworks.android.Intent
16
2
import semmle.code.java.frameworks.android.WebView
17
- import semmle.code.java.dataflow.FlowSources
18
- import DataFlow :: PathGraph
3
+ import semmle.code.java.dataflow.DataFlow
4
+ import semmle.code.java.dataflow.ExternalFlow
19
5
20
6
/**
21
7
* Methods allowing any-local-file and cross-origin access in the WebSettings class
22
8
*/
23
- class CrossOriginAccessMethod extends Method {
9
+ private class CrossOriginAccessMethod extends Method {
24
10
CrossOriginAccessMethod ( ) {
25
11
this .getDeclaringType ( ) instanceof TypeWebSettings and
26
12
(
@@ -33,7 +19,7 @@ class CrossOriginAccessMethod extends Method {
33
19
/**
34
20
* `setJavaScriptEnabled` method for the webview
35
21
*/
36
- class AllowJavaScriptMethod extends Method {
22
+ private class AllowJavaScriptMethod extends Method {
37
23
AllowJavaScriptMethod ( ) {
38
24
this .getDeclaringType ( ) instanceof TypeWebSettings and
39
25
this .hasName ( "setJavaScriptEnabled" )
@@ -43,7 +29,7 @@ class AllowJavaScriptMethod extends Method {
43
29
/**
44
30
* Holds if a call to `v.setJavaScriptEnabled(true)` exists
45
31
*/
46
- predicate isJSEnabled ( Variable v ) {
32
+ private predicate isJSEnabled ( Variable v ) {
47
33
exists ( MethodAccess jsa |
48
34
v .getAnAccess ( ) = jsa .getQualifier ( ) and
49
35
jsa .getMethod ( ) instanceof AllowJavaScriptMethod and
@@ -54,7 +40,7 @@ predicate isJSEnabled(Variable v) {
54
40
/**
55
41
* Fetch URL method call on the `android.webkit.WebView` object
56
42
*/
57
- class FetchResourceMethodAccess extends MethodAccess {
43
+ private class FetchResourceMethodAccess extends MethodAccess {
58
44
FetchResourceMethodAccess ( ) {
59
45
this .getMethod ( ) .getDeclaringType ( ) instanceof TypeWebView and
60
46
this .getMethod ( ) .hasName ( [ "loadUrl" , "postUrl" ] )
@@ -64,12 +50,14 @@ class FetchResourceMethodAccess extends MethodAccess {
64
50
/**
65
51
* Holds if `ma` loads URL `sink`
66
52
*/
67
- predicate fetchResource ( FetchResourceMethodAccess ma , Expr sink ) { sink = ma .getArgument ( 0 ) }
53
+ private predicate fetchResource ( FetchResourceMethodAccess ma , Expr sink ) {
54
+ sink = ma .getArgument ( 0 )
55
+ }
68
56
69
57
/**
70
58
* A URL argument to a `loadUrl` or `postUrl` call, considered as a sink.
71
59
*/
72
- class UrlResourceSink extends DataFlow:: ExprNode {
60
+ private class UrlResourceSink extends DataFlow:: ExprNode {
73
61
UrlResourceSink ( ) { fetchResource ( _, this .getExpr ( ) ) }
74
62
75
63
/** Gets the fetch method that fetches this sink URL. */
@@ -103,28 +91,14 @@ class UrlResourceSink extends DataFlow::ExprNode {
103
91
}
104
92
}
105
93
106
- /**
107
- * Taint configuration tracking flow from untrusted inputs to `loadUrl` or `postUrl` calls.
108
- */
109
- class FetchUntrustedResourceConfiguration extends TaintTracking:: Configuration {
110
- FetchUntrustedResourceConfiguration ( ) { this = "FetchUntrustedResourceConfiguration" }
111
-
112
- override predicate isSource ( DataFlow:: Node source ) { source instanceof RemoteFlowSource }
113
-
114
- override predicate isSink ( DataFlow:: Node sink ) {
115
- sink instanceof UrlResourceSink and
94
+ class FetchUntrustedResourceSink extends UrlResourceSink {
95
+ FetchUntrustedResourceSink ( ) {
116
96
exists ( VarAccess webviewVa , MethodAccess getSettingsMa , Variable v |
117
- sink . ( UrlResourceSink ) .getMethodAccess ( ) .getQualifier ( ) = webviewVa and
97
+ this .getMethodAccess ( ) .getQualifier ( ) = webviewVa and
118
98
getSettingsMa .getMethod ( ) instanceof WebViewGetSettingsMethod and
119
99
webviewVa .getVariable ( ) .getAnAccess ( ) = getSettingsMa .getQualifier ( ) and
120
100
v .getAnAssignedValue ( ) = getSettingsMa and
121
101
isJSEnabled ( v )
122
102
)
123
103
}
124
104
}
125
-
126
- from DataFlow:: PathNode source , DataFlow:: PathNode sink , FetchUntrustedResourceConfiguration conf
127
- where conf .hasFlowPath ( source , sink )
128
- select sink .getNode ( ) .( UrlResourceSink ) .getMethodAccess ( ) , source , sink ,
129
- "Unsafe resource fetching in Android webview due to $@." , source .getNode ( ) ,
130
- sink .getNode ( ) .( UrlResourceSink ) .getSinkType ( )
0 commit comments