diff --git a/android/src/main/java/expo/modules/sharing/SharingRecords.kt b/android/src/main/java/expo/modules/sharing/SharingRecords.kt
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 {
internal data class SharePayload(
@Field var value: String = "",
@Field var shareType: ShareType = ShareType.Text,
- @Field var mimeType: String = "text/plain"
+ @Field var mimeType: String = "text/plain",
+ @Field var originalName: String? = null
) : 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 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
import android.content.Context
import android.content.Intent
import android.net.Uri
+import android.provider.OpenableColumns
internal class SimpleShareIntentDataParser {
companion object {
@@ -17,25 +18,26 @@ internal class SimpleShareIntentDataParser {
}
private fun handleSendAction(context: Context, intent: Intent, type: String): List<SharePayload> {
- return if (type == "text/plain") {
- val text = intent.getStringExtra(Intent.EXTRA_TEXT) ?: return emptyList()
- val isUrl = android.util.Patterns.WEB_URL.matcher(text).matches()
-
- listOf(
+ val stream = intent.getParcelableExtraCompat<Uri>(Intent.EXTRA_STREAM)
+ val text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT)?.toString()
+ if (stream == null) {
+ if (!type.startsWith("text/") || text == null) return emptyList()
+ return listOf(
SharePayload().apply {
value = text
- shareType = if (isUrl) {
- ShareType.Url
- } else {
- ShareType.Text
- }
- mimeType = "text/plain"
+ shareType = if (android.util.Patterns.WEB_URL.matcher(text).matches()) ShareType.Url else ShareType.Text
+ mimeType = type
}
)
- } else {
- val uri = intent.getParcelableExtraCompat<Uri>(Intent.EXTRA_STREAM)
- listOfNotNull(uri?.let { createUriPayload(context, it, type) })
}
+ val filePayload = createUriPayload(context, stream, type)
+ if (text.isNullOrBlank()) return listOf(filePayload)
+ val textPayload = SharePayload().apply {
+ value = text
+ shareType = if (android.util.Patterns.WEB_URL.matcher(text).matches()) ShareType.Url else ShareType.Text
+ mimeType = "text/plain"
+ }
+ return listOf(textPayload, filePayload)
}
private fun handleSendMultipleAction(context: Context, intent: Intent, type: String): List<SharePayload> {
@@ -45,10 +47,25 @@ internal class SimpleShareIntentDataParser {
private fun createUriPayload(context: Context, uri: Uri, defaultType: String): SharePayload {
val specificType = context.contentResolver.getType(uri) ?: defaultType
+ // A blank DISPLAY_NAME must stay null: consumers treat originalName as
+ // the attachment name, and an empty name fails contract validation.
+ val displayName = runCatching {
+ context.contentResolver.query(
+ uri,
+ arrayOf(OpenableColumns.DISPLAY_NAME),
+ null,
+ null,
+ null
+ )?.use { cursor ->
+ val column = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
+ if (column >= 0 && cursor.moveToFirst()) cursor.getString(column) else null
+ }
+ }.getOrNull()?.takeIf { it.isNotBlank() }
return SharePayload().apply {
value = uri.toString()
- shareType = ShareType.fromMimeType(specificType)
+ shareType = if (specificType.startsWith("text/")) ShareType.File else ShareType.fromMimeType(specificType)
mimeType = specificType
+ originalName = displayName
}
}
}
diff --git a/build/Sharing.types.d.ts b/build/Sharing.types.d.ts
index 74544e0728d5f845351693d8d90596abae42eaea..92bd2f53108ac717f1ef7292fe89aea05c676c5e 100644
--- a/build/Sharing.types.d.ts
+++ b/build/Sharing.types.d.ts
@@ -100,6 +100,11 @@ export type SharePayload = {
* @default 'text/plain'
*/
mimeType?: string;
+ /**
+ * The display name of a shared file, when the source app provided one.
+ * Populated on Android from the content resolver's `DISPLAY_NAME`.
+ */
+ originalName?: string | null;
};
export type BaseResolvedSharePayload = SharePayload & {
/**
diff --git a/plugin/build/ios/xcode/addPbxGroup.js b/plugin/build/ios/xcode/addPbxGroup.js
index 0867938bdc6df7aeba560cb84dc9c9156c842d95..6ff5be15de702c79cc932def1af68e4c801db48e 100644
--- a/plugin/build/ios/xcode/addPbxGroup.js
+++ b/plugin/build/ios/xcode/addPbxGroup.js
@@ -29,9 +29,23 @@ function addPbxGroup(xcodeProject, targetName, shareExtensionFiles, platformProj
}).flat();
// Add generated files
targetFiles.push(`${targetName}.entitlements`);
- targetFiles.push(`Info.plist`);
const sharedFiles = Object.values(shareExtensionFiles.sharedFiles ?? []).flat().map((file)=>_path().default.join(sharedFilesPath, file)).map((file)=>_path().default.relative(_path().default.join(platformProjectRoot, targetName, 'shared'), file));
- const { uuid: mainGroupUuid } = xcodeProject.addPbxGroup(targetFiles, targetName, targetName);
+ const { uuid: mainGroupUuid, pbxGroup: mainGroup } = xcodeProject.addPbxGroup(targetFiles, targetName, targetName);
+ // Xcode 27.2 rejects file references shared between groups. Bypass
+ // addPbxGroup's path deduplication so the widget keeps its own Info.plist.
+ const infoPlistReference = xcodeProject.generateUuid();
+ const fileReferences = xcodeProject.pbxFileReferenceSection();
+ fileReferences[infoPlistReference] = {
+ isa: 'PBXFileReference',
+ lastKnownFileType: 'text.plist.xml',
+ path: 'Info.plist',
+ sourceTree: '"<group>"'
+ };
+ fileReferences[`${infoPlistReference}_comment`] = 'Info.plist';
+ mainGroup.children.push({
+ value: infoPlistReference,
+ comment: 'Info.plist'
+ });
const { uuid: sharedGroupUuid } = xcodeProject.addPbxGroup(sharedFiles, 'shared', 'shared');
if (mainGroupUuid && sharedGroupUuid) {
// @ts-expect-error: TODO(@kitten): This used to be untyped, and is failing now
diff --git a/plugin/src/ios/xcode/addPbxGroup.ts b/plugin/src/ios/xcode/addPbxGroup.ts
index 943c263c0cf58be595bfc9745c91827da81befec..3a99a1822beb25fea3835123b9a4ad7ac3b4f8d5 100644
--- a/plugin/src/ios/xcode/addPbxGroup.ts
+++ b/plugin/src/ios/xcode/addPbxGroup.ts
@@ -19,14 +19,29 @@ export function addPbxGroup(
// Add generated files
targetFiles.push(`${targetName}.entitlements`);
- targetFiles.push(`Info.plist`);
const sharedFiles = Object.values(shareExtensionFiles.sharedFiles ?? [])
.flat()
.map((file) => path.join(sharedFilesPath, file))
.map((file) => path.relative(path.join(platformProjectRoot, targetName, 'shared'), file));
- const { uuid: mainGroupUuid } = xcodeProject.addPbxGroup(targetFiles, targetName, targetName);
+ const { uuid: mainGroupUuid, pbxGroup: mainGroup } = xcodeProject.addPbxGroup(
+ targetFiles,
+ targetName,
+ targetName
+ );
+ // Xcode 27.2 rejects file references shared between groups. Bypass
+ // addPbxGroup's path deduplication so the widget keeps its own Info.plist.
+ const infoPlistReference = xcodeProject.generateUuid();
+ const fileReferences = xcodeProject.pbxFileReferenceSection();
+ fileReferences[infoPlistReference] = {
+ isa: 'PBXFileReference',
+ lastKnownFileType: 'text.plist.xml',
+ path: 'Info.plist',
+ sourceTree: '"<group>"',
+ };
+ fileReferences[`${infoPlistReference}_comment`] = 'Info.plist';
+ mainGroup.children.push({ value: infoPlistReference, comment: 'Info.plist' });
const { uuid: sharedGroupUuid } = xcodeProject.addPbxGroup(sharedFiles, 'shared', 'shared');
if (mainGroupUuid && sharedGroupUuid) {
diff --git a/src/Sharing.types.ts b/src/Sharing.types.ts
index 18af320420e7c01a8ffbd7fa5347320409f905d8..3a52b70a20efbc588ad5ca974c38c951e5757c97 100644
--- a/src/Sharing.types.ts
+++ b/src/Sharing.types.ts
@@ -108,6 +108,12 @@ export type SharePayload = {
* @default 'text/plain'
*/
mimeType?: string;
+
+ /**
+ * The display name of a shared file, when the source app provided one.
+ * Populated on Android from the content resolver's `DISPLAY_NAME`.
+ */
+ originalName?: string | null;
};
export type BaseResolvedSharePayload = SharePayload & {