From 98a511e3e26d98d09fa989443de4972880e1c72d Mon Sep 17 00:00:00 2001 From: Porcupiney Hairs Date: Fri, 29 Apr 2022 01:55:57 +0530 Subject: [PATCH 1/2] # 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/onlaj/Piano-LED-Visualizer/blob/6a732caa812c83a807c711f3d091af99209cae7b/webinterface/views_api.py#L970 Here, the `value` 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:///api/change_setting?second_value=no_reload&disable_sequence=true&value=../../../../../../../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 `flask.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/)* --- webinterface/views_api.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webinterface/views_api.py b/webinterface/views_api.py index a70bb4aa..dfec18a4 100644 --- a/webinterface/views_api.py +++ b/webinterface/views_api.py @@ -1,5 +1,5 @@ from webinterface import webinterface -from flask import render_template, send_file, redirect, request, url_for, jsonify +from flask import render_template, send_file, safe_join, redirect, request, url_for, jsonify from lib.functions import find_between, theaterChase, theaterChaseRainbow, sound_of_da_police, scanner, breathing, \ rainbow, rainbowCycle, fastColorWipe, play_midi, clamp import psutil @@ -967,7 +967,7 @@ def change_setting(): return send_file("../Songs/" + value.replace(".mid", "") + ".zip", mimetype='application/x-csv', attachment_filename=value.replace(".mid", "") + ".zip", as_attachment=True) else: - return send_file("../Songs/" + value, mimetype='application/x-csv', attachment_filename=value, + return send_file(safe_join("../Songs/" + value), mimetype='application/x-csv', attachment_filename=value, as_attachment=True) if setting_name == "download_sheet_music": @@ -982,7 +982,7 @@ def change_setting(): i += 1 webinterface.learning.convert_midi_to_abc(value) try: - return send_file("../Songs/" + value.replace(".mid", ".abc"), mimetype='application/x-csv', + return send_file(safe_join("../Songs/", value.replace(".mid", ".abc")), mimetype='application/x-csv', attachment_filename=value.replace(".mid", ".abc"), as_attachment=True) except: print("Converting failed") From 5f4a84b891a2796527d060f9083baae0771a6bfb Mon Sep 17 00:00:00 2001 From: porcupineyhairs <61983466+porcupineyhairs@users.noreply.github.com> Date: Fri, 29 Apr 2022 15:24:38 +0530 Subject: [PATCH 2/2] update import statement --- webinterface/views_api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webinterface/views_api.py b/webinterface/views_api.py index dfec18a4..05f51942 100644 --- a/webinterface/views_api.py +++ b/webinterface/views_api.py @@ -1,5 +1,6 @@ from webinterface import webinterface -from flask import render_template, send_file, safe_join, redirect, request, url_for, jsonify +from flask import render_template, send_file, redirect, request, url_for, jsonify +from werkzeug.utils import safe_join from lib.functions import find_between, theaterChase, theaterChaseRainbow, sound_of_da_police, scanner, breathing, \ rainbow, rainbowCycle, fastColorWipe, play_midi, clamp import psutil