Skip to content

Commit 4d6cb9a

Browse files
authored
Merge pull request #143 from wordpress-mobile/fix/getPhotonImageUrlIndexOutOfBounds
Fallback to original URL if the beginIndex is invalid
2 parents 77d3054 + e6b28eb commit 4d6cb9a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,12 @@ public void getPhotonImageUrlUsesSslOnHttpsImageUrl() {
103103

104104
assertThat(photonUrl, equalTo("https://i0.wp.com/mysite.com/test.jpg?strip=info&quality=65&resize=2,1&ssl=1"));
105105
}
106+
107+
@Test
108+
public void getPhotonImageUrlWithErroneousSchemePosition() {
109+
String imageUrl = "mysite.com/test.jpg#http://another.com";
110+
String photonUrl = PhotonUtils.getPhotonImageUrl(imageUrl, 1, 1);
111+
112+
assertThat(photonUrl, equalTo(imageUrl));
113+
}
106114
}

WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q
5252
return "";
5353
}
5454

55+
String originalUrl = imageUrl;
56+
5557
// make sure it's valid
5658
int schemePos = imageUrl.indexOf("://");
5759
if (schemePos == -1) {
@@ -138,6 +140,12 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q
138140
query += "&ssl=1";
139141
}
140142

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

0 commit comments

Comments
 (0)