share: correct MIME types, drop futile app-link verification, cap incoming copies
- Commit
e74aabf93c144c5348c6d7419e915aae3f7031ac- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-20 23:26 UTC (15 days ago)
- Parents
87b0bc6
Flashcards shared .apkg exports as octet-stream (receivers couldn't route them) - now application/vnd.anki. Office's share used */* - now the resolver's actual type. Notes read shared files unboundedly - now the existing 1MB bounded import reader. Youpipe and communicate claimed android:autoVerify on youtube/wa.me/signal.me domains that can never serve their assetlinks - dropped (BROWSABLE filters kept). Incoming shared-file copies in files/share/calendar copied without size limits (memory/disk abuse): 100MB caps for files/share staging (declared-size precheck + streaming enforcement + partial cleanup), 10MB for calendar ICS. Verified: compile for flashcards, office, notes, youpipe, communicate, files, share, calendar.
Diff
calendar/src/main/java/com/vayunmathur/calendar/MainActivity.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/MainActivity.kt b/calendar/src/main/java/com/vayunmathur/calendar/MainActivity.ktindex 7739ab74c3..cc14b63d3c 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/MainActivity.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/MainActivity.kt@@ -14,7 +14,7 @@ import com.vayunmathur.calendar.platform.ReminderScheduler import com.vayunmathur.calendar.platform.editEventFromIntent import com.vayunmathur.calendar.platform.isTimeEpochIntent import com.vayunmathur.calendar.ui.CalendarAppContent-import com.vayunmathur.calendar.ui.parseICSFile+import com.vayunmathur.calendar.ui.readSharedIcsEvents import com.vayunmathur.calendar.widget.CalendarGlanceWidgetReceiver import com.vayunmathur.library.log.Log import com.vayunmathur.library.ui.DynamicTheme@@ -193,9 +193,7 @@ class MainActivity : MaActivity() { lifecycleScope.launch(Dispatchers.IO) { val withEvents = uris.filter { uri -> try {- contentResolver.openInputStream(uri)?.use { iS ->- parseICSFile(iS).isNotEmpty()- } == true+ readSharedIcsEvents(contentResolver, uri).isNotEmpty() } catch (expected: Exception) { Log.error("MainActivity", "Error reading ICS file: $uri", expected) false
calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt b/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.ktindex 7585d7621e..8ad1e1a672 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/platform/CalendarViewModel.kt@@ -11,7 +11,7 @@ import androidx.lifecycle.viewModelScope import com.vayunmathur.calendar.R import com.vayunmathur.calendar.widget.CalendarGlanceWidget import com.vayunmathur.calendar.widget.CalendarMonthGlanceWidget-import com.vayunmathur.calendar.ui.parseICSFile+import com.vayunmathur.calendar.ui.readSharedIcsEvents import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow@@ -188,9 +188,7 @@ class CalendarViewModel(application: Application) : val allEvents = mutableListOf<Event>() uris.forEach { uri -> try {- app.contentResolver.openInputStream(uri)?.use { iS ->- allEvents.addAll(parseICSFile(iS))- }+ allEvents.addAll(readSharedIcsEvents(app.contentResolver, uri)) } catch (expected: Exception) { Log.error("CalendarViewModel", "Error parsing ICS file: $uri", expected) }
calendar/src/main/java/com/vayunmathur/calendar/ui/IcsImport.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/ui/IcsImport.kt b/calendar/src/main/java/com/vayunmathur/calendar/ui/IcsImport.ktindex 67e4c05e97..9dd6187dec 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/ui/IcsImport.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/ui/IcsImport.kt@@ -17,6 +17,11 @@ import androidx.compose.ui.unit.dp import com.vayunmathur.calendar.R import com.vayunmathur.calendar.platform.describe import com.vayunmathur.calendar.data.Event+import com.vayunmathur.library.log.Log+import android.content.ContentResolver+import android.net.Uri+import android.provider.OpenableColumns+import java.io.ByteArrayInputStream import java.io.InputStream @Composable@@ -59,3 +64,54 @@ fun EventCard(event: Event) { // Thin UI-package entry point; implementation lives in IcsParser.kt. fun parseICSFile(iS: InputStream): List<Event> = parseICSFileImpl(iS)++/** Largest shared ICS file the import path will read (10 MB). */+const val MAX_ICS_BYTES = 10L * 1024 * 1024++/**+ * Reads and parses the shared ICS file at [uri], bounded by [MAX_ICS_BYTES].+ *+ * The parser loads every line into memory, so an arbitrary shared file must+ * not be streamed into it unchecked. Oversized files yield an empty list,+ * which callers already surface as "no events to import". The declared size+ * is checked first; the read itself enforces the same cap because a provider+ * may omit or understate it.+ */+fun readSharedIcsEvents(resolver: ContentResolver, uri: Uri): List<Event> {+ declaredIcsSize(resolver, uri)?.let { size ->+ if (size > MAX_ICS_BYTES) {+ Log.status("IcsImport", "Skipping oversized ICS file ($size bytes): $uri")+ return emptyList()+ }+ }+ val bytes = resolver.openInputStream(uri)?.use { stream ->+ val out = java.io.ByteArrayOutputStream()+ val buffer = ByteArray(DEFAULT_BUFFER_SIZE)+ var total = 0L+ while (true) {+ val read = stream.read(buffer)+ if (read < 0) break+ total += read+ if (total > MAX_ICS_BYTES) {+ Log.status("IcsImport", "Skipping oversized ICS file (> $MAX_ICS_BYTES bytes): $uri")+ return emptyList()+ }+ out.write(buffer, 0, read)+ }+ out.toByteArray()+ } ?: return emptyList()+ return parseICSFile(ByteArrayInputStream(bytes))+}++/** Declared size of a shared ICS [Uri] from its provider, or null when unknown. */+private fun declaredIcsSize(resolver: ContentResolver, uri: Uri): Long? {+ if (uri.scheme != "content") return null+ return try {+ resolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)?.use { cursor ->+ val index = cursor.getColumnIndex(OpenableColumns.SIZE)+ if (index != -1 && cursor.moveToFirst()) cursor.getLong(index).takeIf { it >= 0 } else null+ }+ } catch (_: Exception) {+ null+ }+}
communicate/src/main/AndroidManifest.xml
diff --git a/communicate/src/main/AndroidManifest.xml b/communicate/src/main/AndroidManifest.xmlindex 0eefa75866..24f23a7d3d 100644--- a/communicate/src/main/AndroidManifest.xml+++ b/communicate/src/main/AndroidManifest.xml@@ -88,8 +88,10 @@ android:supportsPictureInPicture="true" android:theme="@style/Theme.General"> <!-- WhatsApp and Signal contact links, so "message this number" from a browser or another app- lands here. autoVerify claims them as App Links where the host allows it. -->- <intent-filter android:autoVerify="true">+ lands here. No autoVerify: wa.me/signal.me (and the other hosts below) are WhatsApp/Signal+ domains that can never publish assetlinks for this app, so verification would always+ fail. Plain BROWSABLE handling still applies. -->+ <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" />
files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.ktindex 8819ea6191..40c5ab17ca 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesShareModel.kt@@ -304,6 +304,9 @@ abstract class FilesShareModel(application: Application) : } } +/** Largest shared file [saveUriToPath] will copy in (100 MB). */+internal const val MAX_INCOMING_FILE_BYTES = 100L * 1024 * 1024+ /** Copies a shared content [Uri] into [targetDir] under its display name. */ internal fun saveUriToPath(context: Context, uri: Uri, targetDir: File) { // The display name is supplied by the sending app's provider, so it must@@ -311,11 +314,50 @@ internal fun saveUriToPath(context: Context, uri: Uri, targetDir: File) { // truncated: sanitize to a plain name and pick a fresh destination. val name = sanitizedFileName(getSharedFileName(context, uri)) ?: "shared_file_${System.currentTimeMillis()}"+ // Size pre-check: a provider that declares its size lets us refuse an+ // oversized share before writing anything. Providers may lie or omit+ // the size, so the copy below enforces the same cap while streaming.+ declaredSharedSize(context, uri)?.let { size ->+ if (size > MAX_INCOMING_FILE_BYTES) {+ throw IOException("Shared file is too large (over 100 MB): $name")+ }+ } val targetFile = uniqueDestination(targetDir, name) val input = context.contentResolver.openInputStream(uri) ?: throw IOException("Could not open shared file: $uri")- input.use {- targetFile.outputStream().use { out -> input.copyTo(out) }+ try {+ input.use {+ targetFile.outputStream().use { out ->+ val buffer = ByteArray(DEFAULT_BUFFER_SIZE)+ var copied = 0L+ while (true) {+ val read = input.read(buffer)+ if (read < 0) break+ copied += read+ if (copied > MAX_INCOMING_FILE_BYTES) {+ throw IOException("Shared file is too large (over 100 MB): $name")+ }+ out.write(buffer, 0, read)+ }+ }+ }+ } catch (e: IOException) {+ // Never leave a truncated or oversized partial copy behind.+ targetFile.delete()+ throw e+ }+}++/** Declared size of a shared [Uri] from its provider, or null when unknown. */+private fun declaredSharedSize(context: Context, uri: Uri): Long? {+ if (uri.scheme != "content") return null+ return try {+ context.contentResolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)?.use { cursor ->+ val index = cursor.getColumnIndex(OpenableColumns.SIZE)+ if (index != -1 && cursor.moveToFirst()) cursor.getLong(index).takeIf { it >= 0 } else null+ }+ } catch (_: Exception) {+ null } }
flashcards/src/main/java/com/vayunmathur/flashcards/ui/NoteListPage.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/ui/NoteListPage.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/ui/NoteListPage.ktindex 2af5d18c8d..77683d2fad 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/ui/NoteListPage.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/ui/NoteListPage.kt@@ -69,7 +69,9 @@ fun NoteListPage( LaunchedEffect(viewModel, deckId) { viewModel.shareRequests.collect { uri -> val intent = Intent(Intent.ACTION_SEND).apply {- type = "application/octet-stream"+ // Real Anki package type (the exported file is a .apkg), so+ // receivers such as AnkiDroid route it instead of generic handlers.+ type = "application/vnd.anki" putExtra(Intent.EXTRA_STREAM, uri) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) }
flashcards/src/main/java/com/vayunmathur/flashcards/ui/SettingsPage.kt
diff --git a/flashcards/src/main/java/com/vayunmathur/flashcards/ui/SettingsPage.kt b/flashcards/src/main/java/com/vayunmathur/flashcards/ui/SettingsPage.ktindex 621f8c9716..7f5a165544 100644--- a/flashcards/src/main/java/com/vayunmathur/flashcards/ui/SettingsPage.kt+++ b/flashcards/src/main/java/com/vayunmathur/flashcards/ui/SettingsPage.kt@@ -79,7 +79,9 @@ fun SettingsPage(backStack: NavBackStack<Route>, viewModel: FlashcardsViewModel) LaunchedEffect(viewModel) { viewModel.shareRequests.collect { uri -> val intent = Intent(Intent.ACTION_SEND).apply {- type = "application/octet-stream"+ // Real Anki package type (the exported file is a .apkg), so+ // receivers such as AnkiDroid route it instead of generic handlers.+ type = "application/vnd.anki" putExtra(Intent.EXTRA_STREAM, uri) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) }
notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt b/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.ktindex 95e6029dfc..bb3b85069c 100644--- a/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt+++ b/notes/src/main/java/com/vayunmathur/notes/platform/NotesViewModel.kt@@ -331,8 +331,11 @@ class NotesViewModel( val ctx = getApplication<Application>() try { val uri = uriString.toUri()- val content = ctx.contentResolver.openInputStream(uri)- ?.bufferedReader()?.use { it.readText() } ?: return@withContext null+ // Bounded like imports: an arbitrary shared file must not OOM the+ // editor via readText(). Oversized reads return null, which the+ // caller surfaces as the existing "couldn't open file" message.+ val content = readBoundedText(ctx, uri, MAX_IMPORT_BYTES)+ ?: return@withContext null val name = IntentHelper.getFileName(ctx, uri) ?: "Untitled" ExternalNoteContent(name.removeSuffix(".markdown").removeSuffix(".md"), content) } catch (e: Exception) {
office/src/main/java/com/vayunmathur/office/ui/OfficeDocumentMenuBar.kt
diff --git a/office/src/main/java/com/vayunmathur/office/ui/OfficeDocumentMenuBar.kt b/office/src/main/java/com/vayunmathur/office/ui/OfficeDocumentMenuBar.ktindex fdb3f0dc40..45f86a6035 100644--- a/office/src/main/java/com/vayunmathur/office/ui/OfficeDocumentMenuBar.kt+++ b/office/src/main/java/com/vayunmathur/office/ui/OfficeDocumentMenuBar.kt@@ -203,7 +203,9 @@ private fun ShareFileItem( onClick = { s.fileMenu = false val send = Intent(Intent.ACTION_SEND).apply {- type = "*/*"+ // The provider's real type for this document; */* only when+ // the provider declines to name one.+ type = context.contentResolver.getType(uri) ?: "*/*" putExtra(Intent.EXTRA_STREAM, uri) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) }
share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt
diff --git a/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt b/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.ktindex c1a3c86779..49cf988cca 100644--- a/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt+++ b/share/src/main/java/com/vayunmathur/share/platform/ShareViewModel.kt@@ -363,6 +363,9 @@ internal fun resolveDisplayName(context: Context, uri: Uri): String? { } } +/** Largest shared file [uriToTempFile] will stage (100 MB). */+internal const val MAX_SHARED_FILE_BYTES = 100L * 1024 * 1024+ @Suppress("TooGenericExceptionCaught") internal fun uriToTempFile(context: Context, uri: Uri): File? { return try {@@ -375,12 +378,44 @@ internal fun uriToTempFile(context: Context, uri: Uri): File? { if (src.exists() && src.isFile) { // Copying a file onto itself truncates it, and shared text is staged here. if (src.canonicalPath == dest.canonicalPath) return src+ if (src.length() > MAX_SHARED_FILE_BYTES) {+ Log.status(TAG, "uriToTempFile skipped oversized file (> $MAX_SHARED_FILE_BYTES bytes): $uri")+ return null+ } src.copyTo(dest, overwrite = true) return dest } }+ // Size pre-check, enforced again while streaming below because a+ // provider may omit or understate its declared size.+ declaredSharedSize(context, uri)?.let { size ->+ if (size > MAX_SHARED_FILE_BYTES) {+ Log.status(TAG, "uriToTempFile skipped oversized file ($size bytes): $uri")+ return null+ }+ }+ var oversized = false context.contentResolver.openInputStream(uri)?.use { input ->- dest.outputStream().use { out -> input.copyTo(out) }+ dest.outputStream().use { out ->+ val buffer = ByteArray(DEFAULT_BUFFER_SIZE)+ var copied = 0L+ while (true) {+ val read = input.read(buffer)+ if (read < 0) break+ copied += read+ if (copied > MAX_SHARED_FILE_BYTES) {+ oversized = true+ break+ }+ out.write(buffer, 0, read)+ }+ }+ }+ if (oversized) {+ // Never leave a truncated or oversized staging copy behind.+ dest.delete()+ Log.status(TAG, "uriToTempFile skipped oversized file (> $MAX_SHARED_FILE_BYTES bytes): $uri")+ return null } if (dest.exists() && dest.length() > 0) dest else null } catch (e: Exception) {@@ -388,3 +423,16 @@ internal fun uriToTempFile(context: Context, uri: Uri): File? { null } }++/** Declared size of a shared [Uri] from its provider, or null when unknown. */+private fun declaredSharedSize(context: Context, uri: Uri): Long? {+ if (uri.scheme != "content") return null+ return try {+ context.contentResolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)?.use { c ->+ val index = c.getColumnIndex(OpenableColumns.SIZE)+ if (index != -1 && c.moveToFirst()) c.getLong(index).takeIf { it >= 0 } else null+ }+ } catch (_: Exception) {+ null+ }+}
youpipe/src/main/AndroidManifest.xml
diff --git a/youpipe/src/main/AndroidManifest.xml b/youpipe/src/main/AndroidManifest.xmlindex 0cb75a6345..5a07e64a15 100644--- a/youpipe/src/main/AndroidManifest.xml+++ b/youpipe/src/main/AndroidManifest.xml@@ -47,7 +47,10 @@ android:supportsPictureInPicture="true" android:configChanges="mcc|mnc|colorMode|density|fontScale|fontWeightAdjustment|keyboard|keyboardHidden|layoutDirection|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode" android:theme="@style/Theme.General">- <intent-filter android:autoVerify="true">+ <!-- No autoVerify: youtube.com/youtu.be are Google's domains and+ can never publish assetlinks for this app, so verification+ would always fail. Plain BROWSABLE handling still applies. -->+ <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" />