Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Granting uri permissions for FileProvider gives SecurityException

I have 2 apps - Demo and Pro. Demo has a content provider and when Pro is installed it needs to transfer all files from the demo provider.

Demo app (provider):

<provider
            android:name="***.provider.InternalStorageProvider"
            android:authorities="***.demo.storage.int.provider"
            android:exported="false"
            android:syncable="true"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/int_storage_paths" />

</provider>

Pro app (consumer):

  • Experiment 1:

    ParcelFileDescriptor pfd = cr.openFileDescriptor(exposedFileUri, "r");
    FileInputStream input = new FileInputStream(pfd.getFileDescriptor());

java.lang.SecurityException: Permission Denial: opening provider .provider.InternalStorageProvider from ProcessRecord{9c85875 10734:/u0a61} (pid=10734, uid=10061) that is not exported from uid 10062

  • Experiment 2:

    Activity activity = getActivity(); activity.grantUriPermission(activity.getPackageName(), exposedFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

    ParcelFileDescriptor pfd = cr.openFileDescriptor(exposedFileUri, "r"); FileInputStream input = new FileInputStream(pfd.getFileDescriptor());

java.lang.SecurityException: Uid 10061 does not have permission to uri 0 @ content://***.demo.storage.int.provider/db/file1

InternalStorageProvider is a copy of a normal FileProvider. But it doesn't matter as the execution cannot even reach it. Exceptions are thrown before it is called. Note that no chooser activities and intents are involved. The consumer tries to open the file from a known uri directly, without choosers. Most of the examples I've found are using Intent.FLAG_GRANT_READ_URI_PERMISSION but I don't use an intent at all.

How I am supposed to grant uri permissions to the consumer correctly?

like image 842
WindRider Avatar asked Feb 18 '16 09:02

WindRider


People also ask

What is Grant URI permission?

<grant-uri-permission>Specifies the subsets of app data that the parent content provider has permission to access. Data subsets are indicated by the path part of a content: URI. (The authority part of the URI identifies the content provider.)

What is androidx core content FileProvider?

androidx.core.content.FileProvider. FileProvider is a special subclass of ContentProvider that facilitates secure sharing of files associated with an app by creating a content:// Uri for a file instead of a file:/// Uri . A content URI allows you to grant read and write access using temporary access permissions.


1 Answers

You need to give permission to pro app...

activity.grantUriPermission("pro app package name", exposedFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
like image 186
Jd Prajapati Avatar answered Oct 08 '22 20:10

Jd Prajapati