Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,21 @@ private fun Context.copyText(label: String, text: String) {

private fun Context.shareText(text: String): Boolean =
runCatching {
val cachePath = java.io.File(cacheDir, "diagnostics").apply { mkdirs() }
val file = java.io.File(cachePath, "diagnostic_report.txt")
file.writeText(text)
Comment on lines +576 to +577

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use unique URIs for each diagnostic report

Because every share overwrites the same diagnostic_report.txt, any app that was previously chosen for a diagnostic share can keep its temporary read grant to this stable FileProvider URI while its receive/share activity remains alive, and then read the contents of a later report that the user shares to someone else. This only affects users who generate/share multiple diagnostic reports before the earlier grant is revoked, but it undermines the intended per-share consent boundary; create a unique file/URI per report and clean up old files instead of reusing this filename.

Useful? React with 👍 / 👎.


val uri = androidx.core.content.FileProvider.getUriForFile(
this,
"${packageName}.fileprovider",
file
)

val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_SUBJECT, "CLHS Score 診斷包")
putExtra(Intent.EXTRA_TEXT, text)
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
Comment on lines 585 to 590

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

When sharing a file URI via Intent.ACTION_SEND and Intent.EXTRA_STREAM, setting FLAG_GRANT_READ_URI_PERMISSION alone is often not sufficient on many Android versions. The system grants URI permissions automatically only to the URI in the intent's data field or its clipData. Since ACTION_SEND uses EXTRA_STREAM instead of data, you should explicitly set the URI in clipData to ensure the receiving application is granted read permissions properly.

Suggested change
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_SUBJECT, "CLHS Score 診斷包")
putExtra(Intent.EXTRA_TEXT, text)
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_SUBJECT, "CLHS Score 診斷包")
putExtra(Intent.EXTRA_STREAM, uri)
clipData = ClipData.newRawUri("", uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

startActivity(Intent.createChooser(intent, "分享診斷包"))
}.isSuccess
4 changes: 4 additions & 0 deletions android/app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="shared_diagnostics" path="diagnostics/" />
</paths>
Loading