Android WebView JavaScript interface exposure

Android WebView JavaScript interface exposure

Description

addJavascriptInterface exposes a Java or Kotlin object to every frame in a WebView. Scripts in an external page or HTML built from user input can then call the app methods you expose.

Potential impact

  • Misuse of internal app functionality.
  • Disclosure of sensitive data.
  • Increased risk of remote code execution on older Android versions.

Remediation

  • Use a bridge only if the page and every embedded frame load app-controlled static content. Keep it out of WebViews that display external content.
  • Expose only the methods you need with @JavascriptInterface. Check permissions and validate input in each method.

Examples

Before

java
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Bridge(), "bridge");
webView.loadUrl(userControlledUrl);

After

java
webView.getSettings().setJavaScriptEnabled(false);
webView.loadUrl("file:///android_asset/help.html");

The revised WebView displays the bundled help page with JavaScript disabled and no bridge.

References