Skip to content

Commit 898ae8f

Browse files
authored
fix: restrict KML image downloads to http/https schemes (#1674)
* fix: restrict KML image downloads to http/https schemes and remove local fallback * test: verify URL scheme validation for KML images
1 parent 6b5a2d3 commit 898ae8f

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

library/src/main/java/com/google/maps/android/data/kml/KmlRenderer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,6 @@ public MarkerIconImageDownload(String iconUrl) {
538538
protected Bitmap doInBackground(String... params) {
539539
try {
540540
return getBitmapFromUrl(mIconUrl);
541-
} catch (MalformedURLException e) {
542-
return BitmapFactory.decodeFile(mIconUrl);
543541
} catch (IOException e) {
544542
e.printStackTrace();
545543
}
@@ -588,8 +586,6 @@ public GroundOverlayImageDownload(String groundOverlayUrl) {
588586
protected Bitmap doInBackground(String... params) {
589587
try {
590588
return getBitmapFromUrl(mGroundOverlayUrl);
591-
} catch (MalformedURLException e) {
592-
return BitmapFactory.decodeFile(mGroundOverlayUrl);
593589
} catch (IOException e) {
594590
Log.e(LOG_TAG, "Image [" + mGroundOverlayUrl + "] download issue", e);
595591
}
@@ -621,7 +617,11 @@ protected void onPostExecute(Bitmap bitmap) {
621617
* @return the bitmap of that image, scaled according to screen density.
622618
*/
623619
private Bitmap getBitmapFromUrl(String url) throws IOException {
624-
return BitmapFactory.decodeStream(openConnectionCheckRedirects(new URL(url).openConnection()));
620+
URL parsedUrl = new URL(url);
621+
if (!parsedUrl.getProtocol().equalsIgnoreCase("http") && !parsedUrl.getProtocol().equalsIgnoreCase("https")) {
622+
throw new MalformedURLException("Unsupported scheme: " + parsedUrl.getProtocol());
623+
}
624+
return BitmapFactory.decodeStream(openConnectionCheckRedirects(parsedUrl.openConnection()));
625625
}
626626

627627
/**

library/src/test/java/com/google/maps/android/data/kml/KmlRendererTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,42 @@ public void testAssignStyleMap() {
7777
assertNotNull(styleMap.get("BlueKey"));
7878
assertEquals(styles.get("BlueKey"), redStyle);
7979
}
80+
81+
@Test
82+
public void testBitmapUrlSchemeValidation() throws Exception {
83+
KmlRenderer renderer = new KmlRenderer(null, null, null, null, null, null, null);
84+
java.lang.reflect.Method method = KmlRenderer.class.getDeclaredMethod("getBitmapFromUrl", String.class);
85+
method.setAccessible(true);
86+
87+
// Should throw MalformedURLException for file:// scheme
88+
try {
89+
method.invoke(renderer, "file:///android_asset/image.png");
90+
org.junit.Assert.fail("Should have thrown InvocationTargetException containing MalformedURLException");
91+
} catch (java.lang.reflect.InvocationTargetException e) {
92+
assertTrue(e.getCause() instanceof java.net.MalformedURLException);
93+
assertEquals("Unsupported scheme: file", e.getCause().getMessage());
94+
}
95+
96+
// Should throw MalformedURLException for ftp:// scheme
97+
try {
98+
method.invoke(renderer, "ftp://example.com/image.png");
99+
org.junit.Assert.fail("Should have thrown InvocationTargetException containing MalformedURLException");
100+
} catch (java.lang.reflect.InvocationTargetException e) {
101+
assertTrue(e.getCause() instanceof java.net.MalformedURLException);
102+
assertEquals("Unsupported scheme: ftp", e.getCause().getMessage());
103+
}
104+
105+
// For http/https, it should not throw MalformedURLException with "Unsupported scheme"
106+
try {
107+
method.invoke(renderer, "http://example.com/image.png");
108+
} catch (java.lang.reflect.InvocationTargetException e) {
109+
org.junit.Assert.assertFalse(e.getCause().getMessage() != null && e.getCause().getMessage().startsWith("Unsupported scheme"));
110+
}
111+
112+
try {
113+
method.invoke(renderer, "https://example.com/image.png");
114+
} catch (java.lang.reflect.InvocationTargetException e) {
115+
org.junit.Assert.assertFalse(e.getCause().getMessage() != null && e.getCause().getMessage().startsWith("Unsupported scheme"));
116+
}
117+
}
80118
}

0 commit comments

Comments
 (0)