CVE-2022-31518
Flask send_file function: Absolute path traversal vulnerability
- CVSS 9.3
- CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
- Input Validation and Sanitization
- Remote
The JustAnotherSoftwareDeveloper/Python-Recipe-Database repository through 2021-03-31 on GitHub allows absolute path traversal because the Flask send_file function is used unsafely.
- CVSS base score
- 9.3
- Published
- 2022-07-11
- OWASP
- A05 Security Misconfiguration
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Path Traversal
- Accessibility scope
- Remote
- Impact
- Unauthorized Access
- Affected component
- Flask
- Fixed by upgrading
- Yes
Solution
Update Flask to version 2.0.2 or higher.
Vulnerable code sample
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/files/<path:filename>')
def serve_file(filename):
# Unsafely using send_file without validation
return send_file(filename)
if __name__ == '__main__':
app.run()Patched code sample
from flask import Flask, send_file, abort
import os
app = Flask(__name__)
@app.route('/files/<path:filename>')
def serve_file(filename):
# Ensure the filename is safe and within a specific directory
safe_directory = '/path/to/safe/directory'
safe_path = os.path.join(safe_directory, filename)
# Check if the file exists and is within the safe directory
if os.path.isfile(safe_path) and os.path.commonprefix([safe_path, safe_directory]) == safe_directory:
return send_file(safe_path)
else:
abort(404)
if __name__ == '__main__':
app.run()Cite this entry
@misc{vaitp:cve202231518,
title = {{Flask send_file function: Absolute path traversal vulnerability}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2022},
note = {VAITP Python Vulnerability Dataset, entry CVE-2022-31518},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-31518/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
