78 lines
3.7 KiB
Diff
78 lines
3.7 KiB
Diff
From 610155b8c22138329f0723eec22521627dbc52ae Mon Sep 17 00:00:00 2001
|
||
From: Marc Philipp <mail@marcphilipp.de>
|
||
Date: Sun, 11 Oct 2020 16:56:21 +0200
|
||
Subject: [PATCH] Merge pull request from GHSA-269g-pwp5-87pp
|
||
MIME-Version: 1.0
|
||
Content-Type: text/plain; charset=UTF-8
|
||
Content-Transfer-Encoding: 8bit
|
||
|
||
When running on Java 7 or later, temporary directories are now created
|
||
Using Java’s NIO API which restricts permissions to owner-only by
|
||
default.
|
||
---
|
||
.../java/org/junit/rules/TemporaryFolder.java | 43 ++++++++++++++++++-
|
||
.../org/junit/rules/TempFolderRuleTest.java | 37 +++++++++++++++-
|
||
2 files changed, 78 insertions(+), 2 deletions(-)
|
||
|
||
diff --git a/src/main/java/org/junit/rules/TemporaryFolder.java b/src/main/java/org/junit/rules/TemporaryFolder.java
|
||
index 1a6a770608..a726c66e36 100644
|
||
--- a/src/main/java/org/junit/rules/TemporaryFolder.java
|
||
+++ b/src/main/java/org/junit/rules/TemporaryFolder.java
|
||
@@ -2,6 +2,9 @@
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
+import java.lang.reflect.Array;
|
||
+import java.lang.reflect.InvocationTargetException;
|
||
+import java.lang.reflect.Method;
|
||
|
||
import org.junit.Rule;
|
||
|
||
@@ -133,7 +136,45 @@ public File newFolder() throws IOException {
|
||
return createTemporaryFolderIn(getRoot());
|
||
}
|
||
|
||
- private File createTemporaryFolderIn(File parentFolder) throws IOException {
|
||
+ private static File createTemporaryFolderIn(File parentFolder) throws IOException {
|
||
+ try {
|
||
+ return createTemporaryFolderWithNioApi(parentFolder);
|
||
+ } catch (ClassNotFoundException ignore) {
|
||
+ // Fallback for Java 5 and 6
|
||
+ return createTemporaryFolderWithFileApi(parentFolder);
|
||
+ } catch (InvocationTargetException e) {
|
||
+ Throwable cause = e.getCause();
|
||
+ if (cause instanceof IOException) {
|
||
+ throw (IOException) cause;
|
||
+ }
|
||
+ if (cause instanceof RuntimeException) {
|
||
+ throw (RuntimeException) cause;
|
||
+ }
|
||
+ IOException exception = new IOException("Failed to create temporary folder in " + parentFolder);
|
||
+ exception.initCause(cause);
|
||
+ throw exception;
|
||
+ } catch (Exception e) {
|
||
+ throw new RuntimeException("Failed to create temporary folder in " + parentFolder, e);
|
||
+ }
|
||
+ }
|
||
+
|
||
+ private static File createTemporaryFolderWithNioApi(File parentFolder) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||
+ Class<?> filesClass = Class.forName("java.nio.file.Files");
|
||
+ Object fileAttributeArray = Array.newInstance(Class.forName("java.nio.file.attribute.FileAttribute"), 0);
|
||
+ Class<?> pathClass = Class.forName("java.nio.file.Path");
|
||
+ Object tempDir;
|
||
+ if (parentFolder != null) {
|
||
+ Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", pathClass, String.class, fileAttributeArray.getClass());
|
||
+ Object parentPath = File.class.getDeclaredMethod("toPath").invoke(parentFolder);
|
||
+ tempDir = createTempDirectoryMethod.invoke(null, parentPath, TMP_PREFIX, fileAttributeArray);
|
||
+ } else {
|
||
+ Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", String.class, fileAttributeArray.getClass());
|
||
+ tempDir = createTempDirectoryMethod.invoke(null, TMP_PREFIX, fileAttributeArray);
|
||
+ }
|
||
+ return (File) pathClass.getDeclaredMethod("toFile").invoke(tempDir);
|
||
+ }
|
||
+
|
||
+ private static File createTemporaryFolderWithFileApi(File parentFolder) throws IOException {
|
||
File createdFolder = null;
|
||
for (int i = 0; i < TEMP_DIR_ATTEMPTS; ++i) {
|
||
// Use createTempFile to get a suitable folder name.
|