Deprecate createTempFile/createTempDir functions

Document their potential permission problems.
Suppress this deprecation in tests.
This commit is contained in:
Ilya Gorbunov
2020-10-28 09:47:31 +03:00
parent 31bfc7d4e3
commit 66f5c98505
4 changed files with 46 additions and 13 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -23,11 +23,24 @@ import java.io.IOException
* *
* To create the new file, the [prefix] and the [suffix] may first be adjusted to fit the limitations of the underlying platform. * To create the new file, the [prefix] and the [suffix] may first be adjusted to fit the limitations of the underlying platform.
* *
* **Note:** if the new directory is created in a directory that is shared with all users,
* it may get permissions allowing everyone to read it or its content, thus creating a risk of leaking
* sensitive information stored in this directory.
* To avoid this, it's recommended either to specify an explicit parent [directory] that is not shared widely,
* or to use alternative ways of creating temporary files,
* such as [java.nio.file.Files.createTempDirectory](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#createTempDirectory-java.lang.String-java.nio.file.attribute.FileAttribute...-)
* or the experimental `createTempDirectory` function in the `kotlin.io.path` package.
*
* @return a file object corresponding to a newly-created directory. * @return a file object corresponding to a newly-created directory.
* *
* @throws IOException in case of input/output error. * @throws IOException in case of input/output error.
* @throws IllegalArgumentException if [prefix] is shorter than three symbols. * @throws IllegalArgumentException if [prefix] is shorter than three symbols.
*/ */
@Deprecated(
"Avoid creating temporary directories in the default temp location with this function " +
"due to too wide permissions on the newly created directory. " +
"Use kotlin.io.path.createTempDirectory instead."
)
public fun createTempDir(prefix: String = "tmp", suffix: String? = null, directory: File? = null): File { public fun createTempDir(prefix: String = "tmp", suffix: String? = null, directory: File? = null): File {
val dir = File.createTempFile(prefix, suffix, directory) val dir = File.createTempFile(prefix, suffix, directory)
dir.delete() dir.delete()
@@ -50,11 +63,24 @@ public fun createTempDir(prefix: String = "tmp", suffix: String? = null, directo
* *
* To create the new file, the [prefix] and the [suffix] may first be adjusted to fit the limitations of the underlying platform. * To create the new file, the [prefix] and the [suffix] may first be adjusted to fit the limitations of the underlying platform.
* *
* **Note:** if the new file is created in a directory that is shared with all users,
* it may get permissions allowing everyone to read it, thus creating a risk of leaking
* sensitive information stored in this file.
* To avoid this, it's recommended either to specify an explicit parent [directory] that is not shared widely,
* or to use alternative ways of creating temporary files,
* such as [java.nio.file.Files.createTempFile](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#createTempFile-java.lang.String-java.lang.String-java.nio.file.attribute.FileAttribute...-)
* or the experimental `createTempFile` function in the `kotlin.io.path` package.
*
* @return a file object corresponding to a newly-created file. * @return a file object corresponding to a newly-created file.
* *
* @throws IOException in case of input/output error. * @throws IOException in case of input/output error.
* @throws IllegalArgumentException if [prefix] is shorter than three symbols. * @throws IllegalArgumentException if [prefix] is shorter than three symbols.
*/ */
@Deprecated(
"Avoid creating temporary files in the default temp location with this function " +
"due to too wide permissions on the newly created file. " +
"Use kotlin.io.path.createTempFile instead or resort to java.io.File.createTempFile."
)
public fun createTempFile(prefix: String = "tmp", suffix: String? = null, directory: File? = null): File { public fun createTempFile(prefix: String = "tmp", suffix: String? = null, directory: File? = null): File {
return File.createTempFile(prefix, suffix, directory) return File.createTempFile(prefix, suffix, directory)
} }
@@ -16,7 +16,7 @@ class FileTreeWalkTest {
val referenceFilenames = val referenceFilenames =
listOf("1", "1/2", "1/3", "1/3/4.txt", "1/3/5.txt", "6", "7.txt", "8", "8/9.txt") listOf("1", "1/2", "1/3", "1/3/4.txt", "1/3/5.txt", "6", "7.txt", "8", "8/9.txt")
fun createTestFiles(): File { fun createTestFiles(): File {
val basedir = createTempDir() val basedir = @Suppress("DEPRECATION") createTempDir()
for (name in referenceFilenames) { for (name in referenceFilenames) {
val file = basedir.resolve(name) val file = basedir.resolve(name)
if (file.extension.isEmpty()) if (file.extension.isEmpty())
@@ -52,7 +52,7 @@ class FileTreeWalkTest {
} }
@Test fun singleFile() { @Test fun singleFile() {
val testFile = createTempFile() val testFile = @Suppress("DEPRECATION") createTempFile()
val nonExistantFile = testFile.resolve("foo") val nonExistantFile = testFile.resolve("foo")
try { try {
for (walk in listOf(File::walkTopDown, File::walkBottomUp)) { for (walk in listOf(File::walkTopDown, File::walkBottomUp)) {
@@ -437,6 +437,7 @@ class FileTreeWalkTest {
} }
} }
@Suppress("DEPRECATION")
@Test fun streamFileTree() { @Test fun streamFileTree() {
val dir = createTempDir() val dir = createTempDir()
try { try {
+14 -8
View File
@@ -17,12 +17,14 @@ class FilesTest {
private val isBackslashSeparator = File.separatorChar == '\\' private val isBackslashSeparator = File.separatorChar == '\\'
@Suppress("DEPRECATION")
@Test fun testPath() { @Test fun testPath() {
val fileSuf = System.currentTimeMillis().toString() val fileSuf = System.currentTimeMillis().toString()
val file1 = createTempFile("temp", fileSuf) val file1 = createTempFile("temp", fileSuf)
assertTrue(file1.path.endsWith(fileSuf), file1.path) assertTrue(file1.path.endsWith(fileSuf), file1.path)
} }
@Suppress("DEPRECATION")
@Test fun testCreateTempDir() { @Test fun testCreateTempDir() {
val dirSuf = System.currentTimeMillis().toString() val dirSuf = System.currentTimeMillis().toString()
val dir1 = createTempDir("temp", dirSuf) val dir1 = createTempDir("temp", dirSuf)
@@ -42,6 +44,7 @@ class FilesTest {
dir3.delete() dir3.delete()
} }
@Suppress("DEPRECATION")
@Test fun testCreateTempFile() { @Test fun testCreateTempFile() {
val fileSuf = System.currentTimeMillis().toString() val fileSuf = System.currentTimeMillis().toString()
val file1 = createTempFile("temp", fileSuf) val file1 = createTempFile("temp", fileSuf)
@@ -61,6 +64,7 @@ class FilesTest {
file3.delete() file3.delete()
} }
@Suppress("DEPRECATION")
@Test fun listFilesWithFilter() { @Test fun listFilesWithFilter() {
val dir = createTempDir("temp") val dir = createTempDir("temp")
@@ -366,7 +370,7 @@ class FilesTest {
} }
@Test fun writeReadText() { @Test fun writeReadText() {
val file = createTempFile() val file = @Suppress("DEPRECATION") createTempFile()
try { try {
val expected = String(CharArray(DEFAULT_BUFFER_SIZE * 2) { Random.nextInt(0, 1024).toChar() }) val expected = String(CharArray(DEFAULT_BUFFER_SIZE * 2) { Random.nextInt(0, 1024).toChar() })
file.writeText(expected) file.writeText(expected)
@@ -391,7 +395,7 @@ class FilesTest {
} }
@Test fun writeReadBytes() { @Test fun writeReadBytes() {
val file = createTempFile() val file = @Suppress("DEPRECATION") createTempFile()
try { try {
val expected = Random.nextBytes(DEFAULT_BUFFER_SIZE * 4) val expected = Random.nextBytes(DEFAULT_BUFFER_SIZE * 4)
file.writeBytes(expected) file.writeBytes(expected)
@@ -427,8 +431,8 @@ class FilesTest {
} }
@Test fun testCopyTo() { @Test fun testCopyTo() {
val srcFile = createTempFile() val srcFile = @Suppress("DEPRECATION") createTempFile()
val dstFile = createTempFile() val dstFile = @Suppress("DEPRECATION") createTempFile()
try { try {
srcFile.writeText("Hello, World!") srcFile.writeText("Hello, World!")
assertFailsWith(FileAlreadyExistsException::class, "copy do not overwrite existing file") { assertFailsWith(FileAlreadyExistsException::class, "copy do not overwrite existing file") {
@@ -493,8 +497,8 @@ class FilesTest {
@Test fun copyToNameWithoutParent() { @Test fun copyToNameWithoutParent() {
val currentDir = File("").absoluteFile!! val currentDir = File("").absoluteFile!!
val srcFile = createTempFile() val srcFile = @Suppress("DEPRECATION") createTempFile()
val dstFile = createTempFile(directory = currentDir) val dstFile = @Suppress("DEPRECATION") createTempFile(directory = currentDir)
try { try {
srcFile.writeText("Hello, World!", Charsets.UTF_8) srcFile.writeText("Hello, World!", Charsets.UTF_8)
dstFile.delete() dstFile.delete()
@@ -512,7 +516,7 @@ class FilesTest {
} }
@Test fun deleteRecursively() { @Test fun deleteRecursively() {
val dir = createTempDir() val dir = @Suppress("DEPRECATION") createTempDir()
dir.delete() dir.delete()
dir.mkdir() dir.mkdir()
val subDir = File(dir, "subdir"); val subDir = File(dir, "subdir");
@@ -563,6 +567,7 @@ class FilesTest {
} }
} }
@Suppress("DEPRECATION")
@Test fun copyRecursively() { @Test fun copyRecursively() {
val src = createTempDir() val src = createTempDir()
val dst = createTempDir() val dst = createTempDir()
@@ -629,6 +634,7 @@ class FilesTest {
} }
} }
@Suppress("DEPRECATION")
@Test fun copyRecursivelyWithOverwrite() { @Test fun copyRecursivelyWithOverwrite() {
val src = createTempDir() val src = createTempDir()
val dst = createTempDir() val dst = createTempDir()
@@ -675,7 +681,7 @@ class FilesTest {
} }
@Test fun helpers2() { @Test fun helpers2() {
val file = createTempFile() val file = @Suppress("DEPRECATION") createTempFile()
val writer = file.printWriter() val writer = file.printWriter()
val str1 = "Hello, world!" val str1 = "Hello, world!"
val str2 = "Everything is wonderful!" val str2 = "Everything is wonderful!"
+2 -2
View File
@@ -12,7 +12,7 @@ import kotlin.random.Random
class IOStreamsTest { class IOStreamsTest {
@Test fun testGetStreamOfFile() { @Test fun testGetStreamOfFile() {
val tmpFile = createTempFile() val tmpFile = @Suppress("DEPRECATION") createTempFile()
var writer: Writer? = null var writer: Writer? = null
try { try {
writer = tmpFile.outputStream().writer() writer = tmpFile.outputStream().writer()
@@ -46,7 +46,7 @@ class IOStreamsTest {
} }
@Test fun readWriteBytes() { @Test fun readWriteBytes() {
val file = createTempFile("temp", Random.nextLong().toString()) val file = @Suppress("DEPRECATION") createTempFile("temp", Random.nextLong().toString())
try { try {
val bytes = Random.nextBytes(256_000) val bytes = Random.nextBytes(256_000)