From 3a47c5b68ee3c7e065d2f5fccf2bc69b4dfccd43 Mon Sep 17 00:00:00 2001 From: Porcupiney Hairs Date: Fri, 29 Apr 2022 18:57:01 +0530 Subject: [PATCH] # Absolute Path Traversal due to incorrect use of `send_file` call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A path traversal attack (also known as directory traversal) aims to access files and directories that are stored outside the web root folder. By manipulating variables that reference files with “dot-dot-slash (../)” sequences and its variations or by using absolute file paths, it may be possible to access arbitrary files and directories stored on file system including application source code or configuration and critical system files. This attack is also known as “dot-dot-slash”, “directory traversal”, “directory climbing” and “backtracking”. ## Root Cause Analysis The `os.path.join` call is unsafe for use with untrusted input. When the `os.path.join` call encounters an absolute path, it ignores all the parameters it has encountered till that point and starts working with the new absolute path. Please see the example below. ``` >>> import os.path >>> static = "path/to/mySafeStaticDir" >>> malicious = "/../../../../../etc/passwd" >>> os.path.join(t,malicious) '/../../../../../etc/passwd' ``` Since the "malicious" parameter represents an absolute path, the result of `os.path.join` ignores the static directory completely. Hence, untrusted input is passed via the `os.path.join` call to `flask.send_file` can lead to path traversal attacks. In this case, the problems occurs due to the following code : https://github.com/JustAnotherSoftwareDeveloper/Python-Recipe-Database/blob/8b88285c0a364eb3293d5f85f619ef91d70ab9dc/lib/app/routes.py#L100 Here, the `filename` parameter is attacker controlled. This parameter passes through the unsafe `os.path.join` call making the effective directory and filename passed to the `send_file` call attacker controlled. This leads to a path traversal attack. ## Proof of Concept The bug can be verified using a proof of concept similar to the one shown below. ``` curl --path-as-is 'http:///download?filename=../../../../../../../etc/passwd"' ``` ## Remediation This can be fixed by preventing flow of untrusted data to the vulnerable `send_file` function. In case the application logic necessiates this behaviour, one can either use the `werkzeug.utils.safe_join` to join untrusted paths or replace `flask.send_file` calls with `flask.send_from_directory` calls. ## References * [OWASP Path Traversal](https://owasp.org/www-community/attacks/Path_Traversal) * github/securitylab#669 ### This bug was found using *[CodeQL by Github](https://codeql.github.com/)* --- lib/app/routes.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/app/routes.py b/lib/app/routes.py index 5253af9..b870cd9 100644 --- a/lib/app/routes.py +++ b/lib/app/routes.py @@ -4,6 +4,7 @@ from flask.json import jsonify from flask import request +from werkzeug.utils import safe_join from flask import send_file from app.FileDelegate import FileDelegate from app.TagsDelegate import TagsDelegate @@ -46,13 +47,14 @@ def getAll(): @app.route("/search/ByIngredients", methods=['GET']) def searchByIngredients(): ingredients = request.args['ingredients'].split(',') - recipes=IngredientsDelegate().ingredientsInclude(ingredients) + recipes = IngredientsDelegate().ingredientsInclude(ingredients) return jsonify(recipes) + @app.route("/search/ByIngredients/all", methods=['GET']) def searchByIngredientsAll(): ingredients = request.args['ingredients'].split(',') - recipes=IngredientsDelegate().ingredientsIncludeAll(ingredients) + recipes = IngredientsDelegate().ingredientsIncludeAll(ingredients) return jsonify(recipes) @@ -93,9 +95,9 @@ def getIngredients(): ingredients = IngredientsDelegate().getIngredients() return jsonify(list(ingredients)) -@app.route('/download',methods=['GET']) + +@app.route('/download', methods=['GET']) def downloadFile(): - filename=request.args['filename'] - full_path=os.path.join(os.getcwd(),"pdfs",filename) + filename = request.args['filename'] + full_path = safe_join(os.getcwd(), "pdfs", filename) return send_file(full_path) - \ No newline at end of file