跨應用程式指令碼
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
OWASP 類別:MASVS-CODE:程式碼品質
總覽
WebView 是 Android 應用程式中的嵌入式瀏覽器元件,
有助於在應用程式中顯示網頁內容。這類程式碼可轉譯 HTML、CSS 和
應用程式使用者介面中的 JavaScript。
跨應用程式指令碼與惡意程式碼的執行普遍相關
並用於受害應用程式的情境中就本文件而言,主題將專門限制在將惡意 JavaScript 程式碼插入漏洞 WebView 的情況。
應用程式接受惡意 JavaScript 而直接進入 WebView 時
驗證或清理作業,會導致應用程式容易遭受跨應用程式攻擊
編寫指令碼。
影響
當攻擊者控管的 JavaScript 內容未經過驗證或處理,就會傳遞至有安全漏洞的應用程式 WebView,進而遭到利用。因此,攻擊者提供的 JavaScript 程式碼會在受害應用程式的 WebView 中執行。惡意 JavaScript 程式碼隨後便可使用與受害應用程式相同的權限,進而竊取機密使用者資料,並盜用帳戶。
因應措施
停用 JavaScript
如果您的應用程式不需要 JavaScript,將其停用即可確保
並未成為威脅:
Kotlin
// Get the WebView Object
val webView = findViewById<WebView>(R.id.webView)
val webSettings = webView.settings
// Disable JavaScript
webSettings.javaScriptEnabled = false
Java
// Get the WebView Object
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
// Disable JavaScript for the WebView
webSettings.setJavaScriptEnabled(false);
如果您的應用程式需要 JavaScript,請確保您擁有或控制
傳送至 WebView 的 JavaScript。避免讓 WebView 任意執行
JavaScript,請參閱下一節的指南。
確保 WebView 中僅載入預期的內容
使用 shouldOverrideUrlLoading()
、loadUrl()
或 evaluateJavascript()
,
等方法時,請務必檢查傳遞給這些方法的所有網址。如先前所述,傳送至 WebView 的任何 JavaScript
來自預期網域,因此請務必驗證載入的內容。
請參閱 OWASP 的輸入驗證說明文件,以及這份 Android 安全性 檢查清單,瞭解 WebView 的最佳建議和範例。
調整 WebView 的檔案存取權設定
確保無法存取檔案,可避免在 WebView 中執行任意 JavaScript。在保護檔案存取權時,請考量下列 WebSettings
:
- 停用檔案存取權。根據預設,
setAllowFileAccess
會在以下位置設為 True
:
API 級別 29 以下,即可允許存取本機檔案。在 API 級別 30 中
且預設值為 False
。為確保不允許存取檔案,請明確將 setAllowFileAccess
設為 False
停用內容存取權。setAllowContentAccess
預設為
True
。內容網址存取權可讓 WebView 從系統中安裝的內容供應器載入內容。如果您的應用程式不需要內容存取權,
將 setAllowContentAccess
設為 False
,以免發生下列情況
跨應用程式指令碼攻擊
Kotlin
kotlin
webView.settings.javaScriptEnabled = false
webView.settings.domStorageEnabled = true
webView.settings.allowFileAccess = false
webView.settings.allowContentAccess = false
Java
java
webView.getSettings().setJavaScriptEnabled(false);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAllowFileAccess(false);
webView.getSettings().setAllowContentAccess(false);
啟用安全瀏覽
在 AndroidManifest.xml
中啟用安全瀏覽功能,以掃描傳送至以下地址的網址:
透過 WebView 防範網路釣魚或惡意網域:
<meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
android:value="true" />
資源
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# Cross-app scripting\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CODE: Code Quality](https://mas.owasp.org/MASVS/10-MASVS-CODE)\n\nOverview\n--------\n\nA WebView is an embedded browser component in Android applications that\nfacilitates the display of web content within an app. It renders HTML, CSS, and\nJavaScript within the app's user interface.\n\nCross-App Scripting is broadly associated with the execution of malicious code\nin the context of a victim application. For the purposes of this documentation,\nthe subject will be constrained specifically to the injection of malicious\nJavaScript code into a vulnerable WebView.\n\nWhen an app accepts malicious JavaScript into a WebView without sufficient\nvalidation or sanitization, the application is vulnerable to cross-app\nScripting.\n\nImpact\n------\n\nCross-app scripting vulnerabilities can be exploited when attacker-controlled\nJavaScript content is passed to the vulnerable app's WebView without being\nvalidated or sanitized. As a result, the JavaScript code provided by the\nattacker is executed in the context of the victim application's WebView. The\nmalicious JavaScript code can then use the same permissions as the victim app's,\nwhich may lead to theft of sensitive user data, and account hijacking.\n\nMitigations\n-----------\n\n### Disable JavaScript\n\nIf your application does not require JavaScript, disabling it will ensure it\ndoes not become a threat: \n\n### Kotlin\n\n // Get the WebView Object\n val webView = findViewById\u003cWebView\u003e(R.id.webView)\n val webSettings = webView.settings\n\n // Disable JavaScript\n webSettings.javaScriptEnabled = false\n\n### Java\n\n // Get the WebView Object\n WebView webView = (WebView) findViewById(R.id.webView);\n WebSettings webSettings = webView.getSettings();\n\n // Disable JavaScript for the WebView\n webSettings.setJavaScriptEnabled(false);\n\nIf your application does require JavaScript, ensure that you own or control any\nJavaScript passed to WebView. Avoid allowing WebView to execute arbitrary\nJavaScript, see the guidance in the next section.\n\n### Ensure only expected content is loaded into WebView\n\nWhen using methods like [`shouldOverrideUrlLoading()`](/reference/android/webkit/WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView,%20android.webkit.WebResourceRequest)), [`loadUrl()`](/reference/android/webkit/WebView#loadUrl(java.lang.String)), or\n[`evaluateJavascript()`](/reference/android/webkit/WebView#evaluateJavascript(java.lang.String,%20android.webkit.ValueCallback%3Cjava.lang.String%3E))`,` make sure that any URLs passed to them are\nchecked. As stated earlier, any JavaScript passed to the WebView should only\ncome from expected domains, so it is important to verify what is being loaded.\n\nCheck OWASP's input validation [documentation](https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html) and this Android\nsecurity [checklist](https://blog.oversecured.com/Android-security-checklist-webview/) for WebViews for good advice and examples.\n\n### Set secure file access settings for WebView\n\nEnsuring that files are not accessible can prevent arbitrary JavaScript from\nbeing executed within WebViews.The following [`WebSettings`](/reference/android/webkit/WebSettings) should be\nconsidered when securing file access:\n\n- Disable file access. By default, [`setAllowFileAccess`](/reference/android/webkit/WebSettings#setAllowFileAccess(boolean)) is set to `True` in API level 29 and lower which will permit access to local files. In API level 30 and higher the default is `False`. To ensure file access is not permitted, explicitly set `setAllowFileAccess` to `False`\n- Disable content access. The default setting of [`setAllowContentAccess`](/reference/android/webkit/WebSettings#setAllowContentAccess(boolean)) is\n `True`. Content URL access allows WebView to load content from a content\n provider installed in the system. If your app does not require content access,\n set `setAllowContentAccess` to `False` to prevent potential misuse in case of a\n cross-app scripting attack.\n\n- kotlin\n `kotlin\n webView.settings.javaScriptEnabled = false\n webView.settings.domStorageEnabled = true\n webView.settings.allowFileAccess = false\n webView.settings.allowContentAccess = false`\n\n- java\n `java\n webView.getSettings().setJavaScriptEnabled(false);\n webView.getSettings().setDomStorageEnabled(true);\n webView.getSettings().setAllowFileAccess(false);\n webView.getSettings().setAllowContentAccess(false);`\n\n### Enable Safe Browsing\n\nEnable Safe Browsing in [`AndroidManifest.xml`](/guide/topics/manifest/manifest-intro) to scan URLs passed to\nWebView for phishing or malicious domains.: \n\n \u003cmeta-data android:name=\"android.webkit.WebView.EnableSafeBrowsing\"\n android:value=\"true\" /\u003e\n\nResources\n---------\n\n- [Safe Browsing documentation](/privacy-and-security/safetynet/safebrowsing)\n- [WebView developer reference](/reference/android/webkit/WebView)\n- [WebSettings for WebView developer reference](/reference/android/webkit/WebSettings)\n- [setAllowFileAccess developer documentation](/reference/android/webkit/WebSettings#setAllowFileAccess(boolean))\n- [setAllowContentAccess developer reference](/reference/android/webkit/WebSettings#setAllowContentAccess(boolean))"]]