2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-25 14:18:12 +02: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.
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]
2020-06-30 12:50:28 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:50:28 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:50:28 +02:00
----
import tempfile
filename = tempfile.mktemp() # Noncompliant
tmp_file = open(filename, "w+")
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:50:28 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:50:28 +02:00
----
import tempfile
tmp_file1 = tempfile.NamedTemporaryFile(delete=False) # Compliant; Easy replacement to tempfile.mktemp()
tmp_file2 = tempfile.NamedTemporaryFile() # Compliant; Created file will be automatically deleted
----
2023-05-03 11:06:20 +02:00
== Resources
2020-06-30 12:50:28 +02:00
2021-11-01 15:00:32 +01:00
* https://owasp.org/Top10/A01_2021-Broken_Access_Control/[OWASP Top 10 2021 Category A1] - Broken Access Control
2022-07-08 13:58:56 +02:00
* https://owasp.org/www-project-top-ten/2017/A9_2017-Using_Components_with_Known_Vulnerabilities[OWASP Top 10 2017 Category A9] - Using Components with Known Vulnerabilities
2022-04-07 08:53:59 -05:00
* https://cwe.mitre.org/data/definitions/377[MITRE, CWE-377] - Insecure Temporary File
* https://cwe.mitre.org/data/definitions/379[MITRE, CWE-379] - Creation of Temporary File in Directory with Incorrect Permissions
2022-07-08 13:58:56 +02:00
* https://owasp.org/www-community/vulnerabilities/Insecure_Temporary_File[OWASP, Insecure Temporary File]
2020-06-30 12:50:28 +02:00
* https://docs.python.org/3/library/tempfile.html#deprecated-functions-and-variables[Python tempfile module]
* https://docs.python.org/2.7/library/os.html[Python 2.7 os module]
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
'{module.method}' is insecure. Use 'tempfile.TemporaryFile' 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[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]