diff --git a/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipImageCopier.kt b/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipImageCopier.kt new file mode 100644 index 0000000..e684a65 --- /dev/null +++ b/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipImageCopier.kt @@ -0,0 +1,119 @@ +package app.handlive.android.feature.clipboard.system + +import android.content.ClipDescription +import android.content.Context +import android.net.Uri +import app.handlive.android.feature.clipboard.engine.ClipLimits +import app.handlive.android.feature.clipboard.module.ClipFiles +import app.handlive.android.feature.clipboard.module.LocalRead +import app.handlive.android.feature.clipboard.module.ReadFailure +import app.handlive.android.feature.connection.bench.BenchEvent +import app.handlive.android.feature.connection.bench.BenchLog +import java.io.File +import java.io.InputStream +import java.util.UUID + +/** Sensitive flag and read source (`auto` or `manual`) of one local clip. */ +internal data class ClipOrigin( + val sensitive: Boolean, + val source: String, +) + +/** CLIP-03 API 1: copy item 0's image into `cache/clip/` before the clipboard URI grant ends. */ +internal object ClipImageCopier { + fun describedMime(description: ClipDescription): String? = + (0 until description.mimeTypeCount) + .map(description::getMimeType) + .firstOrNull { it.startsWith(IMAGE) } + + /** + * The image, or null when [uri] is not one. `getType` and `openInputStream` run only after + * [ClipUriPolicy.isReadable]. + */ + fun copyIfImage( + context: Context, + description: ClipDescription, + uri: Uri, + origin: ClipOrigin, + files: ClipFiles, + ): LocalRead? { + val mime = imageMimeOf(context, description, uri) ?: return null + val target = files.source(UUID.randomUUID().toString()) + val failure = copyImage(context, uri, target) + if (failure != null) logCopyFailure(uri, origin.source, failure) + return if (failure == null) { + LocalRead.Image(target, mime, origin.sensitive, origin.source) + } else { + LocalRead.Failed(failure, origin.source) + } + } + + private fun imageMimeOf( + context: Context, + description: ClipDescription, + uri: Uri, + ): String? { + if (!ClipUriPolicy.isReadable(context, uri)) return null + val provided = runCatching { context.contentResolver.getType(uri) }.getOrNull() + return provided?.takeIf { it.startsWith(IMAGE) } ?: describedMime(description) + } + + /** Copies the image into [target]; a failure deletes it (E2, E3, E10). */ + private fun copyImage( + context: Context, + uri: Uri, + target: File, + ): ReadFailure? { + if (!ClipUriPolicy.isReadable(context, uri)) { + target.delete() + return ReadFailure.IMAGE_UNREADABLE + } + val copied = runCatching { context.contentResolver.openInputStream(uri)?.use { copyLimited(it, target) } } + val size = copied.getOrNull() + val failure = + when { + copied.exceptionOrNull() is SecurityException -> ReadFailure.PERMISSION_LOST + size == null -> ReadFailure.IMAGE_UNREADABLE + size > ClipLimits.MAX_IMAGE_BYTES -> ReadFailure.IMAGE_TOO_LARGE + else -> null + } + if (failure != null) target.delete() + return failure + } + + /** Copies at most `CLIP_MAX_IMAGE` + 1 bytes (logic 3: one byte more tells "too large"). */ + private fun copyLimited( + input: InputStream, + target: File, + ): Long { + val limit = ClipLimits.MAX_IMAGE_BYTES + 1 + var total = 0L + target.outputStream().use { out -> + val buffer = ByteArray(BUFFER) + while (total < limit) { + val read = input.read(buffer, 0, minOf(BUFFER.toLong(), limit - total).toInt()) + if (read < 0) break + out.write(buffer, 0, read) + total += read + } + } + return total + } + + private fun logCopyFailure( + uri: Uri, + source: String, + failure: ReadFailure, + ) { + BenchLog.event( + BenchEvent.CLIP_READ_FAILED, + "reason" to failure.name.lowercase(), + "stage" to "copy", + "authority" to ClipUriPolicy.logAuthority(uri), + "source" to source, + ) + } + + private const val IMAGE = "image/" + private const val BUFFER = 64 * 1024 +} diff --git a/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipReader.kt b/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipReader.kt index 52ec5b5..3400970 100644 --- a/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipReader.kt +++ b/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipReader.kt @@ -5,15 +5,11 @@ import android.content.ClipDescription import android.content.Context import android.net.Uri import app.handlive.android.core.protocol.clipboard.HtmlClipSanitizer -import app.handlive.android.feature.clipboard.engine.ClipLimits import app.handlive.android.feature.clipboard.module.ClipFiles import app.handlive.android.feature.clipboard.module.LocalRead import app.handlive.android.feature.clipboard.module.ReadFailure import app.handlive.android.feature.connection.bench.BenchEvent import app.handlive.android.feature.connection.bench.BenchLog -import java.io.File -import java.io.InputStream -import java.util.UUID /** * CLIP-01 API 2 logic 2 and CLIP-03 API 1: item 0 of a clip is an image copied into `cache/clip/` right away when its @@ -21,7 +17,8 @@ import java.util.UUID * its text, a text URI is read by the system) with the sanitized `htmlText` beside it when the item has one. The * image wins over a text beside it: browsers and OEM galleries put the image's URL, its alt text or an empty string * next to the URI, and the user copied the picture, not that text. - * Anything else is E3. Runs off the main thread; the content is never logged. + * A URI is opened only after [ClipUriPolicy.isReadable] (CLIP-01 API 2 logic 7). Anything else is E3. Runs off the + * main thread; the content and the URI path are never logged. */ object ClipReader { fun read( @@ -30,41 +27,79 @@ object ClipReader { source: String, files: ClipFiles, ): LocalRead { - val item = - clip?.takeIf { it.itemCount > 0 }?.getItemAt(0) - ?: return unsupported(clip, source, if (clip == null) "no_clip" else "no_item") + if (clip == null || clip.itemCount == 0) { + return unsupported(clip, source, if (clip == null) "no_clip" else "no_item") + } + return fromItem(context, clip, clip.getItemAt(0), source, files) + } + + private fun fromItem( + context: Context, + clip: ClipData, + item: ClipData.Item, + source: String, + files: ClipFiles, + ): LocalRead { val sensitive = SystemClipboard.isSensitive(clip.description) + val refused = refusedUri(context, clip, item, source) val uri = item.uri - val imageMime = uri?.let { imageMimeOf(context, clip.description, it) } - return when { - // CLIP-03 API 1 logic 1 first: an item whose URI is an image is a copied image, text beside it or not. - uri != null && imageMime != null -> { - val target = files.source(UUID.randomUUID().toString()) - copyImage(context, uri, target)?.let { failure -> - // Debug builds only (HLBENCH): which provider refused the read; never the path or the content. - BenchLog.event( - BenchEvent.CLIP_READ_FAILED, - "reason" to failure.name.lowercase(), - "stage" to "copy", - "authority" to (uri.authority?.takeIf { uri.scheme == CONTENT } ?: "-"), - "source" to source, - ) - LocalRead.Failed(failure, source) - } ?: LocalRead.Image(target, imageMime, sensitive, source) + val image = + if (refused == null && uri != null && ClipUriPolicy.isReadable(context, uri)) { + ClipImageCopier.copyIfImage( + context, + clip.description, + uri, + ClipOrigin(sensitive, source), + files, + ) + } else { + null } - - item.text != null || item.htmlText != null -> { - text(item.coerceToText(context), sensitive, source, item.htmlText) + val textual = + if (refused == null && image == null) { + textIfAny(context, clip.description, item, sensitive, source) + } else { + null } + return refused ?: image ?: textual ?: unsupported(clip, source, "neither_text_nor_image") + } - uri != null && clip.description.hasMimeType(TEXT_ANY) -> { - text(item.coerceToText(context), sensitive, source) + /** A refused URI that the next step would open, or null when the item can still be read. */ + private fun refusedUri( + context: Context, + clip: ClipData, + item: ClipData.Item, + source: String, + ): LocalRead? { + val uri = item.uri + if (uri == null || ClipUriPolicy.isReadable(context, uri) || !wouldOpenUri(clip.description, item)) { + return null + } + val failure = + if (ClipImageCopier.describedMime(clip.description) != null) { + ReadFailure.IMAGE_UNREADABLE + } else { + ReadFailure.EMPTY_OR_NOT_TEXT } + return rejectUri(uri, source, failure) + } - else -> { - unsupported(clip, source, "neither_text_nor_image") - } - } + private fun textIfAny( + context: Context, + description: ClipDescription, + item: ClipData.Item, + sensitive: Boolean, + source: String, + ): LocalRead? { + val uri = item.uri + val textual = + item.text != null || + item.htmlText != null || + (uri != null && description.hasMimeType(TEXT_ANY)) + if (!textual) return null + // `htmlText` is the string already on the item. `coerceToHtmlText` is not used: with no text and no + // HTML it opens the URI through `openTypedAssetFileDescriptor`. + return text(plainText(context, item), sensitive, source, item.htmlText) } /** @@ -94,7 +129,7 @@ object ClipReader { "why" to why, "mimes" to mimes.ifEmpty { "-" }, "parts" to parts.ifEmpty { "-" }, - "authority" to (item?.uri?.takeIf { it.scheme == CONTENT }?.authority ?: "-"), + "authority" to (item?.uri?.let(ClipUriPolicy::logAuthority) ?: "-"), "source" to source, ) return LocalRead.Failed(ReadFailure.EMPTY_OR_NOT_TEXT, source) @@ -111,61 +146,45 @@ object ClipReader { } ?: LocalRead.Failed(ReadFailure.EMPTY_OR_NOT_TEXT, source) - /** Logic 1: an image when the description or the provider gives an `image` MIME type. */ - private fun imageMimeOf( - context: Context, + /** + * True when the next step would open the item URI: an image MIME on the description, or `coerceToText` / + * `coerceToHtmlText` because `text` is absent (both open a content URI). + */ + private fun wouldOpenUri( description: ClipDescription, - uri: Uri, - ): String? { - val provided = runCatching { context.contentResolver.getType(uri) }.getOrNull() - val described = - (0 until description.mimeTypeCount) - .map( - description::getMimeType, - ).firstOrNull { it.startsWith(IMAGE) } - return provided?.takeIf { it.startsWith(IMAGE) } ?: described - } + item: ClipData.Item, + ): Boolean = + ClipImageCopier.describedMime(description) != null || + (item.text == null && (item.htmlText != null || description.hasMimeType(TEXT_ANY))) - /** Copies the image into [target]; a failure deletes it (E2, E3, E10). */ - private fun copyImage( + /** + * `coerceToText` opens a URI when `text` is absent. A refused URI is never passed to it: the text + * already on the item is returned as-is, otherwise nothing. HTML stays the item's own `htmlText`. + */ + private fun plainText( context: Context, - uri: Uri, - target: File, - ): ReadFailure? { - val copied = runCatching { context.contentResolver.openInputStream(uri)?.use { copyLimited(it, target) } } - val size = copied.getOrNull() - val failure = - when { - copied.exceptionOrNull() is SecurityException -> ReadFailure.PERMISSION_LOST - size == null -> ReadFailure.IMAGE_UNREADABLE - size > ClipLimits.MAX_IMAGE_BYTES -> ReadFailure.IMAGE_TOO_LARGE - else -> null - } - if (failure != null) target.delete() - return failure + item: ClipData.Item, + ): CharSequence? { + val uri = item.uri + if (uri != null && !ClipUriPolicy.isReadable(context, uri)) return item.text + return item.coerceToText(context) } - /** Copies at most `CLIP_MAX_IMAGE` + 1 bytes (logic 3: one byte more tells "too large"). */ - private fun copyLimited( - input: InputStream, - target: File, - ): Long { - val limit = ClipLimits.MAX_IMAGE_BYTES + 1 - var total = 0L - target.outputStream().use { out -> - val buffer = ByteArray(BUFFER) - while (total < limit) { - val read = input.read(buffer, 0, minOf(BUFFER.toLong(), limit - total).toInt()) - if (read < 0) break - out.write(buffer, 0, read) - total += read - } - } - return total + private fun rejectUri( + uri: Uri, + source: String, + failure: ReadFailure, + ): LocalRead { + BenchLog.event( + BenchEvent.CLIP_READ_FAILED, + "reason" to failure.name.lowercase(), + "stage" to "read", + "why" to "uri_not_allowed", + "authority" to ClipUriPolicy.logAuthority(uri), + "source" to source, + ) + return LocalRead.Failed(failure, source) } - private const val IMAGE = "image/" - private const val CONTENT = "content" private const val TEXT_ANY = "text/*" - private const val BUFFER = 64 * 1024 } diff --git a/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipUriPolicy.kt b/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipUriPolicy.kt new file mode 100644 index 0000000..3632776 --- /dev/null +++ b/feature/clipboard/src/main/kotlin/app/handlive/android/feature/clipboard/system/ClipUriPolicy.kt @@ -0,0 +1,26 @@ +package app.handlive.android.feature.clipboard.system + +import android.content.Context +import android.net.Uri + +/** + * CLIP-01 API 2 logic 7. A clip URI may be opened only when its scheme is `content` and + * `PackageManager.resolveContentProvider` returns a provider in another package. Anything else is refused. + * The path is never logged. + */ +internal object ClipUriPolicy { + fun isReadable( + context: Context, + uri: Uri, + ): Boolean { + val authority = uri.authority + if (uri.scheme != CONTENT || authority.isNullOrEmpty()) return false + val owner = context.packageManager.resolveContentProvider(authority, 0)?.packageName + return owner != null && owner != context.packageName + } + + /** Authority of a `content` URI, or `-`. Never the path. */ + fun logAuthority(uri: Uri): String = uri.authority?.takeIf { uri.scheme == CONTENT } ?: "-" + + private const val CONTENT = "content" +} diff --git a/feature/clipboard/src/test/kotlin/app/handlive/android/feature/clipboard/system/ClipReaderTest.kt b/feature/clipboard/src/test/kotlin/app/handlive/android/feature/clipboard/system/ClipReaderTest.kt index 161bdc0..3cf443b 100644 --- a/feature/clipboard/src/test/kotlin/app/handlive/android/feature/clipboard/system/ClipReaderTest.kt +++ b/feature/clipboard/src/test/kotlin/app/handlive/android/feature/clipboard/system/ClipReaderTest.kt @@ -3,6 +3,8 @@ package app.handlive.android.feature.clipboard.system import android.content.ClipData import android.content.ClipDescription import android.content.Context +import android.content.pm.ApplicationInfo +import android.content.pm.ProviderInfo import android.net.Uri import android.os.PersistableBundle import androidx.test.core.app.ApplicationProvider @@ -10,16 +12,19 @@ import app.handlive.android.feature.clipboard.engine.ClipLimits import app.handlive.android.feature.clipboard.module.ClipFiles import app.handlive.android.feature.clipboard.module.LocalRead import app.handlive.android.feature.clipboard.module.ReadFailure +import app.handlive.android.feature.connection.bench.BenchLog import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config +import org.robolectric.shadows.ShadowLog import java.io.File import java.io.InputStream @@ -32,6 +37,11 @@ class ClipReaderTest { private val files = ClipFiles(dir, System::currentTimeMillis) private val imageUri = Uri.parse("content://com.example.photos/image/1") + @Before + fun registerExternalGallery() { + registerProvider(GALLERY_AUTHORITY, "com.example.photos") + } + private fun read(clip: ClipData?) = ClipReader.read(context, clip, "manual", files) @Test @@ -145,5 +155,93 @@ class ClipReaderTest { assertEquals("Order number: HL-240917-0042", (read(clip) as LocalRead.Text).text) } + @Test + fun anExternalContentProviderImageIsCopied() { + val bytes = byteArrayOf(9, 8, 7) + shadowOf(context.contentResolver).registerInputStream(imageUri, bytes.inputStream()) + val image = read(imageClip()) as LocalRead.Image + assertEquals("image/png", image.mime) + assertArrayEquals(bytes, image.file.readBytes()) + } + + @Test + fun aNonContentImageUriIsNotOpened() { + val emptyScheme = + Uri + .Builder() + .authority(GALLERY_AUTHORITY) + .path("/private-clip-7f3a.png") + .build() + assertNull(emptyScheme.scheme) + listOf( + Uri.parse("file://$GALLERY_AUTHORITY/private-clip-7f3a.png"), + Uri.parse("android.resource://$GALLERY_AUTHORITY/private-res-7f3a"), + emptyScheme, + ).forEach { assertImageRefused(it) } + } + + @Test + fun theAppsOwnProviderIsNotOpened() { + val authority = "app.handlive.clipboard.own" + registerProvider(authority, context.packageName) + assertImageRefused(Uri.parse("content://$authority/clip.png")) + } + + @Test + fun anUnresolvedContentAuthorityIsNotOpened() { + assertImageRefused(Uri.parse("content://com.missing.photos/clip.png")) + } + + @Test + fun aFileTextUriIsNotReadAsItsPath() { + val uri = Uri.parse("file://$GALLERY_AUTHORITY/note.txt") + val clip = ClipData(ClipDescription("note", arrayOf("text/plain")), ClipData.Item(uri)) + assertEquals(ReadFailure.EMPTY_OR_NOT_TEXT, (read(clip) as LocalRead.Failed).reason) + } + private fun imageClip() = ClipData(ClipDescription("photo", arrayOf("image/png")), ClipData.Item(imageUri)) + + private fun registerProvider( + authority: String, + packageName: String, + ) { + shadowOf(context.packageManager).addOrUpdateProvider( + ProviderInfo().apply { + this.authority = authority + name = "$packageName.FakeProvider" + this.packageName = packageName + applicationInfo = ApplicationInfo().apply { this.packageName = packageName } + enabled = true + exported = true + }, + ) + } + + /** A registered stream would be copied if the URI check were skipped. */ + private fun assertImageRefused(uri: Uri) { + val bytes = ByteArray(8) { it.toByte() } + shadowOf(context.contentResolver).registerInputStream(uri, bytes.inputStream()) + val before = clipNames() + context.applicationInfo.flags = context.applicationInfo.flags or ApplicationInfo.FLAG_DEBUGGABLE + BenchLog.install(context) + ShadowLog.clear() + val failed = + read(ClipData(ClipDescription("photo", arrayOf("image/png")), ClipData.Item(uri))) as LocalRead.Failed + assertEquals(ReadFailure.IMAGE_UNREADABLE, failed.reason) + assertEquals(before, clipNames()) + val line = ShadowLog.getLogs().filter { it.tag == BenchLog.TAG }.joinToString("\n") { it.msg } + assertTrue(line, line.contains("why=uri_not_allowed")) + uri.path?.let { path -> assertFalse(line, line.contains(path)) } + } + + private fun clipNames() = + dir + .listFiles() + ?.map { it.name } + ?.toSet() + .orEmpty() + + private companion object { + const val GALLERY_AUTHORITY = "com.example.photos" + } } diff --git a/feature/clipboard/src/test/kotlin/app/handlive/android/feature/clipboard/system/ClipUriPolicyTest.kt b/feature/clipboard/src/test/kotlin/app/handlive/android/feature/clipboard/system/ClipUriPolicyTest.kt new file mode 100644 index 0000000..32603f3 --- /dev/null +++ b/feature/clipboard/src/test/kotlin/app/handlive/android/feature/clipboard/system/ClipUriPolicyTest.kt @@ -0,0 +1,74 @@ +package app.handlive.android.feature.clipboard.system + +import android.content.Context +import android.content.pm.ApplicationInfo +import android.content.pm.ProviderInfo +import android.net.Uri +import androidx.test.core.app.ApplicationProvider +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config + +/** CLIP-01 API 2 logic 7: which clip URIs [ClipUriPolicy] allows the reader to open. */ +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [35]) +class ClipUriPolicyTest { + private val context: Context = ApplicationProvider.getApplicationContext() + + @Test + fun anExternalContentProviderIsReadable() { + registerProvider(GALLERY_AUTHORITY, "com.example.photos") + assertTrue(ClipUriPolicy.isReadable(context, Uri.parse("content://$GALLERY_AUTHORITY/image/1"))) + } + + @Test + fun aNonContentSchemeIsNotReadable() { + val emptyScheme = + Uri + .Builder() + .authority(GALLERY_AUTHORITY) + .path("/clip.png") + .build() + assertNull(emptyScheme.scheme) + assertFalse(ClipUriPolicy.isReadable(context, Uri.parse("file://$GALLERY_AUTHORITY/clip.png"))) + assertFalse(ClipUriPolicy.isReadable(context, Uri.parse("android.resource://$GALLERY_AUTHORITY/1"))) + assertFalse(ClipUriPolicy.isReadable(context, emptyScheme)) + } + + @Test + fun theAppsOwnProviderIsNotReadable() { + val authority = "app.handlive.clipboard.own" + registerProvider(authority, context.packageName) + assertFalse(ClipUriPolicy.isReadable(context, Uri.parse("content://$authority/clip.png"))) + } + + @Test + fun anUnresolvedAuthorityIsNotReadable() { + assertFalse(ClipUriPolicy.isReadable(context, Uri.parse("content://com.missing.photos/clip.png"))) + } + + private fun registerProvider( + authority: String, + packageName: String, + ) { + shadowOf(context.packageManager).addOrUpdateProvider( + ProviderInfo().apply { + this.authority = authority + name = "$packageName.FakeProvider" + this.packageName = packageName + applicationInfo = ApplicationInfo().apply { this.packageName = packageName } + enabled = true + exported = true + }, + ) + } + + private companion object { + const val GALLERY_AUTHORITY = "com.example.photos" + } +}