Skip to main content

Insecure temporary files


Secure handling of temporary files


  • Usage of Ruby for building dynamic and object-oriented applications
  • Usage of tempfile for creating temporary files and directories


Non compliant code

def temp_file
temp ='sensitive_data')
temp << 'Sensitive Information'

In this code snippet, we have a method named temp_file that creates a temporary file named 'sensitive_data' using the method. It then writes some sensitive information to this file and finally returns the path of the file.

The vulnerability here is that the sensitive data is stored in a temporary file which can be accessed by any user or process that has permissions to read the file. Moreover, the file is not securely deleted after use, meaning that the data remains available even after the method has finished executing. This could potentially expose sensitive information to unauthorized users or processes.

Another potential risk is that if this temporary file is deployed to a production environment, it could expose sensitive data to anyone who has access to the production environment, including potentially malicious actors.

In a worst-case scenario, if an attacker gains access to the system, they could read these temporary files to extract sensitive information, or even manipulate the data within these files to cause further damage.


  • Avoid storing sensitive information in temporary files
  • Use secure methods to handle sensitive data, such as encryption or secure storage
  • Avoid deploying sensitive temporary files to the production environment
  • Implement proper access controls and permissions for temporary files
  • Regularly clean up and delete temporary files after they are no longer needed

Compliant code

require 'securerandom'
require 'fileutils'

def secure_temp_file
temp =, Dir.tmpdir, :encoding => 'ascii-8bit')
temp << 'Sensitive Information'.encrypt # Assuming you have an encryption method
FileUtils.rm(temp.path) if File.exist?(temp.path)

The updated code now uses the SecureRandom.hex method to generate a random filename for the temporary file, which makes it harder for an attacker to guess the filename. The temporary file is also created in the system's temporary directory, which is typically restricted to the current user.

The sensitive information is encrypted before being written to the temporary file. This ensures that even if an attacker manages to access the file, they won't be able to read the sensitive information without the encryption key.

Finally, the ensure block has been updated to delete the temporary file after it's closed. This ensures that the sensitive information doesn't linger on the system any longer than necessary. The FileUtils.rm method is used to delete the file, and File.exist? is used to check that the file exists before trying to delete it. This prevents a potential error if the file has already been deleted.

Please note that you need to implement the encryption method for 'Sensitive Information'.