Skip to content

Commit 4f8da32

Browse files
Selim GurunAndroid (Google) Code Review
authored andcommitted
Merge "Make JavascriptInterface annotation public." into jb-mr1-dev
2 parents 743115f + e91d5be commit 4f8da32

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;
@@ -1494,18 +1493,30 @@ public void setPictureListener(PictureListener listener) {
14941493
/**
14951494
* Injects the supplied Java object into this WebView. The object is
14961495
* injected into the JavaScript context of the main frame, using the
1497-
* supplied name. This allows the Java object's public methods to be
1498-
* accessed from JavaScript. Note that that injected objects will not
1496+
* supplied name. This allows the Java object's methods to be
1497+
* accessed from JavaScript. For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
1498+
* and above, only public methods that are annotated with
1499+
* {@link android.webkit.JavascriptInterface} can be accessed from JavaScript.
1500+
* For API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below,
1501+
* all public methods (including the inherited ones) can be accessed, see the
1502+
* important security note below for implications. Note that injected objects will not
14991503
* appear in JavaScript until the page is next (re)loaded. For example:
1500-
* <pre> webView.addJavascriptInterface(new Object(), "injectedObject");
1504+
* <pre>
1505+
* class JsObject {
1506+
* {@literal @}JavascriptInterface
1507+
* public String toString() { return "injectedObject"; }
1508+
* }
1509+
* webView.addJavascriptInterface(new JsObject(), "injectedObject");
15011510
* webView.loadData("<!DOCTYPE html><title></title>", "text/html", null);
15021511
* webView.loadUrl("javascript:alert(injectedObject.toString())");</pre>
15031512
* <p>
15041513
* <strong>IMPORTANT:</strong>
15051514
* <ul>
15061515
* <li> This method can be used to allow JavaScript to control the host
15071516
* application. This is a powerful feature, but also presents a security
1508-
* risk, particularly as JavaScript could use reflection to access an
1517+
* risk for applications targeting API level
1518+
* {@link android.os.Build.VERSION_CODES#JELLY_BEAN} or below, because
1519+
* JavaScript could use reflection to access an
15091520
* injected object's public fields. Use of this method in a WebView
15101521
* containing untrusted content could allow an attacker to manipulate the
15111522
* host application in unintended ways, executing Java code with the
@@ -1514,6 +1525,7 @@ public void setPictureListener(PictureListener listener) {
15141525
* <li> JavaScript interacts with Java object on a private, background
15151526
* thread of this WebView. Care is therefore required to maintain thread
15161527
* safety.</li>
1528+
* <li> The Java object's fields are not accessible.</li>
15171529
* </ul>
15181530
*
15191531
* @param object the Java object to inject into this WebView's JavaScript
@@ -1523,9 +1535,6 @@ public void setPictureListener(PictureListener listener) {
15231535
public void addJavascriptInterface(Object object, String name) {
15241536
checkThread();
15251537
mProvider.addJavascriptInterface(object, name);
1526-
// TODO in a separate CL provide logic to enable annotations for API level JB_MR1 and above. Don't forget to
1527-
// update the doc, set a link to annotation and unhide the annotation.
1528-
// also describe that fields of java objects are not accessible from JS.
15291538
}
15301539

15311540
/**

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)