Skip to content

Commit e91d5be

Browse files
author
Selim Gurun
committed
Make JavascriptInterface annotation public.
Bug: 7073422 This change makes @JavascriptInterface public and it requires using this annotation to allow javascript access to public java methods for API level JELLY_BEAN_MR1 and above. The behavior does not change for API levels JELLY_BEAN and below. Change-Id: I4108b17cf71b9ac273d7b61b1c8f7f5581e922ee
1 parent 0bb4d07 commit e91d5be

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

api/current.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26943,6 +26943,9 @@ package android.webkit {
2694326943
method public boolean useHttpAuthUsernamePassword();
2694426944
}
2694526945

26946+
public abstract class JavascriptInterface implements java.lang.annotation.Annotation {
26947+
}
26948+
2694626949
public class JsPromptResult extends android.webkit.JsResult {
2694726950
method public void confirm(java.lang.String);
2694826951
}

core/java/android/webkit/JavascriptInterface.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@
2525
* Annotation that allows exposing methods to JavaScript. Starting from API level
2626
* {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} and above, only methods explicitly
2727
* marked with this annotation are available to the Javascript code. See
28-
* {@link android.webkit.Webview#addJavaScriptInterface} for more information about it.
28+
* {@link android.webkit.WebView#addJavascriptInterface} for more information about it.
2929
*
30-
* @hide
3130
*/
3231
@SuppressWarnings("javadoc")
3332
@Retention(RetentionPolicy.RUNTIME)

core/java/android/webkit/WebView.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import android.graphics.Rect;
2727
import android.graphics.drawable.Drawable;
2828
import android.net.http.SslCertificate;
29-
import android.os.Build;
3029
import android.os.Bundle;
3130
import android.os.Looper;
3231
import android.os.Message;
@@ -1479,18 +1478,30 @@ public void setPictureListener(PictureListener listener) {
14791478
/**
14801479
* Injects the supplied Java object into this WebView. The object is
14811480
* injected into the JavaScript context of the main frame, using the
1482-
* supplied name. This allows the Java object's public methods to be
1483-
* accessed from JavaScript. Note that that injected objects will not
1481+
* supplied name. This allows the Java object's methods to be
1482+
* accessed from JavaScript. For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
1483+
* and above, only public methods that are annotated with
1484+
* {@link android.webkit.JavascriptInterface} can be accessed from JavaScript.
1485+
* For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below,
1486+
* all public methods (including the inherited ones) can be accessed, see the
1487+
* important security note below for implications. Note that injected objects will not
14841488
* appear in JavaScript until the page is next (re)loaded. For example:
1485-
* <pre> webView.addJavascriptInterface(new Object(), "injectedObject");
1489+
* <pre>
1490+
* class JsObject {
1491+
* {@literal @}JavascriptInterface
1492+
* public String toString() { return "injectedObject"; }
1493+
* }
1494+
* webView.addJavascriptInterface(new JsObject(), "injectedObject");
14861495
* webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
14871496
* webView.loadUrl("javascript:alert(injectedObject.toString())");</pre>
14881497
* <p>
14891498
* <strong>IMPORTANT:</strong>
14901499
* <ul>
14911500
* <li> This method can be used to allow JavaScript to control the host
14921501
* application. This is a powerful feature, but also presents a security
1493-
* risk, particularly as JavaScript could use reflection to access an
1502+
* risk for applications targeting API level
1503+
* {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below, because
1504+
* JavaScript could use reflection to access an
14941505
* injected object's public fields. Use of this method in a WebView
14951506
* containing untrusted content could allow an attacker to manipulate the
14961507
* host application in unintended ways, executing Java code with the
@@ -1499,6 +1510,7 @@ public void setPictureListener(PictureListener listener) {
14991510
* <li> JavaScript interacts with Java object on a private, background
15001511
* thread of this WebView. Care is therefore required to maintain thread
15011512
* safety.</li>
1513+
* <li> The Java object's fields are not accessible.</li>
15021514
* </ul>
15031515
*
15041516
* @param object the Java object to inject into this WebView's JavaScript
@@ -1508,9 +1520,6 @@ public void setPictureListener(PictureListener listener) {
15081520
public void addJavascriptInterface(Object object, String name) {
15091521
checkThread();
15101522
mProvider.addJavascriptInterface(object, name);
1511-
// TODO in a separate CL provide logic to enable annotations for API level JB_MR1 and above. Don't forget to
1512-
// update the doc, set a link to annotation and unhide the annotation.
1513-
// also describe that fields of java objects are not accessible from JS.
15141523
}
15151524

15161525
/**

core/java/android/webkit/WebViewClassic.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import android.net.Uri;
5656
import android.net.http.SslCertificate;
5757
import android.os.AsyncTask;
58+
import android.os.Build;
5859
import android.os.Bundle;
5960
import android.os.Handler;
6061
import android.os.Looper;
@@ -4119,10 +4120,17 @@ public void addJavascriptInterface(Object object, String name) {
41194120
return;
41204121
}
41214122
WebViewCore.JSInterfaceData arg = new WebViewCore.JSInterfaceData();
4122-
// TODO in a separate CL provide logic to enable annotations for API level JB_MR1 and above.
4123+
41234124
arg.mObject = object;
41244125
arg.mInterfaceName = name;
4125-
arg.mRequireAnnotation = false;
4126+
4127+
// starting with JELLY_BEAN_MR1, annotations are mandatory for enabling access to
4128+
// methods that are accessible from JS.
4129+
if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
4130+
arg.mRequireAnnotation = true;
4131+
} else {
4132+
arg.mRequireAnnotation = false;
4133+
}
41264134
mWebViewCore.sendMessage(EventHub.ADD_JS_INTERFACE, arg);
41274135
}
41284136

0 commit comments

Comments
 (0)