diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/security/SecurityUtils.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/security/SecurityUtils.kt index 4edb8175f..af3cec93b 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/security/SecurityUtils.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/security/SecurityUtils.kt @@ -186,9 +186,14 @@ class SecurityUtils { /** * Filter an input file name to use only a safe part. + * + * The result is a single path component that cannot resolve to the directory itself or its parent. */ fun sanitizeFileName(originalFileName: String?): String { - return originalFileName?.split("/")?.lastOrNull()?.replace("\\", "") ?: "unnamed" + val safeName = originalFileName?.split("/")?.lastOrNull()?.replace("\\", "") + return safeName?.takeIf { + it.isNotEmpty() && it != "." && it != ".." && it.none { char -> char.isISOControl() } + } ?: "unnamed" } /** diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/service/ProcessingOutgoingMessageInfoHelper.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/service/ProcessingOutgoingMessageInfoHelper.kt index 424e2d05d..d30b56885 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/service/ProcessingOutgoingMessageInfoHelper.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/service/ProcessingOutgoingMessageInfoHelper.kt @@ -133,7 +133,7 @@ object ProcessingOutgoingMessageInfoHelper { val originalAttName = attachmentInfo.getSafeName() if (attachmentInfo.isEncryptionAllowed && outgoingMsgInfo.encryptionType === MessageEncryptionType.ENCRYPTED) { val fileName = originalAttName + "." + Constants.PGP_FILE_EXT - var encryptedTempFile = File(attsCacheDir, fileName) + var encryptedTempFile = FileAndDirectoryUtils.getFileInsideDir(attsCacheDir, fileName) if (encryptedTempFile.exists()) { encryptedTempFile = FileAndDirectoryUtils.createFileWithIncreasedIndex( @@ -149,7 +149,7 @@ object ProcessingOutgoingMessageInfoHelper { uri = Uri.fromFile(encryptedTempFile) name = encryptedTempFile.name } else { - var cachedAtt = File(attsCacheDir, originalAttName) + var cachedAtt = FileAndDirectoryUtils.getFileInsideDir(attsCacheDir, originalAttName) if (cachedAtt.exists()) { cachedAtt = FileAndDirectoryUtils.createFileWithIncreasedIndex(attsCacheDir, cachedAtt.name) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt index 9b3927197..9813f19ba 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt @@ -637,14 +637,13 @@ class CreateMessageFragment : BaseFragment(), }?.forEach { attachmentInfo -> val uri = attachmentInfo.uri ?: return@forEach if (hasAbilityToAddAtt(attachmentInfo)) { - if (attachmentInfo.getSafeName().isEmpty()) { - val msg = "attachmentInfo.getName() is empty, uri = $uri" - ExceptionUtil.handleError(NullPointerException(msg)) - return - } - val fileName = attachmentInfo.getSafeName() - var draftAtt = File(draftCacheDir, fileName) + var draftAtt = try { + FileAndDirectoryUtils.getFileInsideDir(draftCacheDir, fileName) + } catch (e: IOException) { + LogsUtil.d(TAG, "Rejected unsafe attachment name", e) + return@forEach + } draftAtt = if (draftAtt.exists()) { FileAndDirectoryUtils.createFileWithIncreasedIndex(draftCacheDir, fileName) diff --git a/FlowCrypt/src/test/java/com/flowcrypt/email/security/SecurityUtilsTest.kt b/FlowCrypt/src/test/java/com/flowcrypt/email/security/SecurityUtilsTest.kt index 4ea1f018d..3841e85de 100644 --- a/FlowCrypt/src/test/java/com/flowcrypt/email/security/SecurityUtilsTest.kt +++ b/FlowCrypt/src/test/java/com/flowcrypt/email/security/SecurityUtilsTest.kt @@ -6,12 +6,20 @@ package com.flowcrypt.email.security import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Rule import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File /** * @author Denys Bondarenko */ class SecurityUtilsTest { + @get:Rule + val temporaryFolder: TemporaryFolder = TemporaryFolder() + @Test fun sanitizeFileNameWithSlashes() { val fileName = ".**/../#./%%./../sy-s/bl\\a_\\_b\\\\_l.a?" @@ -27,4 +35,33 @@ class SecurityUtilsTest { val theoreticalFileName = "bla__b_l.a?" assertEquals(theoreticalFileName, sanitizedFileName) } + + @Test + fun sanitizeFileNameRejectsDirectoryComponents() { + listOf( + "..", ".", "", null, "foo/..", "a/../..", "..\\", + "bad\u0000name", "bad\nname", "bad\tname", "bad\u007Fname" + ).forEach { name -> + assertEquals("unnamed", SecurityUtils.sanitizeFileName(name)) + } + } + + @Test + fun sanitizedNameStaysInsideDirectory() { + val directory = temporaryFolder.newFolder("attachments").canonicalFile + val prefix = directory.path + File.separator + + listOf( + "../../../../databases/flowcrypt.db-journal", + "..", + ".", + "subdir/../../escape", + "..\\..\\flowcrypt.db-journal", + "" + ).forEach { name -> + val file = File(directory, SecurityUtils.sanitizeFileName(name)).canonicalFile + assertTrue("'$name' resolved to $file", file.path.startsWith(prefix)) + assertFalse(file.path == directory.path) + } + } } diff --git a/FlowCrypt/src/test/java/com/flowcrypt/email/util/GetFileInsideDirTest.kt b/FlowCrypt/src/test/java/com/flowcrypt/email/util/GetFileInsideDirTest.kt index c91e32b2c..4445a4c62 100644 --- a/FlowCrypt/src/test/java/com/flowcrypt/email/util/GetFileInsideDirTest.kt +++ b/FlowCrypt/src/test/java/com/flowcrypt/email/util/GetFileInsideDirTest.kt @@ -4,6 +4,7 @@ package com.flowcrypt.email.util +import com.flowcrypt.email.security.SecurityUtils import org.junit.Assert.assertEquals import org.junit.Assert.assertThrows import org.junit.Assume.assumeNoException @@ -136,6 +137,20 @@ class GetFileInsideDirTest { } } + @Test + fun rejectSymlinkNamedAfterSanitizedTraversalName() { + val directory = temporaryFolder.newFolder("forwarded") + val outsideFile = File(temporaryFolder.newFolder("outside"), "flowcrypt.db-journal").apply { + createNewFile() + } + val safeName = SecurityUtils.sanitizeFileName("../../../../databases/flowcrypt.db-journal") + createSymbolicLinkOrSkip(File(directory, safeName), outsideFile) + + assertThrows(IOException::class.java) { + FileAndDirectoryUtils.getFileInsideDir(directory, safeName) + } + } + @Test fun rejectInvalidFileName() { val directory = temporaryFolder.newFolder("forwarded")