@@ -269,4 +269,174 @@ class BitmapUtilsTest {
269269 assertTrue(e.message?.contains(" JPEG" ) == true )
270270 }
271271 }
272+
273+ // ==================== Additional Security Edge Cases ====================
274+
275+ @Test
276+ fun `WHEN URI with path traversal in query params THEN accept if path is valid` () {
277+ // GIVEN - URI with path traversal in query string (query params are ignored)
278+ val uriWithQueryParams = Uri .parse(" content://provider/image.jpg?path=../../secret.xml" )
279+ val compressFormat = Bitmap .CompressFormat .JPEG
280+
281+ // WHEN & THEN - Should pass since path ends with .jpg (function checks path, not query)
282+ // Query parameters are not part of security validation
283+ BitmapUtils .validateOutputUri(uriWithQueryParams, compressFormat)
284+ // No exception expected - test passes if no exception thrown
285+ }
286+
287+ @Test(expected = SecurityException ::class )
288+ fun `WHEN URI has null scheme THEN throw SecurityException` () {
289+ // GIVEN
290+ val nullSchemeUri = Uri .parse(" //provider/images/image.jpg" )
291+ val compressFormat = Bitmap .CompressFormat .JPEG
292+
293+ // WHEN
294+ BitmapUtils .validateOutputUri(nullSchemeUri, compressFormat)
295+
296+ // THEN - SecurityException expected
297+ }
298+
299+ @Test(expected = SecurityException ::class )
300+ fun `WHEN URI has empty scheme THEN throw SecurityException` () {
301+ // GIVEN
302+ val uri = Uri .parse(" provider/images/image.jpg" ) // No scheme specified
303+ val compressFormat = Bitmap .CompressFormat .JPEG
304+
305+ // WHEN
306+ BitmapUtils .validateOutputUri(uri, compressFormat)
307+
308+ // THEN - SecurityException expected
309+ }
310+
311+ @Test(expected = SecurityException ::class )
312+ fun `WHEN URI has http scheme THEN throw SecurityException` () {
313+ // GIVEN - Network URI should not be allowed
314+ val httpUri = Uri .parse(" http://malicious.com/upload/image.jpg" )
315+ val compressFormat = Bitmap .CompressFormat .JPEG
316+
317+ // WHEN
318+ BitmapUtils .validateOutputUri(httpUri, compressFormat)
319+
320+ // THEN - SecurityException expected (only content:// allowed)
321+ }
322+
323+ @Test(expected = SecurityException ::class )
324+ fun `WHEN URI has https scheme THEN throw SecurityException` () {
325+ // GIVEN
326+ val httpsUri = Uri .parse(" https://example.com/image.png" )
327+ val compressFormat = Bitmap .CompressFormat .PNG
328+
329+ // WHEN
330+ BitmapUtils .validateOutputUri(httpsUri, compressFormat)
331+
332+ // THEN - SecurityException expected
333+ }
334+
335+ @Test(expected = SecurityException ::class )
336+ fun `WHEN content URI has uppercase extension mismatch THEN throw SecurityException` () {
337+ // GIVEN - Extension is .PNG but format is JPEG
338+ val contentUri = Uri .parse(" content://com.example.provider/images/IMAGE.PNG" )
339+ val compressFormat = Bitmap .CompressFormat .JPEG
340+
341+ // WHEN
342+ BitmapUtils .validateOutputUri(contentUri, compressFormat)
343+
344+ // THEN - SecurityException expected
345+ }
346+
347+ @Test
348+ fun `WHEN content URI has mixed case jpg extension THEN validation passes` () {
349+ // GIVEN - Mixed case should work (case-insensitive check)
350+ val contentUri = Uri .parse(" content://com.example.provider/images/image.JpG" )
351+ val compressFormat = Bitmap .CompressFormat .JPEG
352+
353+ // WHEN & THEN - No exception should be thrown
354+ BitmapUtils .validateOutputUri(contentUri, compressFormat)
355+ }
356+
357+ @Test(expected = SecurityException ::class )
358+ fun `WHEN webp URI with wrong format THEN throw SecurityException` () {
359+ // GIVEN
360+ val webpUri = Uri .parse(" content://provider/image.webp" )
361+ val compressFormat = Bitmap .CompressFormat .JPEG
362+
363+ // WHEN
364+ BitmapUtils .validateOutputUri(webpUri, compressFormat)
365+
366+ // THEN - SecurityException expected
367+ }
368+
369+ @Test
370+ fun `WHEN valid webp URI with WEBP format THEN validation passes` () {
371+ // GIVEN
372+ val webpUri = Uri .parse(" content://provider/image.webp" )
373+ val compressFormat = Bitmap .CompressFormat .WEBP
374+
375+ // WHEN & THEN - No exception
376+ BitmapUtils .validateOutputUri(webpUri, compressFormat)
377+ }
378+
379+ @Test(expected = SecurityException ::class )
380+ fun `WHEN URI has double extension THEN validate by last extension` () {
381+ // GIVEN - File has .jpg.png extension
382+ val doubleExtUri = Uri .parse(" content://provider/image.jpg.png" )
383+ val compressFormat = Bitmap .CompressFormat .JPEG
384+
385+ // WHEN
386+ BitmapUtils .validateOutputUri(doubleExtUri, compressFormat)
387+
388+ // THEN - Should fail because last extension is .png, not .jpg
389+ }
390+
391+ @Test
392+ fun `WHEN URI has no path THEN validation checks URI string` () {
393+ // GIVEN - URI with no path, just authority
394+ val noPathUri = Uri .parse(" content://provider" )
395+ val compressFormat = Bitmap .CompressFormat .JPEG
396+
397+ // WHEN
398+ try {
399+ BitmapUtils .validateOutputUri(noPathUri, compressFormat)
400+ throw AssertionError (" Expected SecurityException" )
401+ } catch (e: SecurityException ) {
402+ // THEN - Should fail validation (no valid extension)
403+ assertTrue(e.message?.contains(" extension" ) == true )
404+ }
405+ }
406+
407+ @Test(expected = SecurityException ::class )
408+ fun `WHEN URI has suspicious executable extension THEN throw SecurityException` () {
409+ // GIVEN - Dangerous extension disguised as content URI
410+ val exeUri = Uri .parse(" content://provider/malware.exe" )
411+ val compressFormat = Bitmap .CompressFormat .JPEG
412+
413+ // WHEN
414+ BitmapUtils .validateOutputUri(exeUri, compressFormat)
415+
416+ // THEN - SecurityException expected (doesn't match image format)
417+ }
418+
419+ @Test(expected = SecurityException ::class )
420+ fun `WHEN URI points to SharedPreferences XML THEN throw SecurityException` () {
421+ // GIVEN - Attempt to write to SharedPreferences file
422+ val sharedPrefsUri = Uri .parse(" content://provider/shared_prefs/preferences.xml" )
423+ val compressFormat = Bitmap .CompressFormat .JPEG
424+
425+ // WHEN
426+ BitmapUtils .validateOutputUri(sharedPrefsUri, compressFormat)
427+
428+ // THEN - SecurityException expected (.xml doesn't match .jpg/.jpeg)
429+ }
430+
431+ @Test(expected = SecurityException ::class )
432+ fun `WHEN URI has encoded path traversal THEN still validate extension` () {
433+ // GIVEN - URL encoded path traversal
434+ val encodedUri = Uri .parse(" content://provider/images/%2E%2E%2Fsecret.xml" )
435+ val compressFormat = Bitmap .CompressFormat .PNG
436+
437+ // WHEN
438+ BitmapUtils .validateOutputUri(encodedUri, compressFormat)
439+
440+ // THEN - Should reject due to .xml extension
441+ }
272442}
0 commit comments