Skip to content

Instantly share code, notes, and snippets.

@granoeste
Last active October 11, 2015 10:18
Show Gist options
  • Save granoeste/3844236 to your computer and use it in GitHub Desktop.
Save granoeste/3844236 to your computer and use it in GitHub Desktop.
[Android] Security Considerations of JavascriptInterface of WebView

public void addJavascriptInterface (Object object, String name)

Since: API Level 1

Injects the supplied Java object into this WebView. The object is injected into the JavaScript context of the main frame, using the supplied name. This allows the Java object's public methods to be accessed from JavaScript. Note that that injected objects will not appear in JavaScript until the page is next (re)loaded. For example:

webView.addJavascriptInterface(new Object(), "injectedObject");  
webView.loadData("", "text/html", null);  
webView.loadUrl("javascript:alert(injectedObject.toString())");  

IMPORTANT:

  • This method can be used to allow JavaScript to control the host application. This is a powerful feature, but also presents a security risk, particularly as JavaScript could use reflection to access an injected object's public fields. Use of this method in a WebView containing untrusted content could allow an attacker to manipulate the host application in unintended ways, executing Java code with the permissions of the host application. Use extreme care when using this method in a WebView which could contain untrusted content.
  • JavaScript interacts with Java object on a private, background thread of this WebView. Care is therefore required to maintain thread safety.

Parameters

  • object the Java object to inject into this WebView's JavaScript context. Null values are ignored.
  • name the name used to expose the object in JavaScript

重要:

  • このメソッドは、JavaScriptがホストアプリケーションを制御できるようにするために使用することができます。これはJavaScriptが注入されたオブジェクトのパブリックフィールドにアクセスするためにリフレクションを使用することができ、特にとして、強力な機能ですが、セキュリティ上のリスクが発生します。信頼できないコンテンツを含むWebViewでこのメソッドを使用すると、攻撃者はホストアプリケーションのアクセス許可を使用してJavaコードを実行すると、意図しない方法で、ホストアプリケーションを操作する可能性があります。信頼できないコンテンツが含まれている可能性がWebViewでこのメソッドを使用するときに、細心の注意を払ってください。

  • JavaScriptはこのWebViewのプライベート、バックグラウンドスレッドでJavaオブジェクトと対話します。ケアは、したがって、スレッドの安全性を維持するために必要である。

  • WebView | Android Developers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment