rspec/rules/S2976/java/rule.adoc

44 lines
1.2 KiB
Plaintext

Using ``++File.createTempFile++`` as the first step in creating a temporary directory causes a race condition and is inherently unreliable and insecure. Instead, ``++Files.createTempDirectory++`` (Java 7+) or a library function such as Guava's similarly-named ``++Files.createTempDir++`` should be used.
This rule raises an issue when the following steps are taken in immediate sequence:
* call to ``++File.createTempFile++``
* delete resulting file
* call ``++mkdir++`` on the File object
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++7++``.
== Noncompliant Code Example
----
File tempDir;
tempDir = File.createTempFile("", ".");
tempDir.delete();
tempDir.mkdir(); // Noncompliant
----
== Compliant Solution
----
Path tempPath = Files.createTempDirectory("");
File tempDir = tempPath.toFile();
----
== See
* https://www.owasp.org/index.php/Top_10-2017_A9-Using_Components_with_Known_Vulnerabilities[OWASP Top 10 2017 Category A9] - Using Components with Known Vulnerabilities
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]