Skip to content

Commit 44d9bb0

Browse files
committed
InAppBrowser.java: New method isURLWhileListed to check for whitelisting.
Newtest in shouldOverrideUrlLoading, to allow whitelisted custom schemes like"mycoolapp://" inappbrowser.js: Added "customscheme" channel.
1 parent 3f0528c commit 44d9bb0

File tree

2 files changed

+63
-26
lines changed

2 files changed

+63
-26
lines changed

src/android/InAppBrowser.java

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,33 +170,10 @@ public void run() {
170170
Boolean shouldAllowNavigation = null;
171171
if (url.startsWith("javascript:")) {
172172
shouldAllowNavigation = true;
173+
} else {
174+
shouldAllowNavigation = isURLWhiteListed(url);
173175
}
174-
if (shouldAllowNavigation == null) {
175-
try {
176-
Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class);
177-
shouldAllowNavigation = (Boolean)iuw.invoke(null, url);
178-
} catch (NoSuchMethodException e) {
179-
LOG.d(LOG_TAG, e.getLocalizedMessage());
180-
} catch (IllegalAccessException e) {
181-
LOG.d(LOG_TAG, e.getLocalizedMessage());
182-
} catch (InvocationTargetException e) {
183-
LOG.d(LOG_TAG, e.getLocalizedMessage());
184-
}
185-
}
186-
if (shouldAllowNavigation == null) {
187-
try {
188-
Method gpm = webView.getClass().getMethod("getPluginManager");
189-
PluginManager pm = (PluginManager)gpm.invoke(webView);
190-
Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class);
191-
shouldAllowNavigation = (Boolean)san.invoke(pm, url);
192-
} catch (NoSuchMethodException e) {
193-
LOG.d(LOG_TAG, e.getLocalizedMessage());
194-
} catch (IllegalAccessException e) {
195-
LOG.d(LOG_TAG, e.getLocalizedMessage());
196-
} catch (InvocationTargetException e) {
197-
LOG.d(LOG_TAG, e.getLocalizedMessage());
198-
}
199-
}
176+
200177
// load in webview
201178
if (Boolean.TRUE.equals(shouldAllowNavigation)) {
202179
LOG.d(LOG_TAG, "loading in webview");
@@ -302,6 +279,47 @@ public void run() {
302279
return true;
303280
}
304281

282+
/**
283+
* Is the URL or Scheme WhiteListed
284+
* This code exists for compatibility between 3.x and 4.x versions of Cordova.
285+
* Previously the Config class had a static method, isUrlWhitelisted(). That
286+
* responsibility has been moved to the plugins, with an aggregating method in
287+
* PluginManager.
288+
*/
289+
* @param url, the URL as a String
290+
* @return true if WhiteListed, otherwise null or false
291+
*/
292+
private Boolean isURLWhiteListed(String url) {
293+
Boolean shouldAllowNavigation = null;
294+
if (shouldAllowNavigation == null) {
295+
try {
296+
Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class);
297+
shouldAllowNavigation = (Boolean)iuw.invoke(null, url);
298+
} catch (NoSuchMethodException e) {
299+
LOG.d(LOG_TAG, e.getLocalizedMessage());
300+
} catch (IllegalAccessException e) {
301+
LOG.d(LOG_TAG, e.getLocalizedMessage());
302+
} catch (InvocationTargetException e) {
303+
LOG.d(LOG_TAG, e.getLocalizedMessage());
304+
}
305+
}
306+
if (shouldAllowNavigation == null) {
307+
try {
308+
Method gpm = webView.getClass().getMethod("getPluginManager");
309+
PluginManager pm = (PluginManager)gpm.invoke(webView);
310+
Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class);
311+
shouldAllowNavigation = (Boolean)san.invoke(pm, url);
312+
} catch (NoSuchMethodException e) {
313+
LOG.d(LOG_TAG, e.getLocalizedMessage());
314+
} catch (IllegalAccessException e) {
315+
LOG.d(LOG_TAG, e.getLocalizedMessage());
316+
} catch (InvocationTargetException e) {
317+
LOG.d(LOG_TAG, e.getLocalizedMessage());
318+
}
319+
}
320+
return shouldAllowNavigation;
321+
}
322+
305323
/**
306324
* Called when the view navigates.
307325
*/
@@ -1110,6 +1128,24 @@ else if (url.startsWith("sms:")) {
11101128
LOG.e(LOG_TAG, "Error sending sms " + url + ":" + e.toString());
11111129
}
11121130
}
1131+
// Test for whitelisted custom scheme names, less than 20 chars long, like mycoolapp: or twitteroauthresponse: (Twitter Oauth Response)
1132+
else if (url.matches("^[a-z]{0,20}://.*?$")) {
1133+
if (Boolean.TRUE.equals(isURLWhiteListed(url))) {
1134+
try {
1135+
LOG.w("STEVE IN InAppBrowser.java, whiteliste url SUCCESS: ", url );
1136+
JSONObject obj = new JSONObject();
1137+
obj.put("type", "customscheme");
1138+
obj.put("url", url);
1139+
sendUpdate(obj, true);
1140+
return true;
1141+
} catch (JSONException ex) {
1142+
LOG.e(LOG_TAG, "Custom Scheme URI passed in has caused a JSON error.");
1143+
}
1144+
} else {
1145+
LOG.w("STEVE IN InAppBrowser.java, whitelisted url FAILURE: ", url );
1146+
}
1147+
}
1148+
11131149
return false;
11141150
}
11151151

www/inappbrowser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
'loadstart': channel.create('loadstart'),
3737
'loadstop': channel.create('loadstop'),
3838
'loaderror': channel.create('loaderror'),
39+
'customscheme': channel.create('customscheme'),
3940
'exit': channel.create('exit')
4041
};
4142
}

0 commit comments

Comments
 (0)