Skip to content

Commit

Permalink
fix(android): prevent crash on script injection if the script is too …
Browse files Browse the repository at this point in the history
…long (#7308)

Co-authored-by: Mark Anderson <mark@ionic.io>
Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
  • Loading branch information
3 people committed Mar 6, 2024
1 parent 663e7b3 commit c9895e8
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions android/capacitor/src/main/java/com/getcapacitor/JSInjector.java
Expand Up @@ -69,12 +69,17 @@ public String getScriptString() {
* @return
*/
public InputStream getInjectedStream(InputStream responseStream) {
String js = "<script type=\"text/javascript\">" + getScriptString().replace("${", "\\${") + "</script>";
String js = "<script type=\"text/javascript\">" + getScriptString() + "</script>";
String html = this.readAssetStream(responseStream);

// Insert the js string at the position after <head> or before </head> using StringBuilder
StringBuilder modifiedHtml = new StringBuilder(html);
if (html.contains("<head>")) {
html = html.replaceFirst("<head>", "<head>\n" + js + "\n");
modifiedHtml.insert(html.indexOf("<head>") + "<head>".length(), "\n" + js + "\n");
html = modifiedHtml.toString();
} else if (html.contains("</head>")) {
html = html.replaceFirst("</head>", js + "\n" + "</head>");
modifiedHtml.insert(html.indexOf("</head>"), "\n" + js + "\n");
html = modifiedHtml.toString();
} else {
Logger.error("Unable to inject Capacitor, Plugins won't work");
}
Expand Down

0 comments on commit c9895e8

Please sign in to comment.