Skip to content

Commit

Permalink
fix(PluginManager): AllowBridgeAccess default policy to handle scheme…
Browse files Browse the repository at this point in the history
… & hostname (apache#1332)
  • Loading branch information
erisu authored and wedgberto committed May 17, 2022
1 parent 94d5435 commit 491af91
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
5 changes: 0 additions & 5 deletions framework/src/org/apache/cordova/AllowListPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ public void handleStartTag(XmlPullParser xml) {
if (strNode.equals("content")) {
String startPage = xml.getAttributeValue(null, "src");
allowedNavigations.addAllowListEntry(startPage, false);

// Allow origin for WebViewAssetLoader
if (!this.prefs.getBoolean("AndroidInsecureFileModeEnabled", false)) {
allowedNavigations.addAllowListEntry("https://" + this.prefs.getString("hostname", "localhost"), false);
}
} else if (strNode.equals("allow-navigation")) {
String origin = xml.getAttributeValue(null, "href");
if ("*".equals(origin)) {
Expand Down
26 changes: 25 additions & 1 deletion framework/src/org/apache/cordova/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Licensed to the Apache Software Foundation (ASF) under one
*/
public class PluginManager {
private static String TAG = "PluginManager";

// @todo same as ConfigXmlParser. Research centralizing ideas, maybe create CordovaConstants
private static String SCHEME_HTTPS = "https";
// @todo same as ConfigXmlParser. Research centralizing ideas, maybe create CordovaConstants
private static String DEFAULT_HOSTNAME = "localhost";

private static final int SLOW_EXEC_WARNING_THRESHOLD = Debug.isDebuggerConnected() ? 60 : 16;

// List of service entries
Expand Down Expand Up @@ -366,6 +372,24 @@ public void onNewIntent(Intent intent) {
}
}

/**
* @todo should we move this somewhere public and accessible by all plugins?
* For now, it is placed where it is used and kept private so we can decide later and move without causing a breaking change.
* An ideal location might be in the "ConfigXmlParser" at the time it generates the "launchUrl".
*
* @todo should we be restrictive on the "file://" return? e.g. "file:///android_asset/www/"
* Would be considered as a breaking change if we apply a more granular check.
*/
private String getLaunchUrlPrefix() {
if (!app.getPreferences().getBoolean("AndroidInsecureFileModeEnabled", false)) {
String scheme = app.getPreferences().getString("scheme", SCHEME_HTTPS).toLowerCase();
String hostname = app.getPreferences().getString("hostname", DEFAULT_HOSTNAME);
return scheme + "://" + hostname + '/';
}

return "file://";
}

/**
* Called when the webview is going to request an external resource.
*
Expand Down Expand Up @@ -452,7 +476,7 @@ public boolean shouldAllowBridgeAccess(String url) {
}

// Default policy:
return url.startsWith("file://");
return url.startsWith(getLaunchUrlPrefix());
}

/**
Expand Down

0 comments on commit 491af91

Please sign in to comment.