From 3edb13b4a2d94e5331151c14d1ebbface416243f Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Tue, 4 Oct 2022 00:21:27 +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 --- .../main/java/no/priv/garshol/duke/utils/Utils.java | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/duke-core/src/main/java/no/priv/garshol/duke/utils/Utils.java b/duke-core/src/main/java/no/priv/garshol/duke/utils/Utils.java index 7ed235a7..23087d88 100644 --- a/duke-core/src/main/java/no/priv/garshol/duke/utils/Utils.java +++ b/duke-core/src/main/java/no/priv/garshol/duke/utils/Utils.java @@ -3,6 +3,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import no.priv.garshol.duke.DukeException; @@ -37,17 +38,7 @@ public static File createTempDirectory(String prefix) { File temp = null; try { - temp = File.createTempFile(prefix != null ? prefix : "temp", Long.toString(System.nanoTime())); - - if (!(temp.delete())) { - throw new IOException("Could not delete temp file: " - + temp.getAbsolutePath()); - } - - if (!(temp.mkdir())) { - throw new IOException("Could not create temp directory: " - + temp.getAbsolutePath()); - } + temp = Files.createTempDirectory(prefix != null ? prefix : "temp" + Long.toString(System.nanoTime())).toFile(); } catch (IOException e) { throw new DukeException("Unable to create temporary directory with prefix " + prefix, e); }