files: zip browsing canonicalises entry names like extraction does
- Commit
2956872686fdf6a96fd3fcb79891e025724f0d71- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 07:53 UTC (5 days ago)
- Parents
2eaef19
classifyZipEntry matched raw entry strings against a string
prefix, so entries like ./top.txt, /abs.txt, docs//b.txt,
docs/./c.txt and docs/../rooted.txt browsed under phantom
'.'/'..' folders or were silently dropped — while
UnzipWorker canonicalises the same entries (via
resolveEntry/canonicalFile) to top.txt, abs.txt, docs/b.txt,
docs/c.txt, rooted.txt. Browsing and extraction disagreed on
where entries live.
Listing now canonicalises: normalizedZipSegments mirrors
canonical File resolution (empty/'.' segments vanish, '..'
pops, a '..' escaping the archive root drops the entry,
exactly as extraction does), classifyZipEntry compares
canonical segment lists, and listZipDirItems keys the
browsed folder canonically. Regression test with seven
non-canonical entries failed pre-fix ('[., docs, ..]'
folders) and passes now. Verified: :files:testDevUnitTest
125 tests pass; lint errors are pre-existing on untouched
files.Diff
files/src/main/java/com/vayunmathur/files/platform/FilesListing.kt
diff --git a/files/src/main/java/com/vayunmathur/files/platform/FilesListing.kt b/files/src/main/java/com/vayunmathur/files/platform/FilesListing.ktindex d8685babc0..2bcac228f8 100644--- a/files/src/main/java/com/vayunmathur/files/platform/FilesListing.kt+++ b/files/src/main/java/com/vayunmathur/files/platform/FilesListing.kt@@ -196,29 +196,40 @@ private class ZipListingAccumulator(private val internalDir: String) { if (internalDir.isEmpty()) first else "$internalDir/$first" } +/**+ * The canonical segments of a zip entry name, mirroring the `File.canonicalFile`+ * resolution [UnzipWorker] extracts through: empty and "." segments vanish and+ * ".." pops the segment before it. A ".." that climbs past the archive root makes+ * the whole name null — extraction drops such an entry, so browsing hides it too.+ */+private fun normalizedZipSegments(path: String): List<String>? {+ val segments = mutableListOf<String>()+ for (segment in path.split('/')) {+ when (segment) {+ "", "." -> Unit+ ".." -> if (segments.isEmpty()) return null else segments.removeAt(segments.lastIndex)+ else -> segments.add(segment)+ }+ }+ return segments+}+ /** Reduces one raw zip entry name to a listing row, or null when it is not shown here. */ private fun classifyZipEntry( rawName: String, isDirectory: Boolean, size: Long, internalDir: String,- prefix: String ): ZipListingEntry? {- val normalized = rawName.trimEnd('/')- if (normalized.isEmpty()) return null- if (normalized == internalDir) return null- if (internalDir.isNotEmpty() &&- !rawName.startsWith(prefix) &&- !normalized.startsWith(prefix)- ) return null- val remainder = if (prefix.isEmpty()) {- normalized- } else {- if (normalized.length <= prefix.length) return null- normalized.substring(prefix.length)- }+ val entrySegments = normalizedZipSegments(rawName) ?: return null+ if (entrySegments.isEmpty()) return null+ val internalSegments = normalizedZipSegments(internalDir) ?: return null+ // The folder being browsed itself, an ancestor of it, or another branch entirely.+ if (entrySegments.size <= internalSegments.size) return null+ if (entrySegments.subList(0, internalSegments.size) != internalSegments) return null+ val remainder = entrySegments.drop(internalSegments.size).joinToString("/") if (remainder.isEmpty()) return null- val fullInner = if (internalDir.isEmpty()) remainder else "$internalDir/$remainder"+ val fullInner = entrySegments.joinToString("/") return ZipListingEntry(fullInner, remainder, isDirectory, size) } @@ -228,10 +239,14 @@ internal fun listZipDirItems( ): Pair<List<FileBrowserItem>, List<FileBrowserItem>> { return try { ZipFile(zipFile).use { zf ->- val prefix = if (internalDir.isEmpty()) "" else "$internalDir/"- val acc = ZipListingAccumulator(internalDir)+ // Browse and build rows under the canonical folder, so a folder reached+ // through a non-canonical internal path still keys its children where+ // extraction would put them.+ val canonicalInternal = normalizedZipSegments(internalDir)+ ?.joinToString("/") ?: return emptyList<FileBrowserItem>() to emptyList()+ val acc = ZipListingAccumulator(canonicalInternal) for (entry in zf.entries()) {- classifyZipEntry(entry.name, entry.isDirectory, entry.size, internalDir, prefix)+ classifyZipEntry(entry.name, entry.isDirectory, entry.size, canonicalInternal) ?.let { acc.addEntry(it) } } acc.dirs.values.toList() to acc.files.values.toList()
files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt
diff --git a/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt b/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.ktindex e5b93c4d2b..89c5e871c8 100644--- a/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt+++ b/files/src/test/java/com/vayunmathur/files/platform/FilesListingTest.kt@@ -116,6 +116,39 @@ class FilesListingTest { dir.deleteRecursively() } + @Test+ fun `list zip normalises non-canonical entry names like extraction does`() {+ val dir = tempDir("zip-normalised")+ // UnzipWorker resolves every entry through File.canonicalFile, so these+ // all land at plain canonical paths on extraction: top.txt, abs.txt and+ // rooted.txt at the root, docs/b.txt and docs/c.txt inside docs.+ // Browsing must show that same layout, not phantom "." folders or drops.+ val zip = makeZip(+ dir, "a.zip",+ mapOf(+ "./top.txt" to "top",+ "/abs.txt" to "abs",+ "docs//b.txt" to "b",+ "docs/./c.txt" to "c",+ "docs/../rooted.txt" to "rooted",+ "../evil.txt" to "evil",+ "docs/../../evil2.txt" to "evil2",+ ),+ )+ val (dirs, files) = listZipDirItems(zip, "")+ assertEquals(setOf("docs"), dirs.map { it.name }.toSet())+ assertEquals(setOf("top.txt", "abs.txt", "rooted.txt"), files.map { it.name }.toSet())+ assertEquals("top.txt", files.first { it.name == "top.txt" }.zipInnerPath)+ assertEquals("abs.txt", files.first { it.name == "abs.txt" }.zipInnerPath)++ val (subDirs, subFiles) = listZipDirItems(zip, "docs")+ assertTrue(subDirs.isEmpty())+ assertEquals(setOf("b.txt", "c.txt"), subFiles.map { it.name }.toSet())+ assertEquals("docs/b.txt", subFiles.first { it.name == "b.txt" }.zipInnerPath)+ assertEquals("docs/c.txt", subFiles.first { it.name == "c.txt" }.zipInnerPath)+ dir.deleteRecursively()+ }+ @Test fun `list zip of corrupt or missing file is empty`() { val dir = tempDir("badzip")