Why is this an issue?
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:
Path.GetTempFileName() generates predictable file names and is inherently unreliable and insecure. Additionally, the method
will raise an
IOException if it is used to create more than 65535 files without deleting previous temporary files.
Noncompliant code example
var tempPath = Path.GetTempFileName(); // Noncompliant
using (var writer = new StreamWriter(tempPath))
var randomPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
// Creates a new file with write, non inheritable permissions which is deleted on close.
using (var fileStream = new FileStream(randomPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose))
using (var writer = new StreamWriter(fileStream))
Recommended Secure Coding Practices
Out of the box, .NET is missing secure-by-design APIs to create temporary files. To overcome this, one of the following options can be used:
- Use a dedicated sub-folder with tightly controlled permissions
- Created temporary files in a publicly writable folder and make sure:
- Generated filename is unpredictable
- File is readable and writable only by the creating user ID
- File descriptor is not inherited by child processes
- File is destroyed as soon as it is closed