rspec/rules/S5445/python/rule.adoc

59 lines
2.1 KiB
Plaintext
Raw Normal View History

== 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:
* 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
=== 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+")
----
=== 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
----
== Resources
2020-06-30 12:50:28 +02:00
* https://owasp.org/Top10/A01_2021-Broken_Access_Control/[OWASP Top 10 2021 Category A1] - Broken Access Control
* 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
* 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
* 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]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
'{module.method}' is insecure. Use 'tempfile.TemporaryFile' instead
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]