From 44d9bb0f6a4deff139a1968479030664bf49eff2 Mon Sep 17 00:00:00 2001 From: stevepodell Date: Wed, 4 Apr 2018 08:22:23 -0700 Subject: [PATCH] InAppBrowser.java: New method isURLWhileListed to check for whitelisting. Newtest in shouldOverrideUrlLoading, to allow whitelisted custom schemes like"mycoolapp://" inappbrowser.js: Added "customscheme" channel. --- src/android/InAppBrowser.java | 88 ++++++++++++++++++++++++----------- www/inappbrowser.js | 1 + 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 2b0dbe0..3a03057 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -170,33 +170,10 @@ public class InAppBrowser extends CordovaPlugin { Boolean shouldAllowNavigation = null; if (url.startsWith("javascript:")) { shouldAllowNavigation = true; + } else { + shouldAllowNavigation = isURLWhiteListed(url); } - if (shouldAllowNavigation == null) { - try { - Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class); - shouldAllowNavigation = (Boolean)iuw.invoke(null, url); - } catch (NoSuchMethodException e) { - LOG.d(LOG_TAG, e.getLocalizedMessage()); - } catch (IllegalAccessException e) { - LOG.d(LOG_TAG, e.getLocalizedMessage()); - } catch (InvocationTargetException e) { - LOG.d(LOG_TAG, e.getLocalizedMessage()); - } - } - if (shouldAllowNavigation == null) { - try { - Method gpm = webView.getClass().getMethod("getPluginManager"); - PluginManager pm = (PluginManager)gpm.invoke(webView); - Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class); - shouldAllowNavigation = (Boolean)san.invoke(pm, url); - } catch (NoSuchMethodException e) { - LOG.d(LOG_TAG, e.getLocalizedMessage()); - } catch (IllegalAccessException e) { - LOG.d(LOG_TAG, e.getLocalizedMessage()); - } catch (InvocationTargetException e) { - LOG.d(LOG_TAG, e.getLocalizedMessage()); - } - } + // load in webview if (Boolean.TRUE.equals(shouldAllowNavigation)) { LOG.d(LOG_TAG, "loading in webview"); @@ -302,6 +279,47 @@ public class InAppBrowser extends CordovaPlugin { return true; } + /** + * Is the URL or Scheme WhiteListed + * This code exists for compatibility between 3.x and 4.x versions of Cordova. + * Previously the Config class had a static method, isUrlWhitelisted(). That + * responsibility has been moved to the plugins, with an aggregating method in + * PluginManager. + */ + * @param url, the URL as a String + * @return true if WhiteListed, otherwise null or false + */ + private Boolean isURLWhiteListed(String url) { + Boolean shouldAllowNavigation = null; + if (shouldAllowNavigation == null) { + try { + Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class); + shouldAllowNavigation = (Boolean)iuw.invoke(null, url); + } catch (NoSuchMethodException e) { + LOG.d(LOG_TAG, e.getLocalizedMessage()); + } catch (IllegalAccessException e) { + LOG.d(LOG_TAG, e.getLocalizedMessage()); + } catch (InvocationTargetException e) { + LOG.d(LOG_TAG, e.getLocalizedMessage()); + } + } + if (shouldAllowNavigation == null) { + try { + Method gpm = webView.getClass().getMethod("getPluginManager"); + PluginManager pm = (PluginManager)gpm.invoke(webView); + Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class); + shouldAllowNavigation = (Boolean)san.invoke(pm, url); + } catch (NoSuchMethodException e) { + LOG.d(LOG_TAG, e.getLocalizedMessage()); + } catch (IllegalAccessException e) { + LOG.d(LOG_TAG, e.getLocalizedMessage()); + } catch (InvocationTargetException e) { + LOG.d(LOG_TAG, e.getLocalizedMessage()); + } + } + return shouldAllowNavigation; + } + /** * Called when the view navigates. */ @@ -1110,6 +1128,24 @@ public class InAppBrowser extends CordovaPlugin { LOG.e(LOG_TAG, "Error sending sms " + url + ":" + e.toString()); } } + // Test for whitelisted custom scheme names, less than 20 chars long, like mycoolapp: or twitteroauthresponse: (Twitter Oauth Response) + else if (url.matches("^[a-z]{0,20}://.*?$")) { + if (Boolean.TRUE.equals(isURLWhiteListed(url))) { + try { + LOG.w("STEVE IN InAppBrowser.java, whiteliste url SUCCESS: ", url ); + JSONObject obj = new JSONObject(); + obj.put("type", "customscheme"); + obj.put("url", url); + sendUpdate(obj, true); + return true; + } catch (JSONException ex) { + LOG.e(LOG_TAG, "Custom Scheme URI passed in has caused a JSON error."); + } + } else { + LOG.w("STEVE IN InAppBrowser.java, whitelisted url FAILURE: ", url ); + } + } + return false; } diff --git a/www/inappbrowser.js b/www/inappbrowser.js index 7c3e749..08f96ab 100644 --- a/www/inappbrowser.js +++ b/www/inappbrowser.js @@ -36,6 +36,7 @@ 'loadstart': channel.create('loadstart'), 'loadstop': channel.create('loadstop'), 'loaderror': channel.create('loaderror'), + 'customscheme': channel.create('customscheme'), 'exit': channel.create('exit') }; }