Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,14 +637,13 @@ class CreateMessageFragment : BaseFragment<FragmentCreateMessageBinding>(),
}?.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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
Expand All @@ -27,4 +35,33 @@ class SecurityUtilsTest {
val theoreticalFileName = "bla__b_l.a?"
assertEquals(theoreticalFileName, sanitizedFileName)
}

@Test
fun sanitizeFileNameRejectsDirectoryComponents() {
listOf<String?>(
"..", ".", "", 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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
Loading