This product is not supported for your selected Datadog site. ().
Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: java-security/tempfile-delete

Language: Java

Severity: Warning

Category: Security

CWE: 459

Description

This rule identifies instances where temporary files are created but not properly deleted after use. Leaving temporary files undeleted can lead to resource leaks, unnecessary disk space consumption, and potential exposure of sensitive data if the files contain confidential information.

To comply with this rule, always delete temporary files explicitly when they are no longer needed, or use mechanisms like deleteOnExit() to schedule automatic deletion when the JVM terminates. For example, after creating a temporary file with File.createTempFile(), invoke tempFile.deleteOnExit() to ensure cleanup. This practice helps maintain application stability and security.

Non-Compliant Code Examples

import java.io.File;
import java.io.IOException;

public class SecureTempFileExample {
    public static void main(String[] args) throws IOException {
        File tempFile = File.createTempFile("tempfile_", ".tmp");
        System.out.println("Temporary file created at: " + tempFile.getAbsolutePath());
    }
}

Compliant Code Examples

import java.io.File;
import java.io.IOException;

public class SecureTempFileWithPermissionsExample {
    public static void main(String[] args) throws IOException {
        File tempFile = File.createTempFile("secure_tempfile_", ".tmp");
        tempFile.deleteOnExit();
        System.out.println("Temporary file created with secure permissions at: " 
            + tempFile.getAbsolutePath());
    }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains