WebView addJavascriptInterface
2014-01-07 15:08
260 查看
在android4.2以前,注入步骤如下:
[java] view
plaincopy
webview.getSetting().setJavaScriptEnable(true);
class JsObject {
public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
Android4.2及以后,注入步骤如下:
[java] view
plaincopy
[java] view
plaincopy
webview.getSetting().setJavaScriptEnable(true);
class JsObject {
@JavascriptInterface
public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
发现区别没?4.2之前向webview注入的对象所暴露的接口toString没有注释语句@JavascriptInterface,而4.2及以后的则多了注释语句@JavascriptInterface
经过查官方文档所知,因为这个接口允许JavaScript 控制宿主应用程序,这是个很强大的特性,但同时,在4.2的版本前存在重大安全隐患,因为JavaScript 可以使用反射访问注入webview的java对象的public
fields,在一个包含不信任内容的WebView中使用这个方法,会允许攻击者去篡改宿主应用程序,使用宿主应用程序的权限执行java代码。因此4.2以后,任何为JS暴露的接口,都需要加
官方文档说明:
From the Android 4.2 documentation:
Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the
annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.
注:如果将targetSdkVersion
设置为17或者更高,但却没有给暴露的js接口加@JavascriptInterface注释,则logcat会报如下输出:
E/Web Console: Uncaught TypeError: Object [object Object] has no method 'toString'
Added in 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 methods to be accessed from JavaScript. For applications targeted to API level
above, only public methods that are annotated with
be accessed from JavaScript. For applications targeted to API level
below, all public methods (including the inherited ones) can be accessed, see the important security note below for implications.
Note that injected objects will not appear in JavaScript until the page is next (re)loaded. For example:
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 for applications targeted to API level
below, because 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.
The Java object's fields are not accessible.
Parameters
[java] view
plaincopy
webview.getSetting().setJavaScriptEnable(true);
class JsObject {
public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
Android4.2及以后,注入步骤如下:
[java] view
plaincopy
[java] view
plaincopy
webview.getSetting().setJavaScriptEnable(true);
class JsObject {
@JavascriptInterface
public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
发现区别没?4.2之前向webview注入的对象所暴露的接口toString没有注释语句@JavascriptInterface,而4.2及以后的则多了注释语句@JavascriptInterface
经过查官方文档所知,因为这个接口允许JavaScript 控制宿主应用程序,这是个很强大的特性,但同时,在4.2的版本前存在重大安全隐患,因为JavaScript 可以使用反射访问注入webview的java对象的public
fields,在一个包含不信任内容的WebView中使用这个方法,会允许攻击者去篡改宿主应用程序,使用宿主应用程序的权限执行java代码。因此4.2以后,任何为JS暴露的接口,都需要加
@JavascriptInterface注释,这样,这个Java对象的fields 将不允许被JS访问。
官方文档说明:
From the Android 4.2 documentation:
Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the
annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.
注:如果将targetSdkVersion
设置为17或者更高,但却没有给暴露的js接口加@JavascriptInterface注释,则logcat会报如下输出:
E/Web Console: Uncaught TypeError: Object [object Object] has no method 'toString'
public void addJavascriptInterface (Object object, String name)
Added in API level 1Injects 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 methods to be accessed from JavaScript. For applications targeted to API level
JELLY_BEAN_MR1and
above, only public methods that are annotated with
JavascriptInterfacecan
be accessed from JavaScript. For applications targeted to API level
JELLY_BEANor
below, all public methods (including the inherited ones) can be accessed, see the important security note below for implications.
Note that injected objects will not appear in JavaScript until the page is next (re)loaded. For example:
class JsObject { @JavascriptInterface public String toString() { return "injectedObject"; } } webView.addJavascriptInterface(new JsObject(), "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 for applications targeted to API level
JELLY_BEANor
below, because 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.
The Java object's fields are not accessible.
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 |
相关文章推荐
- 安卓webview和js+html交互利用的addJavascriptInterface和webview.loadUrl("javascript:**");
- Android WebView 中 addJavascriptInterface 接口无效问题
- 备忘--android 4.2+ WebView的addJavascriptInterface(), js找不到注入的函数
- android软件开发之webView.addJavascriptInterface循环渐进【一】
- 安卓webview和js+html交互利用的addJavascriptInterface和webview.loadUrl("javascript:**");
- Android软件开发之webView.addJavascriptInterface循环渐进【一】
- android软件开发之webView.addJavascriptInterface循环渐进【一】
- Android 4.2版本以下使用WebView组件addJavascriptInterface方法存在JS漏洞
- 安卓webview和js+html交互利用的addJavascriptInterface和webview.loadUrl("javascript:**");
- android软件开发之webView.addJavascriptInterface循环渐进【一】
- android软件开发之webView.addJavascriptInterface循环渐进【二】
- WebView中addJavascriptInterface接口的隐患解决方案
- 安卓webview和js+html交互利用的addJavascriptInterface和webview.loadUrl("javascript:**");
- webView.addJavascriptInterface 用法
- 关于android webview js桥(addJavascriptInterface) 失效的问题
- 在webview中addJavascriptInterface,js调用java函数打包出现不响应,即使对混淆处理
- Android WebView在API17后addJavascriptInterface方法无效
- android软件开发之webView.addJavascriptInterface循环渐进【一】
- WebView在API 17后调addJavascriptInterface方法无效的解决办法
- WebView中addJavascriptInterface不起作用解决方法