Skip to content

Commit a896155

Browse files
authored
[file_selector_android] Handle a null stream from openInputStream (flutter#12716)
Null safety when ContentResolver cannot open a stream. Copied from closed: flutter/packages#12683 Fixes: flutter#191989 ## Pre-Review Checklist
1 parent cda604a commit a896155

6 files changed

Lines changed: 87 additions & 1 deletion

File tree

‎packages/file_selector/file_selector_android/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.5.2+11
2+
3+
* Fixes a crash when the content provider returns no stream for a selected
4+
file; the pick now completes with an error instead.
5+
16
## 0.5.2+10
27

38
* Updates pigeon dev_dependency to ^27.3.2 for analyzer 14 compatibility.

‎packages/file_selector/file_selector_android/android/src/main/java/dev/flutter/packages/file_selector_android/FileSelectorApiImpl.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ FileResponse toFileResponse(@NonNull Uri uri) {
351351

352352
final byte[] bytes = new byte[size];
353353
try (InputStream inputStream = contentResolver.openInputStream(uri)) {
354+
if (inputStream == null) {
355+
// `ContentResolver#openInputStream()` returns null when the provider cannot serve the
356+
// file (for example after the provider crashed). Reading it would throw a
357+
// NullPointerException on the activity-result callback, i.e. the main thread.
358+
Log.w(TAG, "The content provider returned no stream for the selected file.");
359+
return null;
360+
}
354361
final DataInputStream dataInputStream = objectFactory.newDataInputStream(inputStream);
355362
dataInputStream.readFully(bytes);
356363
} catch (IOException exception) {

‎packages/file_selector/file_selector_android/android/src/main/java/dev/flutter/packages/file_selector_android/FileUtils.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public static String getPathFromUri(@NonNull Context context, @NonNull Uri uri)
124124
public static String getPathFromCopyOfFileFromUri(@NonNull Context context, @NonNull Uri uri)
125125
throws IOException, SecurityException, IllegalArgumentException {
126126
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
127+
if (inputStream == null) {
128+
// The provider cannot serve the file (see `FileSelectorApiImpl#toFileResponse`); surface
129+
// it as the IO failure it is instead of dereferencing the null stream in `copy()`.
130+
throw new IOException("The content provider returned no stream for the file.");
131+
}
127132
String uuid = UUID.nameUUIDFromBytes(uri.toString().getBytes()).toString();
128133
File targetDirectory = new File(context.getCacheDir(), uuid);
129134
targetDirectory.mkdir();

‎packages/file_selector/file_selector_android/android/src/test/java/dev/flutter/packages/file_selector_android/FileSelectorAndroidPluginTest.java‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static org.mockito.Mockito.lenient;
1313
import static org.mockito.Mockito.mock;
1414
import static org.mockito.Mockito.mockStatic;
15+
import static org.mockito.Mockito.never;
1516
import static org.mockito.Mockito.verify;
1617
import static org.mockito.Mockito.when;
1718

@@ -288,6 +289,60 @@ public void openFilesCompletesWithError_whenSecurityExceptionInGetPathFromCopyOf
288289
// Regression test for https://github.com/flutter/flutter/issues/159568: the
289290
// single-file `openFile` path must likewise surface a copy failure to Dart
290291
// instead of crashing.
292+
@SuppressWarnings({"rawtypes", "unchecked"})
293+
@Test
294+
public void openFileCompletesWithError_whenProviderReturnsNullStream()
295+
throws FileNotFoundException {
296+
final ContentResolver mockContentResolver = mock(ContentResolver.class);
297+
final Uri mockUri = mock(Uri.class);
298+
299+
final Cursor mockCursor = mock(Cursor.class);
300+
when(mockCursor.moveToFirst()).thenReturn(true);
301+
when(mockCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)).thenReturn(0);
302+
when(mockCursor.getString(0)).thenReturn("filename");
303+
when(mockCursor.getColumnIndex(OpenableColumns.SIZE)).thenReturn(1);
304+
when(mockCursor.isNull(1)).thenReturn(false);
305+
when(mockCursor.getInt(1)).thenReturn(30);
306+
when(mockContentResolver.query(mockUri, null, null, null, null, null)).thenReturn(mockCursor);
307+
// A provider that cannot serve the file answers the open with null; previously this reached
308+
// `DataInputStream#readFully` and threw a NullPointerException on the main thread.
309+
when(mockContentResolver.openInputStream(mockUri)).thenReturn(null);
310+
311+
when(mockObjectFactory.newIntent(Intent.ACTION_OPEN_DOCUMENT)).thenReturn(mockIntent);
312+
when(mockActivity.getContentResolver()).thenReturn(mockContentResolver);
313+
when(mockActivityBinding.getActivity()).thenReturn(mockActivity);
314+
final FileSelectorApiImpl fileSelectorApi =
315+
new FileSelectorApiImpl(
316+
mockActivityBinding, mockObjectFactory, (version) -> Build.VERSION.SDK_INT >= version);
317+
318+
final boolean[] callbackCalled = new boolean[1];
319+
final Throwable[] failure = new Throwable[1];
320+
fileSelectorApi.openFile(
321+
null,
322+
new FileTypes(Collections.emptyList(), Collections.emptyList()),
323+
ResultCompat.asCompatCallback(
324+
(reply) -> {
325+
callbackCalled[0] = true;
326+
failure[0] = reply.exceptionOrNull();
327+
return null;
328+
}));
329+
330+
verify(mockActivity).startActivityForResult(mockIntent, 221);
331+
332+
final ArgumentCaptor<PluginRegistry.ActivityResultListener> listenerArgumentCaptor =
333+
ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class);
334+
verify(mockActivityBinding).addActivityResultListener(listenerArgumentCaptor.capture());
335+
336+
final Intent resultMockIntent = mock(Intent.class);
337+
when(resultMockIntent.getData()).thenReturn(mockUri);
338+
listenerArgumentCaptor.getValue().onActivityResult(221, Activity.RESULT_OK, resultMockIntent);
339+
340+
assertTrue(callbackCalled[0]);
341+
assertNotNull(failure[0]);
342+
assertTrue(failure[0].getMessage().contains("Failed to read file"));
343+
verify(mockObjectFactory, never()).newDataInputStream(any());
344+
}
345+
291346
@SuppressWarnings({"rawtypes", "unchecked"})
292347
@Test
293348
public void openFileCompletesWithError_whenSecurityExceptionInGetPathFromCopyOfFileFromUri()

‎packages/file_selector/file_selector_android/android/src/test/java/dev/flutter/packages/file_selector_android/FileUtilsTest.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import static org.junit.Assert.assertNotNull;
1111
import static org.junit.Assert.assertThrows;
1212
import static org.junit.Assert.assertTrue;
13+
import static org.mockito.Mockito.mock;
1314
import static org.mockito.Mockito.mockStatic;
1415
import static org.mockito.Mockito.spy;
16+
import static org.mockito.Mockito.when;
1517
import static org.robolectric.Shadows.shadowOf;
1618

1719
import android.content.ContentProvider;
@@ -68,6 +70,18 @@ public void before() {
6870
}
6971
}
7072

73+
@Test
74+
public void getPathFromCopyOfFileFromUri_throwsIOExceptionForNullStream() throws IOException {
75+
Uri uri = Uri.parse("content://dummy/dummy.txt");
76+
77+
ContentResolver mockContentResolver = mock(ContentResolver.class);
78+
when(mockContentResolver.openInputStream(uri)).thenReturn(null);
79+
Context mockContext = mock(Context.class);
80+
when(mockContext.getContentResolver()).thenReturn(mockContentResolver);
81+
82+
assertThrows(IOException.class, () -> FileUtils.getPathFromCopyOfFileFromUri(mockContext, uri));
83+
}
84+
7185
@Test
7286
public void getPathFromUri_returnsExpectedPathForExternalDocumentUri() {
7387
// Uri that represents Documents/test directory on device:

‎packages/file_selector/file_selector_android/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: file_selector_android
22
description: Android implementation of the file_selector package.
33
repository: https://github.com/flutter/packages/tree/main/packages/file_selector/file_selector_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
5-
version: 0.5.2+10
5+
version: 0.5.2+11
66

77
environment:
88
sdk: ^3.12.0

0 commit comments

Comments
 (0)