You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2014/10/24 22:18:40 UTC

[09/12] android commit: Defer whitelist decisions to plugins

Defer whitelist decisions to plugins

There is a default policy, which is implemented in the case where no plugins override any of the whitelist methods:
 * Error URLs must start with file://
 * Navigation is allowed to file:// and data: URLs which do not contain "app_webview"
 * External URLs do not launch intents
 * XHRs are allowed to file:// and data: URLs which do not contain "app_webview"


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/972540f4
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/972540f4
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/972540f4

Branch: refs/heads/unplug-whitelist
Commit: 972540f4434b0b72f7d062e12652e9b1ac31c09b
Parents: b56c12f
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Oct 22 16:16:52 2014 -0400
Committer: Ian Clelland <ic...@chromium.org>
Committed: Wed Oct 22 16:28:07 2014 -0400

----------------------------------------------------------------------
 .../src/org/apache/cordova/AndroidWebView.java  |  6 +-
 .../src/org/apache/cordova/CordovaActivity.java |  8 ++-
 .../org/apache/cordova/CordovaUriHelper.java    | 71 +++++++++++++++-----
 .../cordova/IceCreamCordovaWebViewClient.java   | 10 ++-
 4 files changed, 73 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/972540f4/framework/src/org/apache/cordova/AndroidWebView.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/AndroidWebView.java b/framework/src/org/apache/cordova/AndroidWebView.java
