rspec/rules/S5445/csharp/rule.adoc

54 lines
2.1 KiB
Plaintext
Raw Normal View History

2021-01-27 12:06:36 +01:00
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.
2021-02-02 15:02:10 +01:00
2021-01-27 12:06:36 +01:00
In the past, it has led to the following vulnerabilities:
* https://nvd.nist.gov/vuln/detail/CVE-2014-1858[CVE-2014-1858]
* https://nvd.nist.gov/vuln/detail/CVE-2014-1932[CVE-2014-1932]
2021-01-27 13:42:22 +01:00
``++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.
2021-01-26 04:07:35 +00:00
2021-01-27 04:07:23 +00:00
== 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
2021-01-26 04:07:35 +00:00
== Noncompliant Code Example
----
var tempPath = Path.GetTempFileName(); // Noncompliant
2021-01-27 04:07:23 +00:00
using (var writer = new StreamWriter(tempPath))
2021-01-26 04:07:35 +00:00
{
writer.WriteLine("content");
}
----
== Compliant Solution
----
var randomPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
2021-01-27 04:07:23 +00:00
// 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))
2021-01-26 04:07:35 +00:00
{
writer.WriteLine("content");
}
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]