2023-07-06 17:05:53 +02:00
|
|
|
include::../common/rationale.adoc[]
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
include::../common/description.adoc[]
|
2021-01-27 12:06:36 +01:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
=== What is the potential impact?
|
2021-02-02 15:02:10 +01:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
include::../common/impact.adoc[]
|
2021-01-27 12:06:36 +01:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
== How to fix it
|
|
|
|
|
|
|
|
=== Code examples
|
2021-01-27 12:06:36 +01:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
include::../common/fix/code-rationale.adoc[]
|
2021-01-26 04:07:35 +00:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
==== Noncompliant code example
|
2021-01-26 04:07:35 +00:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
2021-01-26 04:07:35 +00:00
|
|
|
----
|
2023-07-06 17:05:53 +02:00
|
|
|
using System.IO;
|
2021-01-27 04:07:23 +00:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
public void Example()
|
2021-01-26 04:07:35 +00:00
|
|
|
{
|
2023-07-06 17:05:53 +02:00
|
|
|
var tempPath = Path.GetTempFileName(); // Noncompliant
|
|
|
|
|
|
|
|
using (var writer = new StreamWriter(tempPath))
|
|
|
|
{
|
|
|
|
writer.WriteLine("content");
|
|
|
|
}
|
2021-01-26 04:07:35 +00:00
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
2021-01-26 04:07:35 +00:00
|
|
|
----
|
2023-07-06 17:05:53 +02:00
|
|
|
using System.IO;
|
2021-01-27 04:07:23 +00:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
public void Example()
|
2021-01-26 04:07:35 +00:00
|
|
|
{
|
2023-07-06 17:05:53 +02:00
|
|
|
var randomPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
|
|
|
|
|
|
|
using (var fileStream = new FileStream(randomPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.DeleteOnClose))
|
|
|
|
using (var writer = new StreamWriter(fileStream))
|
|
|
|
{
|
|
|
|
writer.WriteLine("content");
|
|
|
|
}
|
2021-01-26 04:07:35 +00:00
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
include::../common/fix/introduction.adoc[]
|
|
|
|
|
|
|
|
include::../common/fix/manual-setup.adoc[]
|
|
|
|
|
|
|
|
Here the example compliant code uses the `Path.GetTempPath` and
|
|
|
|
`Path.GetRandomFileName` functions to generate a unique random file name. The
|
|
|
|
file is then open with the `FileMode.CreateNew` option that will ensure the
|
|
|
|
creation fails if the file already exists. The `FileShare.None` option will
|
|
|
|
additionally prevent the file from being opened again by any process. To finish,
|
|
|
|
this code ensures the file will get destroyed once the application has finished
|
|
|
|
using it with the `FileOptions.DeleteOnClose` option.
|
|
|
|
|
|
|
|
//=== Pitfalls
|
|
|
|
|
|
|
|
//=== Going the extra mile
|
|
|
|
|
|
|
|
|
|
|
|
== Resources
|
|
|
|
|
|
|
|
include::../common/resources/documentation.adoc[]
|
2023-05-03 11:06:20 +02:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
//=== Articles & blog posts
|
|
|
|
//=== Conference presentations
|
2023-05-03 11:06:20 +02:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
include::../common/resources/standards.adoc[]
|
2023-05-03 11:06:20 +02:00
|
|
|
|
2023-07-06 17:05:53 +02:00
|
|
|
//=== Benchmarks
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
'Path.GetTempFileName()' is insecure. Use 'Path.GetRandomFileName()' instead.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2023-06-22 10:38:01 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|