Granting uri permissions for FileProvider gives SecurityException
Asked Answered
W

1

6

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?

Winze answered 18/2, 2016 at 9:30 Comment(3)
Running on this issue right now, by any chance you were able to solve it?Clifton
It was quite long ago, I barely remember anything. This stuff is real evil.Winze
Solved it by passing FLAG_GRANT_READ_URI_PERMISSION to absolutely every other intent that was making use of the URI inside my appClifton
C
1

You need to give permission to pro app...

activity.grantUriPermission("pro app package name", exposedFileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
Chelyuskin answered 16/3, 2017 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.