Skip to content

Commit b9c2ee7

Browse files
committed
Java: Query for Android WebView File Access
Query for Android WebView file access settings
1 parent 8b11e98 commit b9c2ee7

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>
7+
File access in an Android WebView can expose the device's file system to
8+
the JavaScript running in the WebView. If there are vulnerabilities in the
9+
JavaScript, file access may allow an attacker to access or steal the
10+
user's data.
11+
</p>
12+
</overview>
13+
14+
<recommendation>
15+
<p>When possible, you should disallow file access by setting the following settings to <code>false</code>:</p>
16+
17+
<ul>
18+
<li><code>setAllowFileAccess</code></li>
19+
<li><code>setAllowFileAccessFromFileURLs</code></li>
20+
<li><code>setAllowUniversalAccessFromFileURLs</code></li>
21+
</ul>
22+
</recommendation>
23+
24+
<example>
25+
<p>In the following (bad) example, the WebView is configured with the settings
26+
which would allow local file access.</p>
27+
28+
<sample src="WebViewFileAccessUnsafe.java"/>
29+
30+
<p>In the following (good) example, the WebView is configured to disallow file access.</p>
31+
32+
<sample src="WebViewFileAccessSafe.java"/>
33+
34+
</example>
35+
36+
<references>
37+
<li>
38+
Android documentation: <a href="https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)"><code>WebSettings.setAllowFileAccess</code></a>.
39+
</li>
40+
<li>
41+
Android documentation: <a href="https://developer.android.com/reference/android/webkit/WebSettings#setAllowFileAccessFromFileURLs(boolean)"><code>WebSettings.setAllowFileAccessFromFileURLs</code></a>.
42+
</li>
43+
<li>
44+
Android documentation: <a href="https://developer.android.com/reference/android/webkit/WebSettings#setAllowUniversalAccessFromFileURLs(boolean)"><code>WebSettings.setAllowUniversalAccessFromFileURLs</code></a>.
45+
</li>
46+
<li>
47+
File access from URLs is enabled for WebView: <a href="https://oversecured.com/vulnerabilities#Android/File_access_from_file_URLs_is_enabled_for_WebView">File access for URLs is enabled for WebView</a>.
48+
</li>
49+
<li>
50+
File access is enabled for WebView: <a href="https://oversecured.com/vulnerabilities#Android/File_access_is_enabled_for_WebView">File access is enabled for WebView</a>.
51+
</li>
52+
<li>
53+
Universal file access from file URLs is enabled for WebView: <a href="https://oversecured.com/vulnerabilities#Android/Universal_file_access_from_file_URLs_is_enabled_for_WebView">Universal file access from file URLs is enabled for WebView</a>.
54+
</li>
55+
</references>
56+
57+
</qhelp>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @name Android WebSettings file access
3+
* @kind problem
4+
* @id java/android-websettings-file-access
5+
* @problem.severity warning
6+
* @security-severity 6.5
7+
* @precision high
8+
* @tags security
9+
* external/cwe/cwe-200
10+
*/
11+
12+
import java
13+
import semmle.code.java.frameworks.android.WebView
14+
15+
from MethodAccess ma
16+
where ma.getMethod() instanceof CrossOriginAccessMethod
17+
select ma, "WebView setting $@ may allow for unauthorized access of sensitive information.", ma,
18+
ma.getMethod().getName()
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);

0 commit comments

Comments
 (0)