Security

What is Creation of Temporary File in Directory (CWE-379)?

Learn how temporary files created in insecure directories pose a risk to confidentiality. Explore real-world code examples, detection methods, and...

SP
Shreya Pillai July 29, 2026 5 min read Security
AI-friendly summary

What it is: Creation of Temporary File in Directory with Insecure Permissions (CWE-379) is a vulnerability where temporary files are created in directories that allow unintended actors to determine the file's existence or access it.

Why it matters: This can lead to unauthorized access and exposure of sensitive data stored temporarily by applications.

How to fix it: Store temporary files in secure, user-specific directories with restricted permissions.

TL;DR: Creation of Temporary File in Directory with Insecure Permissions (CWE-379) is a vulnerability where temporary files are created in insecure directories, allowing unauthorized access. It can be fixed by storing files in secure directories.

Field Value
CWE ID CWE-379
OWASP Category A01:2025 - Broken Access Control
CAPEC None known
Typical Severity Medium
Affected Technologies Python, Java, Node.js, PHP
Detection Difficulty Moderate
Last Updated 2026-07-29

What is Creation of Temporary File in Directory with Insecure Permissions?

Creation of Temporary File in Directory with Insecure Permissions (CWE-379) is a type of vulnerability where temporary files are created in directories that allow unintended actors to determine the file’s existence or access it. As defined by the MITRE Corporation under CWE-379, and classified by the OWASP Foundation under A01:2025 - Broken Access Control.

Quick Summary

This vulnerability occurs when an application creates temporary files in directories with insecure permissions, allowing unauthorized users to view or modify these files. This can lead to exposure of sensitive data stored temporarily by applications. Jump to: Overview · How It Works · Business Impact · Attack Scenario · Detection · Fixing

Jump to: Quick Summary · Creation of Temporary File in Directory with Insecure Permissions Overview · How Creation of Temporary File in Directory with Insecure Permissions Works · Business Impact of Creation of Temporary File in Directory with Insecure Permissions · Creation of Temporary File in Directory with Insecure Permissions Attack Scenario · How to Detect Creation of Temporary File in Directory with Insecure Permissions · How to Fix Creation of Temporary File in Directory with Insecure Permissions · Framework-Specific Fixes for Creation of Temporary File in Directory with Insecure Permissions · How to Ask AI to Check Your Code for Creation of Temporary File in Directory with Insecure Permissions · Creation of Temporary File in Directory with Insecure Permissions Best Practices Checklist · Creation of Temporary File in Directory with Insecure Permissions FAQ · Vulnerabilities Related to Creation of Temporary File in Directory with Insecure Permissions · References · Scan Your Own Site

Creation of Temporary File in Directory with Insecure Permissions Overview

What: A temporary file is created in a directory that allows unintended actors to determine the file’s existence or access it.

Why it matters: Unauthorized users can gain access to sensitive data stored temporarily by applications, leading to potential misuse and exposure of confidential information.

Where it occurs: Primarily in web applications and backend services using languages like Python, Java, Node.js, and PHP that create temporary files without proper permission checks.

Who is affected: Applications that store sensitive data temporarily and do not enforce strict directory permissions on these files are at risk.

Who is NOT affected: Systems already using secure tempfile functions or storing temporary files in user-specific directories with restricted access.

How Creation of Temporary File in Directory with Insecure Permissions Works

Root Cause

The root cause lies in the application’s failure to create temporary files in directories that enforce strict permissions, allowing unauthorized users to view or modify these files.

Attack Flow

  1. An attacker identifies a directory where temporary files are created.
  2. The attacker determines the existence of specific temporary files and accesses them.
  3. Sensitive data is exposed due to lack of proper permission checks on the directory.

Prerequisites to Exploit

  • The attacker must have read access to the directory containing temporary files.
  • The application does not enforce strict permissions or ownership on these directories.

Vulnerable Code

import os

def create_temp_file(filename):
    temp_path = '/tmp/' + filename  # Insecure path
    with open(temp_path, 'w') as f:
        f.write('Sensitive data')

This code creates a temporary file in the /tmp directory without proper permission checks.

Secure Code

import tempfile

def create_temp_file_secure(filename):
    temp_dir = tempfile.gettempdir()  # Secure tempfile library
    with tempfile.NamedTemporaryFile(dir=temp_dir, delete=False) as f:
        f.write('Sensitive data')

This code uses Python’s tempfile module to create a temporary file in a secure directory.

Business Impact of Creation of Temporary File in Directory with Insecure Permissions

Confidentiality: Sensitive data stored temporarily by applications can be exposed, leading to unauthorized access and potential misuse.

Integrity: Unauthorized users may modify or delete sensitive files, compromising the integrity of application data.

  • Financial consequences: Legal liabilities due to data breaches.
  • Compliance issues: Non-compliance with regulations such as GDPR, HIPAA, etc.
  • Reputation damage: Loss of customer trust due to security incidents.

