|
| 1 | +From 2a3b5c8b4d62291d4c646b5fd9aac2753378b49e Mon Sep 17 00:00:00 2001 |
| 2 | +From: jykanase <v-jykanase@microsoft.com> |
| 3 | +Date: Tue, 11 Feb 2025 13:28:51 +0000 |
| 4 | +Subject: [PATCH] CVE-2020-15250 |
| 5 | + |
| 6 | +Source Link: https://github.com/junit-team/junit4/commit/610155b8c22138329f0723eec22521627dbc52ae |
| 7 | +--- |
| 8 | + .../java/org/junit/rules/TemporaryFolder.java | 43 ++++++++++++++++++- |
| 9 | + 1 file changed, 42 insertions(+), 1 deletion(-) |
| 10 | + |
| 11 | +diff --git a/src/main/java/org/junit/rules/TemporaryFolder.java b/src/main/java/org/junit/rules/TemporaryFolder.java |
| 12 | +index 1a6a770..a726c66 100644 |
| 13 | +--- a/src/main/java/org/junit/rules/TemporaryFolder.java |
| 14 | ++++ b/src/main/java/org/junit/rules/TemporaryFolder.java |
| 15 | +@@ -4,6 +4,9 @@ import static org.junit.Assert.fail; |
| 16 | + |
| 17 | + import java.io.File; |
| 18 | + import java.io.IOException; |
| 19 | ++import java.lang.reflect.Array; |
| 20 | ++import java.lang.reflect.InvocationTargetException; |
| 21 | ++import java.lang.reflect.Method; |
| 22 | + |
| 23 | + import org.junit.Rule; |
| 24 | + |
| 25 | +@@ -229,7 +232,45 @@ public class TemporaryFolder extends ExternalResource { |
| 26 | + return createTemporaryFolderIn(getRoot()); |
| 27 | + } |
| 28 | + |
| 29 | +- private File createTemporaryFolderIn(File parentFolder) throws IOException { |
| 30 | ++ private static File createTemporaryFolderIn(File parentFolder) throws IOException { |
| 31 | ++ try { |
| 32 | ++ return createTemporaryFolderWithNioApi(parentFolder); |
| 33 | ++ } catch (ClassNotFoundException ignore) { |
| 34 | ++ // Fallback for Java 5 and 6 |
| 35 | ++ return createTemporaryFolderWithFileApi(parentFolder); |
| 36 | ++ } catch (InvocationTargetException e) { |
| 37 | ++ Throwable cause = e.getCause(); |
| 38 | ++ if (cause instanceof IOException) { |
| 39 | ++ throw (IOException) cause; |
| 40 | ++ } |
| 41 | ++ if (cause instanceof RuntimeException) { |
| 42 | ++ throw (RuntimeException) cause; |
| 43 | ++ } |
| 44 | ++ IOException exception = new IOException("Failed to create temporary folder in " + parentFolder); |
| 45 | ++ exception.initCause(cause); |
| 46 | ++ throw exception; |
| 47 | ++ } catch (Exception e) { |
| 48 | ++ throw new RuntimeException("Failed to create temporary folder in " + parentFolder, e); |
| 49 | ++ } |
| 50 | ++ } |
| 51 | ++ |
| 52 | ++ private static File createTemporaryFolderWithNioApi(File parentFolder) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
| 53 | ++ Class<?> filesClass = Class.forName("java.nio.file.Files"); |
| 54 | ++ Object fileAttributeArray = Array.newInstance(Class.forName("java.nio.file.attribute.FileAttribute"), 0); |
| 55 | ++ Class<?> pathClass = Class.forName("java.nio.file.Path"); |
| 56 | ++ Object tempDir; |
| 57 | ++ if (parentFolder != null) { |
| 58 | ++ Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", pathClass, String.class, fileAttributeArray.getClass()); |
| 59 | ++ Object parentPath = File.class.getDeclaredMethod("toPath").invoke(parentFolder); |
| 60 | ++ tempDir = createTempDirectoryMethod.invoke(null, parentPath, TMP_PREFIX, fileAttributeArray); |
| 61 | ++ } else { |
| 62 | ++ Method createTempDirectoryMethod = filesClass.getDeclaredMethod("createTempDirectory", String.class, fileAttributeArray.getClass()); |
| 63 | ++ tempDir = createTempDirectoryMethod.invoke(null, TMP_PREFIX, fileAttributeArray); |
| 64 | ++ } |
| 65 | ++ return (File) pathClass.getDeclaredMethod("toFile").invoke(tempDir); |
| 66 | ++ } |
| 67 | ++ |
| 68 | ++ private static File createTemporaryFolderWithFileApi(File parentFolder) throws IOException { |
| 69 | + File createdFolder = null; |
| 70 | + for (int i = 0; i < TEMP_DIR_ATTEMPTS; ++i) { |
| 71 | + // Use createTempFile to get a suitable folder name. |
| 72 | +-- |
| 73 | +2.45.2 |
| 74 | + |
0 commit comments