TypeScript
fix(mobile): shared file names can no longer write outside the Android cache (#26175)
Julius Marminge622bc06d8ad9
diff --git a/patches/[email protected] b/patches/[email protected]index a5ce4559f52..3084d8e0062 100644--- a/patches/[email protected]+++ b/patches/[email protected]@@ -1,5 +1,5 @@diff --git a/android/src/main/java/expo/modules/sharing/SharingRecords.kt b/android/src/main/java/expo/modules/sharing/SharingRecords.kt-index 1113a0a..e89529a 100644+index 1113a0adbb951081dea5214a711fe1dbb5a3e814..e89529a43258ae3bd6ec730c19453aed5e5efac4 100644--- a/android/src/main/java/expo/modules/sharing/SharingRecords.kt+++ b/android/src/main/java/expo/modules/sharing/SharingRecords.kt@@ -51,7 +51,8 @@ enum class ContentType(val value: String) : Enumerable {@@ -12,8 +12,56 @@ index 1113a0a..e89529a 100644) : Record@OptimizedRecord+diff --git a/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt b/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt+index 0a4c92e4b6459632933027827a86a4ec994ce72f..54ff2b5f44df740c9b971e409397e4a08a973717 100644+--- a/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt++++ b/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt+@@ -9,6 +9,7 @@ import java.io.File+ import java.io.FileOutputStream+ import java.net.HttpURLConnection+ import java.net.URL++import java.util.UUID+ import kotlin.text.indexOf+ import kotlin.text.substring++@@ -56,14 +57,20 @@ class ResolvingShareIntentDataParser {++ private fun resolveUri(context: Context, uri: Uri, mimeType: String): ResolvedSharePayload {+ val contentResolver = context.contentResolver+- val fileName = getFileName(contentResolver, uri) ?: "unknown"++ val fileName = safeFileName(getFileName(contentResolver, uri))+ val fileSize = getFileSize(contentResolver, uri)+ val specificMimeType = contentResolver.getType(uri) ?: mimeType+ val type = ShareType.fromMimeType(specificMimeType)++- // Copy to cache dir to ensure access+- val file = File(context.cacheDir, fileName)++ // The sending app controls the name, so copy into a fresh directory and refuse any++ // destination that resolves outside cacheDir.++ val cacheDir = context.cacheDir.canonicalFile++ val file = File(File(cacheDir, "shared-${UUID.randomUUID()}"), fileName)++ if (!file.canonicalPath.startsWith(cacheDir.path + File.separator)) {++ throw SecurityException("Shared file name escapes the cache directory")++ }+ try {++ file.parentFile?.mkdirs()+ contentResolver.openInputStream(uri)?.use { input ->+ FileOutputStream(file).use { output ->+ input.copyTo(output)+@@ -85,6 +92,11 @@ class ResolvingShareIntentDataParser {+ }+ }+++ private fun safeFileName(name: String?): String {++ val baseName = name?.let { File(it).name }++ return if (baseName.isNullOrBlank() || baseName == "." || baseName == "..") "unknown" else baseName++ }+++ private fun getFileName(resolver: ContentResolver, uri: Uri): String? {+ val cursor = resolver.query(uri, null, null, null, null)+ val name = cursor?.use { cursor ->diff --git a/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt b/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt-index 3381c5f..481cdb4 100644+index 3381c5fd207ebb4cd8e0301aac59d307939ff162..481cdb4bc64ea5ae49919071f686324f6d56d55c 100644--- a/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt+++ b/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt@@ -3,6 +3,7 @@ package expo.modules.sharing@@ -93,7 +141,7 @@ index 3381c5f..481cdb4 100644}}diff --git a/build/Sharing.types.d.ts b/build/Sharing.types.d.ts-index 74544e0..92bd2f5 100644+index 74544e0728d5f845351693d8d90596abae42eaea..92bd2f53108ac717f1ef7292fe89aea05c676c5e 100644--- a/build/Sharing.types.d.ts+++ b/build/Sharing.types.d.ts@@ -100,6 +100,11 @@ export type SharePayload = {@@ -175,7 +223,7 @@ index 943c263c0cf58be595bfc9745c91827da81befec..3a99a1822beb25fea3835123b9a4ad7aif (mainGroupUuid && sharedGroupUuid) {diff --git a/src/Sharing.types.ts b/src/Sharing.types.ts-index 18af320..3a52b70 100644+index 18af320420e7c01a8ffbd7fa5347320409f905d8..3a52b70a20efbc588ad5ca974c38c951e5757c97 100644--- a/src/Sharing.types.ts+++ b/src/Sharing.types.ts@@ -108,6 +108,12 @@ export type SharePayload = {diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 13f9ba0a7be..ba6cfe4dee3 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -375,7 +375,7 @@ patchedDependencies:[email protected]: 530af071088a809fe1a97ce2b2269c78335fbb5dcd3893d6937ff5c304c85a0a[email protected]: f0b0fd100550ed76e5c25b4d13eb7c9ff62825f8f197c53edf7bf447c50c9fb7[email protected]: cd09b6d1744dde4efa081b5619ebcff38877d98f7502a51cab121629dece8237- [email protected]: 61a9741dc6166fdbb5baa2e489275a65dd21aa11d2c915f490a1b337a4468e76+ [email protected]: dcdc534eeb18eb9fb0919c0ff7da1f7a75076b5b38b57f231cad32bb17c9dda3[email protected]: e80ec3ef8c4af50a607beea1c0917e71ba679844efdb702b45d17a5f03cf466c[email protected]: 8bf49067f93742855e15f29559ac75c8b50230b47069060993c9ea01dac11171[email protected]: afe6b2c89e1eaddfd3c90cf2723f12c9a17d513e65beff51653781486013251c@@ -697,7 +697,7 @@ importers:version: 58.0.4([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))expo-sharing:specifier: 58.0.19- version: 58.0.19(patch_hash=61a9741dc6166fdbb5baa2e489275a65dd21aa11d2c915f490a1b337a4468e76)(@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected])+ version: 58.0.19(patch_hash=dcdc534eeb18eb9fb0919c0ff7da1f7a75076b5b38b57f231cad32bb17c9dda3)(@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected])expo-splash-screen:specifier: ~58.0.10@@ -18514,7 +18514,7 @@ snapshots:- [email protected](patch_hash=61a9741dc6166fdbb5baa2e489275a65dd21aa11d2c915f490a1b337a4468e76)(@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected]):+ [email protected](patch_hash=dcdc534eeb18eb9fb0919c0ff7da1f7a75076b5b38b57f231cad32bb17c9dda3)(@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected]):dependencies:'@expo/config-plugins': 58.0.9(@tsc-rs/[email protected])([email protected])'@expo/config-types': 58.0.3