Skip to content

Commit

Permalink
Merge pull request #143 from wordpress-mobile/fix/getPhotonImageUrlIn…
Browse files Browse the repository at this point in the history
…dexOutOfBounds

Fallback to original URL if the beginIndex is invalid
  • Loading branch information
Antonis Lilis authored Mar 14, 2024
2 parents 77d3054 + e6b28eb commit 4d6cb9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,12 @@ public void getPhotonImageUrlUsesSslOnHttpsImageUrl() {

assertThat(photonUrl, equalTo("https://i0.wp.com/mysite.com/test.jpg?strip=info&quality=65&resize=2,1&ssl=1"));
}

@Test
public void getPhotonImageUrlWithErroneousSchemePosition() {
String imageUrl = "mysite.com/test.jpg#http://another.com";
String photonUrl = PhotonUtils.getPhotonImageUrl(imageUrl, 1, 1);

assertThat(photonUrl, equalTo(imageUrl));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q
return "";
}

String originalUrl = imageUrl;

// make sure it's valid
int schemePos = imageUrl.indexOf("://");
if (schemePos == -1) {
Expand Down Expand Up @@ -138,6 +140,12 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q
query += "&ssl=1";
}

return "https://i0.wp.com/" + imageUrl.substring(schemePos + 3, imageUrl.length()) + query;
int beginIndex = schemePos + 3;
if (beginIndex < 0 || beginIndex > imageUrl.length()) {
// Fallback to original URL if the beginIndex is invalid to avoid `StringIndexOutOfBoundsException`
// Ref: https://github.com/wordpress-mobile/WordPress-Android/issues/18626
return originalUrl;
}
return "https://i0.wp.com/" + imageUrl.substring(beginIndex) + query;
}
}

0 comments on commit 4d6cb9a

Please sign in to comment.