From 062cd80461185eedfe2721ab6c33b78e226ee9eb Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Tue, 6 Aug 2024 19:34:34 +0200 Subject: [PATCH] fix(android): better cleaning of allowedOrigin url --- .../src/main/java/com/getcapacitor/Bridge.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index bcff4682e..594146a0f 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -259,17 +259,13 @@ private void loadWebView() { // Start the local web server JSInjector injector = getJSInjector(); if (WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) { - String allowedOrigin = appUrl; - Uri appUri = Uri.parse(appUrl).buildUpon().clearQuery().build(); - if (appUri.getPath() != null) { - if (appUri.getPath().equals("/")) { - allowedOrigin = appUrl.substring(0, appUrl.length() - 1); - } else { - allowedOrigin = appUri.toString().replace(appUri.getPath(), ""); - } + String allowedOrigin = Uri.parse(appUrl).buildUpon().path(null).fragment(null).clearQuery().build().toString(); + try { + WebViewCompat.addDocumentStartJavaScript(webView, injector.getScriptString(), Collections.singleton(allowedOrigin)); + injector = null; + } catch (IllegalArgumentException ex) { + Logger.warn("Invalid url, using fallback"); } - WebViewCompat.addDocumentStartJavaScript(webView, injector.getScriptString(), Collections.singleton(allowedOrigin)); - injector = null; } localServer = new WebViewLocalServer(context, this, injector, authorities, html5mode); localServer.hostAssets(DEFAULT_WEB_ASSET_DIR);