Creation of Temporary File in Directory with Insecure Permissions Attack Scenario

  1. An attacker identifies a web application that creates temporary files in the /tmp directory.
  2. The attacker determines which specific temporary files are created and gains read access to them.
  3. Sensitive data stored temporarily by the application is exposed, leading to potential misuse.

How to Detect Creation of Temporary File in Directory with Insecure Permissions

Manual Testing

  • Review code for tempfile functions and check if they create files in world-readable directories without proper permission checks.
  • Verify that temporary files are created in secure, user-specific directories with restricted access.

Automated Scanners (SAST / DAST)

Static analysis can detect insecure tempfile creation patterns, but dynamic testing is required to confirm the actual permissions on target directories.

PenScan Detection

PenScan’s scanner engines such as ZAP and Wapiti can identify instances of CWE-379 by analyzing code and directory permissions.

False Positive Guidance

False positives may occur if a file appears in an insecure location but has proper access controls enforced through other means (e.g., SELinux policies).

How to Fix Creation of Temporary File in Directory with Insecure Permissions

  • Try to store sensitive tempfiles in a directory which is not world readable – i.e., per-user directories.
  • Avoid using vulnerable temp file functions.

Framework-Specific Fixes for Creation of Temporary File in Directory with Insecure Permissions

Python

import tempfile

def create_temp_file_secure(filename):
    temp_dir = tempfile.gettempdir()  # Secure tempfile library
    with tempfile.NamedTemporaryFile(dir=temp_dir, delete=False) as f:
        f.write('Sensitive data')

Java

import java.nio.file.Files;
import java.nio.file.Paths;

public class TempFile {
    public void createTempFile(String filename) throws IOException {
        Path tempDir = Files.createTempDirectory("secure_temp");
        try (FileWriter writer = new FileWriter(tempDir.resolve(filename).toFile())) {
            writer.write("Sensitive data");
        }
    }
}

Node.js

const fs = require('fs');
const path = require('path');

function createTempFileSecure(filename) {
    const tempDir = path.join(os.tmpdir(), 'secure_temp');  // Secure tempfile directory
    if (!fs.existsSync(tempDir)) {
        fs.mkdirSync(tempDir, { recursive: true });
    }
    fs.writeFileSync(path.join(tempDir, filename), 'Sensitive data');
}

PHP

<?php
function createTempFileSecure($filename) {
    $tempDir = sys_get_temp_dir() . '/secure_temp';  // Secure tempfile directory
    if (!file_exists($tempDir)) {
        mkdir($tempDir, 0755, true);
    }
    file_put_contents($tempDir . '/' . $filename, 'Sensitive data');
}

How to Ask AI to Check Your Code for Creation of Temporary File in Directory with Insecure Permissions

Copy-paste prompt

Review the following Python code block for potential CWE-379 Creation of Temporary File in Directory with Insecure Permissions vulnerabilities and rewrite it using secure tempfile functions: [paste code here]

Creation of Temporary File in Directory with Insecure Permissions Best Practices Checklist

✅ Store sensitive temporary files in user-specific directories with restricted access.

✅ Use language-specific secure tempfile libraries to handle permissions properly.

✅ Avoid hardcoding directory paths for temporary file creation and use environment variables or configuration settings instead.

✅ Regularly audit code for insecure tempfile creation patterns and fix them promptly.

Creation of Temporary File in Directory with Insecure Permissions FAQ

How does the creation of temporary files in directories with insecure permissions occur?

It happens when a program creates a temporary file in a directory that is accessible to unauthorized users, allowing them to view or modify the file.

What are the potential impacts of CWE-379 on application security?

Attackers can gain access to sensitive data stored temporarily by the application and use it for malicious purposes.

Can you provide an example of insecure tempfile creation in Python code?

A vulnerable Python script might create a temporary file using os.mktemp() without proper permissions checks.

How does OWASP categorize this vulnerability?

OWASP classifies it under A01:2025 - Broken Access Control, as it involves improper access to sensitive files.

What are the steps to detect creation of temporary file in directory with insecure permissions manually?

Review code for tempfile functions and check if they create files in world-readable directories without proper permission checks.

How can I fix this issue using secure tempfile functions in Python?

Use Python’s built-in tempfile module, which handles permissions securely by default.

What are the best practices to prevent CWE-379 vulnerabilities?

Store sensitive temporary files in user-specific directories with restricted access and use language-specific secure tempfile libraries.

CWE Name Relationship
CWE-377 Insecure Temporary File (ChildOf)  

References

Scan Your Own Site

Manual code review catches what you know to look for. An automated scan catches what you didn’t. Scan your own website using PenScan to find Creation of Temporary File in Directory with Insecure Permissions and other risks before an attacker does.