files: creating a file can no longer escape the current folder
- Commit
e7e023e7bc09376af1242738b7c6cd1abacc8904- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 19:04 UTC (5 days ago)
- Parents
2cf9209
The New File/Folder dialog passed its raw text straight to File(dir, name), so typing ../escaped.txt created the file in the parent directory and sub/nested created a folder inside sub/ — invisible in the refreshed listing, a silent divergence. Every sibling path (rename, archive, incoming-share save, SAF picker) already enforced the plain-name rule via sanitizedFileName(); create was the sole outlier and the dialog does no sanitisation. createDestination now sanitises the name the same way rename does: path-like input reduces to its leaf in the current folder, and '.'/'..' are a no-op. New regression test failed pre-fix (file landed in the parent) and passes now. Copy/move/delete, SAF handling, sort comparators and zip extraction were audited and cleared. Verified: :files:testDevUnitTest green (23 suites); lint findings are pre-existing on untouched files.
Diff
files/src/main/java/com/vayunmathur/files/platform/FilesEditModel.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesEditModel.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesEditModel.ktindex 9f74cd8933..57d579aa27 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesEditModel.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesEditModel.kt@@ -21,9 +21,14 @@ abstract class FilesEditModel(application: Application) : CreateActions { internal fun createDestination(trimmedName: String, mkdir: Boolean) {+ // The dialog passes raw text, which may contain separators: keep the+ // creation inside the current folder, the same rule rename, archive+ // and the share paths enforce via sanitizedFileName. A blank name or+ // a bare "." / ".." is a no-op, like a blank create name.+ val safeName = sanitizedFileName(trimmedName) ?: return viewModelScope.launch(Dispatchers.IO) { try {- val dest = uniqueDestination(currentDirectoryState.value, trimmedName)+ val dest = uniqueDestination(currentDirectoryState.value, safeName) val created = if (mkdir) dest.mkdirs() else dest.createNewFile() if (created) { loadDirectory()
files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt b/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.ktindex 3e013c62d0..45f1e39a3b 100644--- a/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt+++ b/files/src/test/java/com/vayunmathur/files/platform/FilesViewModelTest.kt@@ -190,6 +190,23 @@ class FilesViewModelTest { assertFalse(File(dirA, "").let { it.exists() && it != dirA }) } + @Test+ fun `create with a path-like name stays in the current folder`() {+ vm.createFile("../escaped.txt")+ await { File(dirA, "escaped.txt").isFile || File(base, "escaped.txt").isFile }+ assertTrue(File(dirA, "escaped.txt").isFile, "the new file must be created in the current folder")+ assertFalse(File(base, "escaped.txt").exists(), "the new file must not escape the current folder")+ vm.createFolder("sub/nested-dir")+ await { File(dirA, "nested-dir").isDirectory || File(dirA, "sub/nested-dir").isDirectory }+ assertTrue(File(dirA, "nested-dir").isDirectory, "the new folder must be created in the current folder")+ assertFalse(File(dirA, "sub/nested-dir").exists(), "the new folder must not be created in a subfolder")+ // a bare parent reference is a no-op, like a blank create name+ vm.createFile("..")+ vm.createFolder("..")+ Thread.sleep(400); ShadowLooper.idleMainLooper()+ assertTrue(dirA.isDirectory)+ }+ @Test fun `rename moves the file and clears selection`() { val one = itemOf("one.txt")