Skip to content

Commit 6b2eb35

Browse files
fix(mobile): shared file names can no longer write outside the Android cache
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dc84b09 commit 6b2eb35

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

‎patches/[email protected]‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/android/src/main/java/expo/modules/sharing/SharingRecords.kt b/android/src/main/java/expo/modules/sharing/SharingRecords.kt
2-
index 1113a0a..e89529a 100644
2+
index 1113a0adbb951081dea5214a711fe1dbb5a3e814..e89529a43258ae3bd6ec730c19453aed5e5efac4 100644
33
--- a/android/src/main/java/expo/modules/sharing/SharingRecords.kt
44
+++ b/android/src/main/java/expo/modules/sharing/SharingRecords.kt
55
@@ -51,7 +51,8 @@ enum class ContentType(val value: String) : Enumerable {
@@ -12,8 +12,56 @@ index 1113a0a..e89529a 100644
1212
) : Record
1313

1414
@OptimizedRecord
15+
diff --git a/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt b/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt
16+
index 0a4c92e4b6459632933027827a86a4ec994ce72f..54ff2b5f44df740c9b971e409397e4a08a973717 100644
17+
--- a/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt
18+
+++ b/android/src/main/java/expo/modules/sharing/dataParsers/ResolvingShareIntentDataParser.kt
19+
@@ -9,6 +9,7 @@ import java.io.File
20+
import java.io.FileOutputStream
21+
import java.net.HttpURLConnection
22+
import java.net.URL
23+
+import java.util.UUID
24+
import kotlin.text.indexOf
25+
import kotlin.text.substring
26+
27+
@@ -56,14 +57,20 @@ class ResolvingShareIntentDataParser {
28+
29+
private fun resolveUri(context: Context, uri: Uri, mimeType: String): ResolvedSharePayload {
30+
val contentResolver = context.contentResolver
31+
- val fileName = getFileName(contentResolver, uri) ?: "unknown"
32+
+ val fileName = safeFileName(getFileName(contentResolver, uri))
33+
val fileSize = getFileSize(contentResolver, uri)
34+
val specificMimeType = contentResolver.getType(uri) ?: mimeType
35+
val type = ShareType.fromMimeType(specificMimeType)
36+
37+
- // Copy to cache dir to ensure access
38+
- val file = File(context.cacheDir, fileName)
39+
+ // The sending app controls the name, so copy into a fresh directory and refuse any
40+
+ // destination that resolves outside cacheDir.
41+
+ val cacheDir = context.cacheDir.canonicalFile
42+
+ val file = File(File(cacheDir, "shared-${UUID.randomUUID()}"), fileName)
43+
+ if (!file.canonicalPath.startsWith(cacheDir.path + File.separator)) {
44+
+ throw SecurityException("Shared file name escapes the cache directory")
45+
+ }
46+
try {
47+
+ file.parentFile?.mkdirs()
48+
contentResolver.openInputStream(uri)?.use { input ->
49+
FileOutputStream(file).use { output ->
50+
input.copyTo(output)
51+
@@ -85,6 +92,11 @@ class ResolvingShareIntentDataParser {
52+
}
53+
}
54+
55+
+ private fun safeFileName(name: String?): String {
56+
+ val baseName = name?.let { File(it).name }
57+
+ return if (baseName.isNullOrBlank() || baseName == "." || baseName == "..") "unknown" else baseName
58+
+ }
59+
+
60+
private fun getFileName(resolver: ContentResolver, uri: Uri): String? {
61+
val cursor = resolver.query(uri, null, null, null, null)
62+
val name = cursor?.use { cursor ->
1563
diff --git a/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt b/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt
16-
index 3381c5f..481cdb4 100644
64+
index 3381c5fd207ebb4cd8e0301aac59d307939ff162..481cdb4bc64ea5ae49919071f686324f6d56d55c 100644
1765
--- a/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt
1866
+++ b/android/src/main/java/expo/modules/sharing/dataParsers/SimpleShareIntentDataParser.kt
1967
@@ -3,6 +3,7 @@ package expo.modules.sharing
@@ -93,7 +141,7 @@ index 3381c5f..481cdb4 100644
93141
}
94142
}
95143
diff --git a/build/Sharing.types.d.ts b/build/Sharing.types.d.ts
96-
index 74544e0..92bd2f5 100644
144+
index 74544e0728d5f845351693d8d90596abae42eaea..92bd2f53108ac717f1ef7292fe89aea05c676c5e 100644
97145
--- a/build/Sharing.types.d.ts
98146
+++ b/build/Sharing.types.d.ts
99147
@@ -100,6 +100,11 @@ export type SharePayload = {
@@ -175,7 +223,7 @@ index 943c263c0cf58be595bfc9745c91827da81befec..3a99a1822beb25fea3835123b9a4ad7a
175223

176224
if (mainGroupUuid && sharedGroupUuid) {
177225
diff --git a/src/Sharing.types.ts b/src/Sharing.types.ts
178-
index 18af320..3a52b70 100644
226+
index 18af320420e7c01a8ffbd7fa5347320409f905d8..3a52b70a20efbc588ad5ca974c38c951e5757c97 100644
179227
--- a/src/Sharing.types.ts
180228
+++ b/src/Sharing.types.ts
181229
@@ -108,6 +108,12 @@ export type SharePayload = {

‎pnpm-lock.yaml‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)