False Positive on javasecurity:S6096 when using java.nio.file.Files.createTempFile

Make sure to read this post before raising a thread here:

Then tell us:

  • What language is this for :
    • Java
  • Which rule :
    • S6096
  • Why do you believe it’s a false-positive/false-negative?
    • A Zip Slip vulnerability is impossible when using the entry name as a prefix or suffix for a file created by Files.createTempFile, according to Javadoc : Files (Java SE 17 & JDK 17)
  • Are you using
    • SonarQube Server - Enterprise Edition v2025.1 (102418)
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Example {

    public Path unzip(File zip) throws IOException {
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zip), StandardCharsets.UTF_8)) {
            ZipEntry ze = zis.getNextEntry();
            if (ze != null) {
                /* This line uses the entry name as a suffix for the name of a new temporary file.
                 * This file will be created in the default temporary folder according to Javadoc
                 * and will throw an IllegalArgumentException if it contains characters that cannot be used in file names.
                 */
                Path unzippedFilePath = Files.createTempFile("unzipped", ze.getName());
                Files.copy(zis, unzippedFilePath);
                return unzippedFilePath;
            } else {
                return null;
            }
        }

    }
}

Hi @AntoineL,

Welcome to this forum, and yes. You are totally right. I am going to create a PR to fix that.

For reproducibility, here is java.nio’s check: jdk/src/java.base/share/classes/java/nio/file/TempFileHelper.java at b0536f9c2a6ddfa27be8fad8f53783c6b28d22c9 · openjdk/jdk · GitHub

The PR is done. Thanks for your contribution! :tada:

1 Like