Closed Bug 2063609 Opened 1 month ago Closed 1 month ago

Uri.getFileName crashes when content providers reject metadata queries or return an empty cursor

Categories

(Firefox for Android :: Downloads, defect, P2)

All
Android
defect

Tracking

()

RESOLVED FIXED
156 Branch
Tracking Status
relnote-firefox --- 156+
firefox156 --- fixed

People

(Reporter: corentin, Assigned: corentin)

References

(Blocks 1 open bug)

Details

(Keywords: crash, Whiteboard: [fxdroid][android-activation-trust] )

Attachments

(1 file)

When handling a result from the Android file picker, FilePicker.handleFilePickerIntentResult() calls enqueueForCleanup() so that the temporary file can be deleted later. enqueueForCleanup() attempts to get the file name by calling Uri.getFileName(), but some content providers can throw an exception at this step:

  • ContentResolver.query() can throw IllegalArgumentException
  • CursorWrapper.getString() can throw CursorIndexOutOfBoundsException if the cursor is empty

These exceptions happen before Firefox processes the selected file, so the upload is interrupted and Firefox crashes.

Example stacks:

java.lang.IllegalArgumentException
    at android.database.DatabaseUtils.readExceptionFromParcel
    at android.content.ContentProviderProxy.query
    at android.content.ContentResolver.query
    at mozilla.components.support.ktx.android.net.UriKt.getFileName
    at mozilla.components.feature.prompts.file.FilePicker.enqueueForCleanup
android.database.CursorIndexOutOfBoundsException
    at android.database.AbstractCursor.checkPosition
    at android.database.AbstractWindowedCursor.getString
    at android.database.CursorWrapper.getString
    at mozilla.components.support.ktx.android.net.UriKt.getFileName
    at mozilla.components.feature.prompts.file.FilePicker.enqueueForCleanup

Expected behavior

Failure to retrieve filename metadata should not crash Firefox. Such exceptions should be either caught, or a fallback file name should be generated to process so that the uploaded file is processed

Any additional information?

These exceptions were noticed when investigation this bug. These are ones of many causes of java.lang.RuntimeException: at android.app.ActivityThread.deliverResults described in this meta ticket

No longer depends on: 1805342
Assignee: nobody → cbect
Status: NEW → ASSIGNED
Severity: -- → S3
Priority: -- → P2

FilePicker.enqueueForCleanup() calls Uri.getFileName() to name the temporary file that will be deleted later. Content URIs are processed in getFileNameForContentUris(), which used to query the content provider for OpenableColumns.DISPLAY_NAME column without accounting for some failures:

  • ContentResolver.query() re throws whatever the content provider throws. For instance, an unrecognized URI can be rejected with an IllegalArgumentException
  • moveToFirst() returns a boolean telling whether the move succeeded. This value was ignored and getString() could be called with an invalid cursor position, leading to a CursorIndexOutOfBoundsException

Both cases made Firefox crash. getFileNameForContentUris() now checks moveToFirst() result before calling getString(), and treats a RuntimeException from the query or from the cursor read as "no name available". A fallback name is now generated once, at the end of the function.

New unit tests have been added to cover these cases. The existing tests covering getFileNameForContentUris() have been renamed to use GIVEN ... WHEN ... THEN ... form for consistency.

Attachment #9628265 - Attachment description: WIP: Bug 2063609 - Prevent crash when a content provider fails to return a file name → Bug 2063609 - Prevent crash when a content provider fails to return a file name
Whiteboard: [fxdroid][group4] → [fxdroid][android-activation-trust]
Pushed by cbect@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/1961d663da0c https://hg.mozilla.org/integration/autoland/rev/8b5b009b2b9d Prevent crash when a content provider fails to return a file name r=android-reviewers,rebecatudor273
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 156 Branch

[Tracking Requested - why for this release]: Fixed an issue that could cause Firefox to crash when selecting files from some Android file providers.

Release Note Request (optional, but appreciated)
[Why is this notable]:
[Affects Firefox for Android]:
[Suggested wording]: Fixed an issue that could cause Firefox to crash when selecting files from some Android file providers.
[Links (documentation, blog post, etc)]:

Thanks, added to the Fx156 nightly release notes, please allow 30 minutes for the site to update.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: