{"id":968,"cve":"CVE-2022-31575","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2022-31575","cwe":"CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')","cwe_number":22,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/22","cvss_score":9.3,"published":"2022-07-11","owasp":"A01 Broken Access Control","odc":"Interface","cdc":"Incorrect Interface","category":"Input Validation and Sanitization","subcategory":"Path Traversal","scope":"Remote","impact":"Information Disclosure","summary":"GitHub repository duducosmos\/livro_python: Absolute path traversal via Flask send_file","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2022-31575\/","description":"The duducosmos\/livro_python repository through 2018-06-06 on GitHub allows absolute path traversal because the Flask send_file function is used unsafely.","solution":"Fix by updating Flask and the duducosmos\/livro_python repository to the latest versions","affected_component":"Flask","fixed_by_upgrading":true,"vulnerable_code":"from flask import Flask, send_file\n\napp = Flask(__name__)\n\n@app.route('\/download\/<path:filename>')\ndef download_file(filename):\n    # Unsafe use of send_file that allows absolute path traversal\n    return send_file(filename)\n\nif __name__ == '__main__':\n    app.run()","patched_code":"from flask import Flask, send_file, abort, safe_join, send_from_directory\nimport os\n\napp = Flask(__name__)\n\n@app.route('\/download\/<path:filename>')\ndef download_file(filename):\n    # Define a safe directory to serve files from\n    safe_directory = '\/path\/to\/safe\/directory'\n    \n    # Use safe_join to prevent absolute path traversal\n    safe_path = safe_join(safe_directory, filename)\n\n    # Check if the file exists and is within the safe directory\n    if os.path.isfile(safe_path):\n        return send_file(safe_path)\n    else:\n        abort(404)\n\nif __name__ == '__main__':\n    app.run()","payload_needed":false,"payload":null,"payload_status":"none","published_fix":[],"references":"https:\/\/rules.sonarsource.com\/python\/type\/Vulnerability\/RSPEC-5146\/"}