Creating temporary files using insecure methods exposes the application to race conditions on filenames: a malicious user can try to create a file
with a predictable name before the application does. A successful attack can result in other files being accessed, modified, corrupted or deleted.
This risk is even higher if the application run with elevated permissions.
In the past, it has led to the following vulnerabilities:
Noncompliant Code Example
filename = tempfile.mktemp() # Noncompliant
tmp_file = open(filename, "w+")
tmp_file1 = tempfile.NamedTemporaryFile(delete=False) # Compliant; Easy replacement to tempfile.mktemp()
tmp_file2 = tempfile.NamedTemporaryFile() # Compliant; Created file will be automatically deleted