diff --git a/app/src/main/java/com/droiddeck/launcher/files/AppFilesProvider.kt b/app/src/main/java/com/droiddeck/launcher/files/AppFilesProvider.kt index 9f534b39be..affd27293d 100644 --- a/app/src/main/java/com/droiddeck/launcher/files/AppFilesProvider.kt +++ b/app/src/main/java/com/droiddeck/launcher/files/AppFilesProvider.kt @@ -13,6 +13,7 @@ import android.provider.DocumentsContract.Root import android.provider.DocumentsProvider import android.webkit.MimeTypeMap import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionPrefs import java.io.File import java.io.FileNotFoundException import java.io.IOException @@ -26,11 +27,21 @@ import java.nio.file.attribute.BasicFileAttributes class AppFilesProvider : DocumentsProvider() { private lateinit var base: File private lateinit var baseCanonical: String + private lateinit var privatePaths: List override fun attachInfo(context: Context, info: ProviderInfo) { super.attachInfo(context, info) base = context.dataDir baseCanonical = base.canonicalPath + // The wireless ADB pairing identity, preferences, store logins and Steam's saved login stay app-only. + privatePaths = listOf( + context.noBackupFilesDir, + File(base, "shared_prefs"), + context.getDatabasePath("x").parentFile!!, + File(context.filesDir, "stores"), + File(SessionPrefs.steamRoot(context, "arm64"), "config"), + File(SessionPrefs.steamRoot(context, "x86_64"), "config"), + ).flatMap { listOf(it.absoluteFile.normalize().path, runCatching { it.canonicalPath }.getOrDefault(it.path)) }.distinct() } override fun onCreate() = true @@ -74,6 +85,7 @@ class AppFilesProvider : DocumentsProvider() { var file = File(parent, name) var n = 2 while (file.exists()) file = File(parent, "$name (${n++})") + if (isPrivate(file)) throw FileNotFoundException("could not create ${file.path}") val made = try { if (mimeType == Document.MIME_TYPE_DIR) file.mkdir() else file.createNewFile() } catch (e: IOException) { @@ -85,7 +97,7 @@ class AppFilesProvider : DocumentsProvider() { override fun deleteDocument(documentId: String) { val file = fileFor(documentId) - if (file.canonicalPath == baseCanonical) throw FileNotFoundException("the app folder itself is not deleted") + if (file.canonicalPath == baseCanonical || holdsPrivate(file)) throw FileNotFoundException("$documentId holds the app's private files") try { deleteTree(file.toPath()) } catch (e: IOException) { @@ -116,17 +128,19 @@ class AppFilesProvider : DocumentsProvider() { override fun renameDocument(documentId: String, displayName: String): String { val file = fileFor(documentId) + if (holdsPrivate(file)) throw FileNotFoundException("$documentId holds the app's private files") val target = File(file.parentFile, displayName.substringAfterLast('/')) - if (target.exists() || !file.renameTo(target)) throw FileNotFoundException("could not rename $documentId") + if (target.exists() || isPrivate(target) || !file.renameTo(target)) throw FileNotFoundException("could not rename $documentId") return target.absolutePath } override fun moveDocument(sourceDocumentId: String, sourceParentDocumentId: String, targetParentDocumentId: String): String { val source = fileFor(sourceDocumentId) + if (holdsPrivate(source)) throw FileNotFoundException("$sourceDocumentId holds the app's private files") val targetParent = fileFor(targetParentDocumentId) if (!targetParent.isDirectory) throw FileNotFoundException("$targetParentDocumentId is not a folder") val target = File(targetParent, source.name) - if (target.exists() || !source.renameTo(target)) throw FileNotFoundException("could not move $sourceDocumentId") + if (target.exists() || isPrivate(target) || !source.renameTo(target)) throw FileNotFoundException("could not move $sourceDocumentId") return target.absolutePath } @@ -163,7 +177,19 @@ class AppFilesProvider : DocumentsProvider() { val abs = f.absoluteFile.normalize().path if (abs != base.absolutePath && !abs.startsWith(base.absolutePath + "/")) return false val real = runCatching { f.canonicalPath }.getOrNull() ?: return false - return real == baseCanonical || real.startsWith("$baseCanonical/") + if (real != baseCanonical && !real.startsWith("$baseCanonical/")) return false + return !isPrivate(f) + } + + private fun isPrivate(f: File): Boolean { + val real = runCatching { f.canonicalPath }.getOrNull() ?: return true + val paths = listOf(f.absoluteFile.normalize().path, real) + return privatePaths.any { p -> paths.any { it == p || it.startsWith("$p/") } } + } + + private fun holdsPrivate(f: File): Boolean { + val paths = listOfNotNull(f.absoluteFile.normalize().path, runCatching { f.canonicalPath }.getOrNull()) + return privatePaths.any { p -> paths.any { p.startsWith("$it/") } } } private fun addRow(result: MatrixCursor, file: File) { @@ -173,7 +199,7 @@ class AppFilesProvider : DocumentsProvider() { } else if (file.canWrite()) { flags = flags or Document.FLAG_SUPPORTS_WRITE } - if (file.absolutePath != base.absolutePath && file.parentFile?.canWrite() == true) { + if (file.absolutePath != base.absolutePath && file.parentFile?.canWrite() == true && !holdsPrivate(file)) { flags = flags or Document.FLAG_SUPPORTS_DELETE or Document.FLAG_SUPPORTS_RENAME or Document.FLAG_SUPPORTS_MOVE or Document.FLAG_SUPPORTS_REMOVE } diff --git a/app/src/test/java/com/droiddeck/launcher/files/AppFilesProviderTest.kt b/app/src/test/java/com/droiddeck/launcher/files/AppFilesProviderTest.kt new file mode 100644 index 0000000000..6a413cc81a --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/files/AppFilesProviderTest.kt @@ -0,0 +1,105 @@ +package com.droiddeck.launcher.files + +import android.Manifest +import android.content.pm.ProviderInfo +import android.database.Cursor +import android.provider.DocumentsContract.Document +import com.droiddeck.launcher.session.SessionPrefs +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertThrows +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.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File +import java.io.FileNotFoundException + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class AppFilesProviderTest { + private val app get() = RuntimeEnvironment.getApplication() + private lateinit var provider: AppFilesProvider + private lateinit var base: File + + @Before fun setUp() { + base = app.dataDir + provider = AppFilesProvider() + provider.attachInfo(app, ProviderInfo().apply { + authority = "${app.packageName}.documents" + exported = true + grantUriPermissions = true + readPermission = Manifest.permission.MANAGE_DOCUMENTS + writePermission = Manifest.permission.MANAGE_DOCUMENTS + }) + } + + private fun file(path: String, text: String = "x") = File(base, path).apply { parentFile!!.mkdirs(); writeText(text) } + + private fun children(dir: File): List { + val c: Cursor = provider.queryChildDocuments(dir.absolutePath, null, null as String?) + try { + val names = mutableListOf() + while (c.moveToNext()) names += c.getString(c.getColumnIndexOrThrow(Document.COLUMN_DISPLAY_NAME)) + return names + } finally { + c.close() + } + } + + @Test fun listingHidesPrivateFolders() { + file("no_backup/adbkey") + file("shared_prefs/settings.xml") + file("databases/app.db") + file("files/stores/epic/auth.json") + file("files/saves/slot1.sav") + val steam = SessionPrefs.steamRoot(app, "arm64") + File(steam, "config/loginusers.vdf").apply { parentFile!!.mkdirs(); writeText("x") } + File(steam, "steamapps/game.txt").apply { parentFile!!.mkdirs(); writeText("x") } + + val root = children(base) + assertFalse(root.contains("no_backup")) + assertFalse(root.contains("shared_prefs")) + assertFalse(root.contains("databases")) + assertTrue(root.contains("files")) + val files = children(File(base, "files")) + assertFalse(files.contains("stores")) + assertTrue(files.contains("saves")) + val steamChildren = children(steam) + assertFalse(steamChildren.contains("config")) + assertTrue(steamChildren.contains("steamapps")) + } + + @Test fun privateFilesDoNotOpen() { + val key = file("no_backup/adbkey") + val prefs = file("shared_prefs/settings.xml") + val login = File(SessionPrefs.steamRoot(app, "x86_64"), "config/loginusers.vdf").apply { parentFile!!.mkdirs(); writeText("x") } + listOf(key, prefs, login).forEach { f -> + assertThrows(FileNotFoundException::class.java) { provider.openDocument(f.absolutePath, "r", null) } + assertThrows(FileNotFoundException::class.java) { provider.queryDocument(f.absolutePath, null) } + assertThrows(FileNotFoundException::class.java) { provider.deleteDocument(f.absolutePath) } + } + assertTrue(key.exists()) + } + + @Test fun ordinaryFileStillOpens() { + val save = file("files/saves/slot1.sav", "progress") + provider.openDocument(save.absolutePath, "r", null).use { pfd -> + assertEquals("progress", android.os.ParcelFileDescriptor.AutoCloseInputStream(pfd).bufferedReader().readText()) + } + } + + @Test fun privateFoldersCannotBeCreatedOrCarriedOut() { + File(base, "databases").deleteRecursively() + assertThrows(FileNotFoundException::class.java) { provider.createDocument(base.absolutePath, Document.MIME_TYPE_DIR, "databases") } + assertFalse(File(base, "databases").exists()) + file("files/stores/epic/auth.json") + val files = File(base, "files") + assertThrows(FileNotFoundException::class.java) { provider.renameDocument(files.absolutePath, "elsewhere") } + assertThrows(FileNotFoundException::class.java) { provider.deleteDocument(files.absolutePath) } + assertTrue(File(files, "stores/epic/auth.json").exists()) + } +}