Skip to content

Commit 01285ba

Browse files
authored
feat(android): don't allow server.androidScheme to be set to schemes handled by WebView
1 parent e47959f commit 01285ba

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

android/capacitor/src/main/java/com/getcapacitor/CapConfig.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import androidx.annotation.Nullable;
1010
import com.getcapacitor.util.JSONUtils;
1111
import java.io.IOException;
12+
import java.util.Arrays;
1213
import java.util.HashMap;
1314
import java.util.Iterator;
15+
import java.util.List;
1416
import java.util.Locale;
1517
import java.util.Map;
1618
import org.json.JSONException;
@@ -105,7 +107,11 @@ private CapConfig(Builder builder) {
105107
this.html5mode = builder.html5mode;
106108
this.serverUrl = builder.serverUrl;
107109
this.hostname = builder.hostname;
108-
this.androidScheme = builder.androidScheme;
110+
111+
if (this.validateScheme(builder.androidScheme)) {
112+
this.androidScheme = builder.androidScheme;
113+
}
114+
109115
this.allowNavigation = builder.allowNavigation;
110116

111117
// Android Config
@@ -148,7 +154,12 @@ private void deserializeConfig(@Nullable Context context) {
148154
html5mode = JSONUtils.getBoolean(configJSON, "server.html5mode", html5mode);
149155
serverUrl = JSONUtils.getString(configJSON, "server.url", null);
150156
hostname = JSONUtils.getString(configJSON, "server.hostname", hostname);
151-
androidScheme = JSONUtils.getString(configJSON, "server.androidScheme", androidScheme);
157+
158+
String configSchema = JSONUtils.getString(configJSON, "server.androidScheme", androidScheme);
159+
if (this.validateScheme(configSchema)) {
160+
androidScheme = configSchema;
161+
}
162+
152163
allowNavigation = JSONUtils.getArray(configJSON, "server.allowNavigation", null);
153164

154165
// Android
@@ -191,6 +202,16 @@ private void deserializeConfig(@Nullable Context context) {
191202
pluginsConfiguration = deserializePluginsConfig(JSONUtils.getObject(configJSON, "plugins"));
192203
}
193204

205+
private boolean validateScheme(String scheme) {
206+
List<String> invalidSchemes = Arrays.asList("file", "ftp", "ftps", "ws", "wss", "about", "blob", "data");
207+
if (invalidSchemes.contains(scheme)) {
208+
Logger.warn(scheme + " is not an allowed scheme. Defaulting to http.");
209+
return false;
210+
}
211+
212+
return true;
213+
}
214+
194215
public boolean isHTML5Mode() {
195216
return html5mode;
196217
}

0 commit comments

Comments
 (0)