Skip to content

[image_picker] Fix crash due to SecurityException #4004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.6+16

* Fixes crashes caused by `SecurityException` when calling `getPathFromUri()`.

## 0.8.6+15

* Bumps androidx.activity:activity from 1.6.1 to 1.7.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ String getPathFromUri(final Context context, final Uri uri) {
// target file was written in full. Flushing the stream merely moves
// the bytes into the OS, not necessarily to the file.
return null;
} catch (SecurityException e) {
// Calling `ContentResolver#openInputStream()` has been reported to throw a
// `SecurityException` on some devices in certain circumstances. Instead of crashing, we
// return `null`.
//
// See https://github.com/flutter/flutter/issues/100025 for more details.
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
package io.flutter.plugins.imagepicker;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
Expand Down Expand Up @@ -66,7 +72,22 @@ public void FileUtil_GetPathFromUri() throws IOException {

assertTrue(bytes.length > 0);
String imageStream = new String(bytes, UTF_8);
assertTrue(imageStream.equals("imageStream"));
assertEquals("imageStream", imageStream);
}

@Test
public void FileUtil_GetPathFromUri_securityException() throws IOException {
Uri uri = Uri.parse("content://dummy/dummy.png");

ContentResolver mockContentResolver = mock(ContentResolver.class);
when(mockContentResolver.openInputStream(any(Uri.class))).thenThrow(SecurityException.class);

Context mockContext = mock(Context.class);
when(mockContext.getContentResolver()).thenReturn(mockContentResolver);

String path = fileUtils.getPathFromUri(mockContext, uri);

assertNull(path);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Android implementation of the image_picker plugin.
repository: https://github.com/flutter/packages/tree/main/packages/image_picker/image_picker_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22

version: 0.8.6+15
version: 0.8.6+16

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down