index 9817476..1d152d1 100755
--- a/framework/src/org/apache/cordova/AndroidWebView.java
+++ b/framework/src/org/apache/cordova/AndroidWebView.java
@@ -354,7 +354,9 @@ public class AndroidWebView extends WebView implements CordovaWebView {
         if (LOG.isLoggable(LOG.DEBUG) && !url.startsWith("javascript:")) {
             LOG.d(TAG, ">>> loadUrlNow()");
         }
-        if (url.startsWith("file://") || url.startsWith("javascript:") || internalWhitelist.isUrlWhiteListed(url)) {
+        Boolean shouldAllowNavigation = pluginManager.shouldAllowNavigation(url);
+        if (url.startsWith("file://") || url.startsWith("javascript:") ||
+            (shouldAllowNavigation != null && shouldAllowNavigation == true)) {
             super.loadUrl(url);
         }
     }
@@ -429,7 +431,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
         if (!openExternal) {
 
             // Make sure url is in whitelist
-            if (url.startsWith("file://") || internalWhitelist.isUrlWhiteListed(url)) {
+            if (url.startsWith("file://") || pluginManager.shouldAllowNavigation(url)) {
                 // TODO: What about params?
                 // Load new URL
                 loadUrlIntoView(url, true);

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/972540f4/framework/src/org/apache/cordova/CordovaActivity.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java
index e065a9b..93309f0 100755
--- a/framework/src/org/apache/cordova/CordovaActivity.java
+++ b/framework/src/org/apache/cordova/CordovaActivity.java
@@ -554,7 +554,13 @@ public class CordovaActivity extends Activity implements CordovaInterface {
 
         // If errorUrl specified, then load it
         final String errorUrl = preferences.getString("errorUrl", null);
-        if ((errorUrl != null) && (errorUrl.startsWith("file://") || internalWhitelist.isUrlWhiteListed(errorUrl)) && (!failingUrl.equals(errorUrl))) {
+        Boolean shouldAllowNavigation = (appView != null && appView.getPluginManager().shouldAllowNavigation(errorUrl));
+        if ((errorUrl != null) &&
+            (
+                errorUrl.startsWith("file://") ||
+                (shouldAllowNavigation != null && shouldAllowNavigation == true)
+            ) && (!failingUrl.equals(errorUrl))
+           ) {
             // Load URL on UI thread
             me.runOnUiThread(new Runnable() {
                 public void run() {

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/972540f4/framework/src/org/apache/cordova/CordovaUriHelper.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaUriHelper.java b/framework/src/org/apache/cordova/CordovaUriHelper.java
index 6c1c4fa..89e07a2 100644
--- a/framework/src/org/apache/cordova/CordovaUriHelper.java
+++ b/framework/src/org/apache/cordova/CordovaUriHelper.java
@@ -23,6 +23,7 @@ import android.annotation.TargetApi;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Build;
+import android.util.Log;
 import android.webkit.WebView;
 
 public class CordovaUriHelper {
@@ -37,11 +38,54 @@ public class CordovaUriHelper {
         appView = webView;
         cordova = cdv;
     }
-    
+
+    /**
+     * Determine whether the webview should be allowed to navigate to a given URL.
+     *
+     * This method implements the default whitelist policy when no plugins override
+     * shouldAllowNavigation
+     */
+    public boolean shouldAllowNavigation(String url) {
+        Boolean pluginManagerAllowsNavigation = this.appView.getPluginManager().shouldAllowNavigation(url);
+        if (pluginManagerAllowsNavigation == null) {
+            // Default policy:
+            // Internal urls on file:// or data:// that do not contain "app_webview" are allowed for navigation
+            if(url.startsWith("file://") || url.startsWith("data:"))
+            {
+                //This directory on WebKit/Blink based webviews contains SQLite databases!
+                //DON'T CHANGE THIS UNLESS YOU KNOW WHAT YOU'RE DOING!
+                return !url.contains("app_webview");
+            }
+            return false;
+        }
+        return pluginManagerAllowsNavigation;
+    }
+
+    /**
+     * Determine whether the webview should be allowed to launch an intent for a given URL.
+     *
+     * This method implements the default whitelist policy when no plugins override
+     * shouldOpenExternalUrl
+     */
+    public boolean shouldOpenExternalUrl(String url) {
+        Boolean pluginManagerAllowsExternalUrl = this.appView.getPluginManager().shouldOpenExternalUrl(url);
+        if (pluginManagerAllowsExternalUrl == null) {
+            // Default policy:
+            // External URLs are not allowed
+            return false;
+        }
+        return pluginManagerAllowsExternalUrl;
+    }
+
     /**
      * Give the host application a chance to take over the control when a new url
      * is about to be loaded in the current WebView.
      *
+     * This method implements the default whitelist policy when no plugins override
+     * the whitelist methods:
+     *   Internal urls on file:// or data:// that do not contain "app_webview" are allowed for navigation
+     *   External urls are not allowed.
+     *
      * @param view          The WebView that is initiating the callback.
      * @param url           The url to be loaded.
      * @return              true to override, false for default behavior
@@ -49,23 +93,15 @@ public class CordovaUriHelper {
     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
     public boolean shouldOverrideUrlLoading(String url) {
         // Give plugins the chance to handle the url
-        if (this.appView.getPluginManager().onOverrideUrlLoading(url)) {
-            // Do nothing other than what the plugins wanted.
-            // If any returned true, then the request was handled.
-            return true;
-        }
-        else if(url.startsWith("file://") | url.startsWith("data:"))
-        {
-            //This directory on WebKit/Blink based webviews contains SQLite databases!
-            //DON'T CHANGE THIS UNLESS YOU KNOW WHAT YOU'RE DOING!
-            return url.contains("app_webview");
-        }
-        else if (appView.getWhitelist().isUrlWhiteListed(url)) {
+        if (shouldAllowNavigation(url)) {
             // Allow internal navigation
             return false;
         }
-        else if (appView.getExternalWhitelist().isUrlWhiteListed(url))
-        {
+        if (shouldOpenExternalUrl(url)) {
+            // Do nothing other than what the plugins wanted.
+            // If any returned false, then the request was either blocked
+            // completely, or handled out-of-band by the plugin. If they all
+            // returned true, then we should open the URL here.
             try {
                 Intent intent = new Intent(Intent.ACTION_VIEW);
                 intent.setData(Uri.parse(url));
@@ -77,10 +113,11 @@ public class CordovaUriHelper {
                 this.cordova.getActivity().startActivity(intent);
                 return true;
             } catch (android.content.ActivityNotFoundException e) {
-                LOG.e(TAG, "Error loading url " + url, e);
+                Log.e(TAG, "Error loading url " + url, e);
             }
+            return true;
         }
-        // Intercept the request and do nothing with it -- block it
+        // Block by default
         return true;
     }
 }

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/972540f4/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java
index 68f8741..fbe048d 100644
--- a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java
+++ b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java
@@ -72,8 +72,14 @@ public class IceCreamCordovaWebViewClient extends AndroidWebViewClient {
     }
 
     private boolean isUrlHarmful(String url) {
-        return ((url.startsWith("http:") || url.startsWith("https:")) && !appView.getWhitelist().isUrlWhiteListed(url))
-            || url.contains("app_webview");
+        Boolean shouldAllowRequest = !appView.getPluginManager().shouldAllowRequest(url);
+        if (shouldAllowRequest == null || shouldAllowRequest == true) {
+            // This is the behaviour when all plugins return true, and is also the default behaviour
+            // if no plugins override shouldAllowRequest()
+            return ((url.startsWith("file://") || url.startsWith("data:")) && url.contains("app_webview"));
+        } else {
+            return true;
+        }
     }
 
     private static boolean needsKitKatContentUrlFix(Uri uri) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org