Android WebView JavaScript enabled

Android WebView JavaScript enabled

Description

With JavaScript enabled, WebView runs scripts in the page it loads. If users supply the URL or an app builds HTML from their input, those scripts can run in the page’s context.

Potential impact

  • XSS execution within WebView.
  • Disclosure of cookies, tokens, or local-storage values.
  • Misuse of app functionality through addJavascriptInterface.

Remediation

  • Use setJavaScriptEnabled(false) for pages that need no scripts.
  • If JavaScript is required, limit both the initial URL and later navigation to trusted domains. Display user input as text, or escape it for the context where you insert it into HTML.

Examples

Before

java
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(userControlledUrl);

After

java
webView.getSettings().setJavaScriptEnabled(false);
webView.loadUrl("https://example.com/help");

The revised WebView loads a help page chosen by the app. It needs neither a user-supplied URL nor JavaScript.

References