From 1e76b2b3af229a269b276a46151c2beca0b71f7b Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Wed, 27 Jul 2022 16:35:37 +0000 Subject: [PATCH] vuln-fix: Temporary Directory Hijacking or Information Disclosure This fixes either Temporary Directory Hijacking, or Temporary Directory Local Information Disclosure. Weakness: CWE-379: Creation of Temporary File in Directory with Insecure Permissions Severity: High CVSSS: 7.3 Detection: CodeQL & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.UseFilesCreateTempDirectory) Reported-by: Jonathan Leitschuh Signed-off-by: Jonathan Leitschuh Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/10 Co-authored-by: Moderne --- .../oauth/JsonServiceAccountConfigTestUtil.java | 9 ++------- .../oauth/LegacyJsonServiceAccountConfigUtil.java | 9 ++------- .../oauth/P12ServiceAccountConfigTestUtil.java | 9 ++------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTestUtil.java b/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTestUtil.java index b732e2a..23d3910 100644 --- a/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTestUtil.java +++ b/src/test/java/com/google/jenkins/plugins/credentials/oauth/JsonServiceAccountConfigTestUtil.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.StringWriter; import java.nio.charset.Charset; +import java.nio.file.Files; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; @@ -64,13 +65,7 @@ public static String createTempJsonKeyFile(String clientEmail, PrivateKey privat private static File getTempFolder() throws IOException { if (tempFolder == null) { - tempFolder = File.createTempFile("temp", Long.toString(System.nanoTime())); - if (!tempFolder.delete()) { - throw new IOException("Could not delete temp file: " + tempFolder.getAbsolutePath()); - } - if (!tempFolder.mkdir()) { - throw new IOException("Could not create temp directory: " + tempFolder.getAbsolutePath()); - } + tempFolder = Files.createTempDirectory("temp" + Long.toString(System.nanoTime())).toFile(); tempFolder.deleteOnExit(); } return tempFolder; diff --git a/src/test/java/com/google/jenkins/plugins/credentials/oauth/LegacyJsonServiceAccountConfigUtil.java b/src/test/java/com/google/jenkins/plugins/credentials/oauth/LegacyJsonServiceAccountConfigUtil.java index 6e9b8e5..ad0132c 100644 --- a/src/test/java/com/google/jenkins/plugins/credentials/oauth/LegacyJsonServiceAccountConfigUtil.java +++ b/src/test/java/com/google/jenkins/plugins/credentials/oauth/LegacyJsonServiceAccountConfigUtil.java @@ -21,6 +21,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.file.Files; /** * Util class for {@link com.google.jenkins.plugins.credentials.oauth @@ -86,13 +87,7 @@ public static String createTempInvalidLegacyJsonKeyFile() throws IOException { private static File getTempFolder() throws IOException { if (tempFolder == null) { - tempFolder = File.createTempFile("temp", Long.toString(System.nanoTime())); - if (!tempFolder.delete()) { - throw new IOException("Could not delete temp file: " + tempFolder.getAbsolutePath()); - } - if (!tempFolder.mkdir()) { - throw new IOException("Could not create temp directory: " + tempFolder.getAbsolutePath()); - } + tempFolder = Files.createTempDirectory("temp" + Long.toString(System.nanoTime())).toFile(); tempFolder.deleteOnExit(); } return tempFolder; diff --git a/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTestUtil.java b/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTestUtil.java index a0c907a..127cf77 100644 --- a/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTestUtil.java +++ b/src/test/java/com/google/jenkins/plugins/credentials/oauth/P12ServiceAccountConfigTestUtil.java @@ -19,6 +19,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; +import java.nio.file.Files; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; @@ -64,13 +65,7 @@ public static String createTempP12KeyFile(KeyPair keyPair) private static File getTempFolder() throws IOException { if (tempFolder == null) { - tempFolder = File.createTempFile("temp", Long.toString(System.nanoTime())); - if (!tempFolder.delete()) { - throw new IOException("Could not delete temp file: " + tempFolder.getAbsolutePath()); - } - if (!tempFolder.mkdir()) { - throw new IOException("Could not create temp directory: " + tempFolder.getAbsolutePath()); - } + tempFolder = Files.createTempDirectory("temp" + Long.toString(System.nanoTime())).toFile(); tempFolder.deleteOnExit(); } return tempFolder;