クロスアプリ スクリプティング
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
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()
などのメソッドを使用する場合は、渡された URL がチェックされていることを確認してください。,
前述のように、WebView に渡される JavaScript は、想定されるドメインからのみ取得される必要があります。そのため、何が読み込まれているかを検証することが重要です。
OWASP の入力検証に関するドキュメントとこちらの Android をご確認ください
セキュリティチェックリストをご覧ください。
WebView の安全なファイル アクセスを設定する
ファイルにアクセスできないようにすれば、任意の JavaScript によるアクセスを
次の WebSettings
を
ファイル・アクセスを保護する場合に考慮すべきことです。
- ファイル アクセスを無効にします。デフォルトでは、
setAllowFileAccess
は True
に設定されています。
API レベル 29 以前: ローカル ファイルへのアクセスを許可します。API レベル 30 以降では、デフォルトは False
です。ファイルへのアクセスが許可されないようにするには、
setAllowFileAccess
を明示的に False
に設定する
コンテンツ アクセスを無効にします。setAllowContentAccess
のデフォルト設定は True
です。コンテンツ URL アクセスにより、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 UTC。
[[["わかりやすい","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 UTC。"],[],[],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))"]]