diff --git a/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt b/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt index 1a4a9fa3567..c376916f5af 100644 --- a/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt +++ b/libraries/stdlib/jvm/src/kotlin/io/files/Utils.kt @@ -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. */ @@ -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. * + * **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. * * @throws IOException in case of input/output error. * @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 { val dir = File.createTempFile(prefix, suffix, directory) 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. * + * **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. * * @throws IOException in case of input/output error. * @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 { return File.createTempFile(prefix, suffix, directory) } diff --git a/libraries/stdlib/jvm/test/io/FileTreeWalks.kt b/libraries/stdlib/jvm/test/io/FileTreeWalks.kt index a980a58ec7e..8d5ac446cfc 100644 --- a/libraries/stdlib/jvm/test/io/FileTreeWalks.kt +++ b/libraries/stdlib/jvm/test/io/FileTreeWalks.kt @@ -16,7 +16,7 @@ class FileTreeWalkTest { val referenceFilenames = listOf("1", "1/2", "1/3", "1/3/4.txt", "1/3/5.txt", "6", "7.txt", "8", "8/9.txt") fun createTestFiles(): File { - val basedir = createTempDir() + val basedir = @Suppress("DEPRECATION") createTempDir() for (name in referenceFilenames) { val file = basedir.resolve(name) if (file.extension.isEmpty()) @@ -52,7 +52,7 @@ class FileTreeWalkTest { } @Test fun singleFile() { - val testFile = createTempFile() + val testFile = @Suppress("DEPRECATION") createTempFile() val nonExistantFile = testFile.resolve("foo") try { for (walk in listOf(File::walkTopDown, File::walkBottomUp)) { @@ -437,6 +437,7 @@ class FileTreeWalkTest { } } + @Suppress("DEPRECATION") @Test fun streamFileTree() { val dir = createTempDir() try { diff --git a/libraries/stdlib/jvm/test/io/Files.kt b/libraries/stdlib/jvm/test/io/Files.kt index 3248b36dc9c..a5db29691b2 100644 --- a/libraries/stdlib/jvm/test/io/Files.kt +++ b/libraries/stdlib/jvm/test/io/Files.kt @@ -17,12 +17,14 @@ class FilesTest { private val isBackslashSeparator = File.separatorChar == '\\' + @Suppress("DEPRECATION") @Test fun testPath() { val fileSuf = System.currentTimeMillis().toString() val file1 = createTempFile("temp", fileSuf) assertTrue(file1.path.endsWith(fileSuf), file1.path) } + @Suppress("DEPRECATION") @Test fun testCreateTempDir() { val dirSuf = System.currentTimeMillis().toString() val dir1 = createTempDir("temp", dirSuf) @@ -42,6 +44,7 @@ class FilesTest { dir3.delete() } + @Suppress("DEPRECATION") @Test fun testCreateTempFile() { val fileSuf = System.currentTimeMillis().toString() val file1 = createTempFile("temp", fileSuf) @@ -61,6 +64,7 @@ class FilesTest { file3.delete() } + @Suppress("DEPRECATION") @Test fun listFilesWithFilter() { val dir = createTempDir("temp") @@ -366,7 +370,7 @@ class FilesTest { } @Test fun writeReadText() { - val file = createTempFile() + val file = @Suppress("DEPRECATION") createTempFile() try { val expected = String(CharArray(DEFAULT_BUFFER_SIZE * 2) { Random.nextInt(0, 1024).toChar() }) file.writeText(expected) @@ -391,7 +395,7 @@ class FilesTest { } @Test fun writeReadBytes() { - val file = createTempFile() + val file = @Suppress("DEPRECATION") createTempFile() try { val expected = Random.nextBytes(DEFAULT_BUFFER_SIZE * 4) file.writeBytes(expected) @@ -427,8 +431,8 @@ class FilesTest { } @Test fun testCopyTo() { - val srcFile = createTempFile() - val dstFile = createTempFile() + val srcFile = @Suppress("DEPRECATION") createTempFile() + val dstFile = @Suppress("DEPRECATION") createTempFile() try { srcFile.writeText("Hello, World!") assertFailsWith(FileAlreadyExistsException::class, "copy do not overwrite existing file") { @@ -493,8 +497,8 @@ class FilesTest { @Test fun copyToNameWithoutParent() { val currentDir = File("").absoluteFile!! - val srcFile = createTempFile() - val dstFile = createTempFile(directory = currentDir) + val srcFile = @Suppress("DEPRECATION") createTempFile() + val dstFile = @Suppress("DEPRECATION") createTempFile(directory = currentDir) try { srcFile.writeText("Hello, World!", Charsets.UTF_8) dstFile.delete() @@ -512,7 +516,7 @@ class FilesTest { } @Test fun deleteRecursively() { - val dir = createTempDir() + val dir = @Suppress("DEPRECATION") createTempDir() dir.delete() dir.mkdir() val subDir = File(dir, "subdir"); @@ -563,6 +567,7 @@ class FilesTest { } } + @Suppress("DEPRECATION") @Test fun copyRecursively() { val src = createTempDir() val dst = createTempDir() @@ -629,6 +634,7 @@ class FilesTest { } } + @Suppress("DEPRECATION") @Test fun copyRecursivelyWithOverwrite() { val src = createTempDir() val dst = createTempDir() @@ -675,7 +681,7 @@ class FilesTest { } @Test fun helpers2() { - val file = createTempFile() + val file = @Suppress("DEPRECATION") createTempFile() val writer = file.printWriter() val str1 = "Hello, world!" val str2 = "Everything is wonderful!" diff --git a/libraries/stdlib/jvm/test/io/IOStreams.kt b/libraries/stdlib/jvm/test/io/IOStreams.kt index d616d886a2b..03d0a6fa32b 100644 --- a/libraries/stdlib/jvm/test/io/IOStreams.kt +++ b/libraries/stdlib/jvm/test/io/IOStreams.kt @@ -12,7 +12,7 @@ import kotlin.random.Random class IOStreamsTest { @Test fun testGetStreamOfFile() { - val tmpFile = createTempFile() + val tmpFile = @Suppress("DEPRECATION") createTempFile() var writer: Writer? = null try { writer = tmpFile.outputStream().writer() @@ -46,7 +46,7 @@ class IOStreamsTest { } @Test fun readWriteBytes() { - val file = createTempFile("temp", Random.nextLong().toString()) + val file = @Suppress("DEPRECATION") createTempFile("temp", Random.nextLong().toString()) try { val bytes = Random.nextBytes(256_000)