From cc8c040c6168ab5b5ac48eae57037ebd2dd791b5 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Wed, 13 Mar 2024 16:49:05 +0200 Subject: [PATCH 1/3] Fallback to original URL if the beginIndex is invalid --- .../main/java/org/wordpress/android/util/PhotonUtils.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java index 2f12eb05f..09034e45b 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java @@ -138,6 +138,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 + // Ref: https://github.com/wordpress-mobile/WordPress-Android/issues/18626 + return imageUrl; + } + return "https://i0.wp.com/" + imageUrl.substring(beginIndex) + query; } } From c47b21325359b3a7fe34c786ec6644ad46e80041 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Wed, 13 Mar 2024 19:30:06 +0200 Subject: [PATCH 2/3] Adds test case that reproduces the issue --- .../java/org/wordpress/android/util/PhotonUtilsTest.java | 8 ++++++++ .../main/java/org/wordpress/android/util/PhotonUtils.java | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java index 0e2d4b301..cf86cc823 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java @@ -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("mysite.com/test.jpg")); + } } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java index 09034e45b..c618263d2 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java @@ -140,7 +140,7 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q int beginIndex = schemePos + 3; if (beginIndex < 0 || beginIndex > imageUrl.length()) { - // Fallback to original URL if the beginIndex is invalid + // Fallback to original URL if the beginIndex is invalid to avoid `StringIndexOutOfBoundsException` // Ref: https://github.com/wordpress-mobile/WordPress-Android/issues/18626 return imageUrl; } From e6b28eb2feacaf27fcb2196073be52688ce72e62 Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Thu, 14 Mar 2024 08:47:27 +0200 Subject: [PATCH 3/3] Return the original url before any mutations as a fallback in the error case --- .../java/org/wordpress/android/util/PhotonUtilsTest.java | 2 +- .../src/main/java/org/wordpress/android/util/PhotonUtils.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java index cf86cc823..c6f3ee743 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/PhotonUtilsTest.java @@ -109,6 +109,6 @@ public void getPhotonImageUrlWithErroneousSchemePosition() { String imageUrl = "mysite.com/test.jpg#http://another.com"; String photonUrl = PhotonUtils.getPhotonImageUrl(imageUrl, 1, 1); - assertThat(photonUrl, equalTo("mysite.com/test.jpg")); + assertThat(photonUrl, equalTo(imageUrl)); } } diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java index c618263d2..e54d346f8 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/PhotonUtils.java @@ -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) { @@ -142,7 +144,7 @@ public static String getPhotonImageUrl(String imageUrl, int width, int height, Q 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 imageUrl; + return originalUrl; } return "https://i0.wp.com/" + imageUrl.substring(beginIndex) + query; }