Skip to content

Commit 0ebfee8

Browse files
authored
Merge pull request github#11241 from egregius313/egregius313/webview-file-access
Java: Query to detect Android Webview file access
2 parents 4ff823c + 4278997 commit 0ebfee8

File tree

11 files changed

+150
-2
lines changed

11 files changed

+150
-2
lines changed

java/ql/lib/semmle/code/java/frameworks/android/WebView.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ class WebViewGetUrlMethod extends Method {
4545
class CrossOriginAccessMethod extends Method {
4646
CrossOriginAccessMethod() {
4747
this.getDeclaringType() instanceof TypeWebSettings and
48-
this.hasName(["setAllowUniversalAccessFromFileURLs", "setAllowFileAccessFromFileURLs"])
48+
this.hasName([
49+
"setAllowFileAccess", "setAllowUniversalAccessFromFileURLs",
50+
"setAllowFileAccessFromFileURLs"
51+
])
4952
}
5053
}
5154

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
Allowing file access in an Android WebView can expose a device's file system to
8+
the JavaScript running in that WebView. If the JavaScript contains
9+
vulnerabilities or the WebView loads untrusted content, file access
10+
allows an attacker to steal the user's data.
11+
</p>
12+
</overview>
13+
14+
<recommendation>
15+
<p>When possible, do not allow file access. The file access settings
16+
are disabled by default. You can explicitly disable file access by setting the
17+
following settings to <code>false</code>:</p>
18+
19+
<ul>
20+
<li><code>setAllowFileAccess</code></li>
21+
<li><code>setAllowFileAccessFromFileURLs</code></li>
22+
<li><code>setAllowUniversalAccessFromFileURLs</code></li>
23+
</ul>
24+
25+
<p>If your application requires access to the file system, it is best to
26+
avoid using <code>file://</code> URLs. Instead, use an alternative that
27+
loads files via HTTPS, such
28+
as <code>androidx.webkit.WebViewAssetLoader</code>.</p>
29+
</recommendation>
30+
31+
<example>
32+
<p>In the following (bad) example, the WebView is configured with settings
33+
that allow local file access.</p>
34+
35+
<sample src="WebViewFileAccessUnsafe.java"/>
36+
37+
<p>In the following (good) example, the WebView is configured to disallow file access.</p>
38+
39+
<sample src="WebViewFileAccessSafe.java"/>
40+
41+
<p>
42+
As mentioned previously, asset loaders can load files without file system
43+
access. In the following (good) example, an asset loader is configured to
44+
load assets over HTTPS.
45+
</p>
46+
47+
<sample src="AssetLoaderExample.java"/>
48+
</example>
49+
50+
<references>
51+
<li>
52+
Android documentation: <a href="https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)">WebSettings.setAllowFileAccess</a>.
53+
</li>
54+
<li>
55+
Android documentation: <a href="https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccessFromFileURLs(boolean)">WebSettings.setAllowFileAccessFromFileURLs</a>.
56+
</li>
57+
<li>
58+
Android documentation: <a href="https://developer.android.com/reference/android/webkit/WebSettings#setAllowUniversalAccessFromFileURLs(boolean)">WebSettings.setAllowUniversalAccessFromFileURLs</a>.
59+
</li>
60+
<li>
61+
Android documentation: <a href="https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader">WebViewAssetLoader</a>.
62+
</li>
63+
</references>
64+
65+
</qhelp>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @name Android WebSettings file access
3+
* @kind problem
4+
* @description Enabling access to the file system in a WebView allows attackers to view sensitive information.
5+
* @id java/android-websettings-file-access
6+
* @problem.severity warning
7+
* @security-severity 6.5
8+
* @precision medium
9+
* @tags security
10+
* external/cwe/cwe-200
11+
*/
12+
13+
import java
14+
import semmle.code.java.frameworks.android.WebView
15+
16+
from MethodAccess ma
17+
where
18+
ma.getMethod() instanceof CrossOriginAccessMethod and
19+
ma.getArgument(0).(CompileTimeConstantExpr).getBooleanValue() = true
20+
select ma,
21+
"WebView setting " + ma.getMethod().getName() +
22+
" may allow for unauthorized access of sensitive information."
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
WebViewAssetLoader loader = new WebViewAssetLoader.Builder()
2+
// Replace the domain with a domain you control, or use the default
3+
// appassets.androidplatform.com
4+
.setDomain("appassets.example.com")
5+
.addPathHandler("/resources", new AssetsPathHandler(this))
6+
.build();
7+
8+
webView.setWebViewClient(new WebViewClientCompat() {
9+
@Override
10+
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
11+
return assetLoader.shouldInterceptRequest(request.getUrl());
12+
}
13+
});
14+
15+
webView.loadUrl("https://appassets.example.com/resources/www/index.html");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
WebSettings settings = view.getSettings();
2+
3+
settings.setAllowFileAccess(false);
4+
settings.setAllowFileAccessFromURLs(false);
5+
settings.setAllowUniversalAccessFromURLs(false);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
WebSettings settings = view.getSettings();
2+
3+
settings.setAllowFileAccess(true);
4+
settings.setAllowFileAccessFromURLs(true);
5+
settings.setAllowUniversalAccessFromURLs(true);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: newQuery
3+
---
4+
* Added a new query `java/android-websettings-file-access` to detect configurations that enable file system access in Android WebViews.
5+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| WebViewFileAccess.java:8:9:8:41 | setAllowFileAccess(...) | WebView setting setAllowFileAccess may allow for unauthorized access of sensitive information. |
2+
| WebViewFileAccess.java:10:9:10:53 | setAllowFileAccessFromFileURLs(...) | WebView setting setAllowFileAccessFromFileURLs may allow for unauthorized access of sensitive information. |
3+
| WebViewFileAccess.java:12:9:12:58 | setAllowUniversalAccessFromFileURLs(...) | WebView setting setAllowUniversalAccessFromFileURLs may allow for unauthorized access of sensitive information. |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import android.webkit.WebView;
2+
import android.webkit.WebSettings;
3+
4+
class WebViewFileAccess {
5+
void configure(WebView view) {
6+
WebSettings settings = view.getSettings();
7+
8+
settings.setAllowFileAccess(true);
9+
10+
settings.setAllowFileAccessFromFileURLs(true);
11+
12+
settings.setAllowUniversalAccessFromFileURLs(true);
13+
}
14+
15+
void configureSafe(WebView view) {
16+
WebSettings settings = view.getSettings();
17+
18+
settings.setAllowFileAccess(false);
19+
20+
settings.setAllowFileAccessFromFileURLs(false);
21+
22+
settings.setAllowUniversalAccessFromFileURLs(false);
23+
}
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-200/AndroidWebViewSettingsFileAccess.ql

0 commit comments

Comments
 (0)