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 (#7316)

Co-authored-by: Alex Ryltsov <3608222+ryaa@users.noreply.github.com>
Co-authored-by: Mark Anderson <mark@ionic.io>
  • Loading branch information
3 people authored Mar 6, 2024
1 parent 28e7f08 commit 67c00e2
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
Original file line number Diff line number Diff line change
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 67c00e2

Please sign in to comment.