From 67c00e2b18994c78433b4b35b7c9e69fa24277b0 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Wed, 6 Mar 2024 18:26:10 +0100 Subject: [PATCH] fix(android): prevent crash on script injection if the script is too long (#7316) Co-authored-by: Alex Ryltsov <3608222+ryaa@users.noreply.github.com> Co-authored-by: Mark Anderson --- .../src/main/java/com/getcapacitor/JSInjector.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/JSInjector.java b/android/capacitor/src/main/java/com/getcapacitor/JSInjector.java index 5f030a1a7..a3871f7bd 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/JSInjector.java +++ b/android/capacitor/src/main/java/com/getcapacitor/JSInjector.java @@ -69,12 +69,17 @@ public String getScriptString() { * @return */ public InputStream getInjectedStream(InputStream responseStream) { - String js = ""; + String js = ""; String html = this.readAssetStream(responseStream); + + // Insert the js string at the position after or before using StringBuilder + StringBuilder modifiedHtml = new StringBuilder(html); if (html.contains("")) { - html = html.replaceFirst("", "\n" + js + "\n"); + modifiedHtml.insert(html.indexOf("") + "".length(), "\n" + js + "\n"); + html = modifiedHtml.toString(); } else if (html.contains("")) { - html = html.replaceFirst("", js + "\n" + ""); + modifiedHtml.insert(html.indexOf(""), "\n" + js + "\n"); + html = modifiedHtml.toString(); } else { Logger.error("Unable to inject Capacitor, Plugins won't work"); }