CB-7912: Update to work with whitelist plugins in Cordova 4.x

This commit is contained in:
Ian Clelland
2014-10-30 15:32:37 -04:00
parent aebe887ffe
commit e17e8f01f5

View File

@@ -55,6 +55,7 @@ import org.apache.cordova.CordovaResourceApi;
import org.apache.cordova.CordovaResourceApi.OpenForReadResult;
import org.apache.cordova.PluginManager;
import org.apache.cordova.PluginResult;
import org.apache.cordova.Whitelist;
import org.apache.cordova.file.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
@@ -687,9 +688,39 @@ public class FileTransfer extends CordovaPlugin {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
return;
}
// TODO: refactor to also allow resources & content:
if (!isLocalTransfer && !Config.isUrlWhiteListed(source)) {
/* This code exists for compatibility between 3.x and 4.x versions of Cordova.
* Previously the CordovaWebView class had a method, getWhitelist, which would
* return a Whitelist object. Since the fixed whitelist is removed in Cordova 4.x,
* the correct call now is to shouldAllowRequest from the plugin manager.
*/
Boolean shouldAllowRequest = null;
if (isLocalTransfer) {
shouldAllowRequest = true;
}
if (shouldAllowRequest == null) {
try {
Method gwl = webView.getClass().getMethod("getWhitelist");
Whitelist whitelist = (Whitelist)gwl.invoke(webView);
shouldAllowRequest = whitelist.isUrlWhiteListed(source);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
if (shouldAllowRequest == null) {
try {
Method gpm = webView.getClass().getMethod("getPluginManager");
PluginManager pm = (PluginManager)gpm.invoke(webView);
Method san = pm.getClass().getMethod("shouldAllowRequest", String.class);
shouldAllowRequest = (Boolean)san.invoke(pm, source);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
if (!Boolean.TRUE.equals(shouldAllowRequest)) {
Log.w(LOG_TAG, "Source URL is not in white list: '" + source + "'");
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, null, 401, null);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));