Skip to content

Commit

Permalink
Fix google#4011: Files::createTempDir security vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Nov 19, 2020
1 parent 751d7c0 commit 6b6a070
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions guava/src/com/google/common/io/Files.java
Expand Up @@ -421,25 +421,14 @@ public static boolean equal(File file1, File file2) throws IOException {
@Beta
@Deprecated
public static File createTempDir() {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
@SuppressWarnings("GoodTime") // reading system time without TimeSource
String baseName = System.currentTimeMillis() + "-";

for (int counter = 0; counter < TEMP_DIR_ATTEMPTS; counter++) {
File tempDir = new File(baseDir, baseName + counter);
if (tempDir.mkdir()) {
return tempDir;
}
try {
@SuppressWarnings("GoodTime") // reading system time without TimeSource
String baseName = System.currentTimeMillis() + "-";
java.nio.file.Path temp = java.nio.file.Files.createTempDirectory(baseName);
return temp.toFile();
} catch (IOException ioex) {
throw new IllegalStateException("Failed to create temporary directory.", ioex);
}
throw new IllegalStateException(
"Failed to create directory within "
+ TEMP_DIR_ATTEMPTS
+ " attempts (tried "
+ baseName
+ "0 to "
+ baseName
+ (TEMP_DIR_ATTEMPTS - 1)
+ ')');
}

/**
Expand Down

0 comments on commit 6b6a070

Please sign in to comment.