Android FileProvider: share a document while limiting access
A Share PDF button sends data beyond your app. You need to know which document another app receives, what permissions it gains and what happens afterward. We will explore FileProvider through a fictional example and build a verification plan for your own Android release.
Define exactly what the user is sending
A fictional journal creates a PDF containing a synthetic entry called “Training document A.” The user wants to share that export with a document editor. The recipient does not need other journal entries. This is a teaching model, not an account of a vulnerability found in a real app.
Start with the PDF itself: does it include unrelated entries, internal fields or another account’s data? A preview helps the user understand the result. Android’s access mechanism limits access to a file; it does not remove unnecessary information from a document already generated. The recipient can save its own copy.
Use a narrow directory for exports
Android Developers recommends sharing a content:// URI through FileProvider instead of a direct file:// address. Path configuration defines which directories the provider can serve at all. The journal could reserve filesDir/exports/ for prepared copies intended for sharing.
A files-path mapping specifies the exports/ subdirectory. It defines a file area, not a PDF extension filter or an automatic grant for one document. Avoid root-path or mapping the entire internal directory for convenience. If you use cacheDir/exports/, remember that the system may clear cached files.
A broad path increases the consequences of an incorrect URI grant. It does not, by itself, prove that every app can already read every file: permissions, granted URIs and sharing code also matter. Investigate the complete chain rather than treating a configuration string as proof of exposure.
Inspect the manifest in the actual release
The FileProvider documentation shows android:exported="false" together with android:grantUriPermissions="true". The provider is not generally exposed to other apps, but can issue individual URI grants. The second attribute enables that mechanism; it does not grant access automatically.
The authority in the manifest must match the value used with getUriForFile(). Account for applicationId variants and the final merged manifest: libraries may add their own providers. Review the release users will actually receive. If authorities differ, fix that configuration while preserving the exported restriction; opening the provider to all apps is unnecessary.
Share a specific URI with the minimum permissions
Prepare a separate document copy and obtain its URI with getUriForFile(). FLAG_GRANT_READ_URI_PERMISSION is sufficient for read-only sharing. Write access needs a separate justification; viewing a PDF does not provide one.
For ACTION_SEND, place the URI in EXTRA_STREAM, set the MIME type to application/pdf and arrange permissions and ClipData correctly for supported Android versions. Follow the file-sharing guide and FileProvider reference. A direct Uri.fromFile() is not a substitute for a targeted grant.
Sharing from internal storage this way does not require broad permission to read the whole device storage. Preparing the export remains your code’s responsibility: FileProvider does not know which journal entry the current account may send. Enforce that decision before generating the file.
Account for grant duration and retained copies
A URI grant’s lifetime depends on how it was issued. The Android reference ties an Intent grant to the receiving Activity’s stack. Closing the sender’s app chooser is not a universal revocation point. An explicit Context.grantUriPermission() grant lasts until revokeUriPermission() or a device reboot.
If your app grants access explicitly, design its revocation. A document the recipient has already copied will not disappear with the grant. Do not promise that closing a window or signing out of the journal deletes a previously shared copy.
Define export retention and cleanup after account changes. Avoid deleting a file before the recipient reads it, or reusing an old copy for the next export. These application rules complement system permissions and need separate verification.
Test boundaries with two apps you control
Use your own emulator or test device, the journal build and a separate test recipient app. Create synthetic documents only. The following is a proposed verification plan, not the results of an audit performed here.
- The selected export opens and contains exactly the expected material.
- A fresh recipient without a grant cannot access that URI.
- A recipient with read-only access cannot modify the original file.
- getUriForFile() refuses to create a URI for a file outside the configured directories.
- Grant lifetime and explicit revocation match the chosen sharing method.
- Switching accounts does not offer the previous export; canceling the chooser does not send it.
- Sharing, failure handling and cleanup are checked across supported Android versions.
Record a result your team can reproduce
Keep the build identifier, Android version, provider configuration and expected outcome for each scenario. A synthetic document and an action sequence are enough to reproduce it. OWASP recommends combining narrow paths, a non-exported provider and temporary grants; verification should show those controls working together.
After a fix, rerun the matrix against the finished release, including providers contributed by SDKs. If the journal obtains records through a website or API, review server permissions for those records separately: local FileProvider configuration does not control cloud access.