Skip to content

Commit

Permalink
# Absolute Path Traversal due to incorrect use of send_file call
Browse files Browse the repository at this point in the history
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

Passing untrusted input to `flask.send_file`can lead to path traversal attacks.

In this case, the problems occurs due to the following code :
https://github.com/piaoyunsoft/bt_lnmp/blob/fa49519b04586a00e76c105e7ce1da36eadf6922/www/server/panel/BTPanel/__init__.py#L858

Here, the `filename` parameter is attacker controlled and is used as the filename passed to the `send_file` call. This leads to a path traversal attack.

## 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](codeql.github.com/)*
  • Loading branch information
Porcupiney Hairs committed Apr 28, 2022
1 parent 976b22a commit 184e0ee
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions sphere/gui/gui_server.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import flask
from furl import furl
from datetime import date
import os
import pathlib
from .. import db_drivers


app = flask.Flask(__name__)


@app.route('/')
def index():
users = app._db_driver.get_users()
return flask.render_template('index.html', users=users)


@app.route('/<int:user_id>')
def user(user_id):
user = app._db_driver.get_users(user_id=user_id)
Expand All @@ -20,28 +23,32 @@ def user(user_id):
return flask.render_template('user.html', user=user, snapshots=snapshots)
return 'wrong page', 404


@app.route('/<int:user_id>/<int:snapshot_id>')
def snapshot(user_id, snapshot_id):
user = app._db_driver.get_users(user_id=user_id)
if not user:
return 'wrong page', 404
snapshot = app._db_driver.get_snapshots(user_id=user_id, snapshot_id=snapshot_id)
snapshot = app._db_driver.get_snapshots(
user_id=user_id, snapshot_id=snapshot_id)
if not snapshot:
return 'wrong page', 404
return flask.render_template('snapshot.html', user=user, snapshot=snapshot)


@app.route('/uploads/<path:path>')
def download_file(path):
return flask.send_file(
'../../' + path,
attachment_filename=f'{path}.jpg',
mimetype='image/jpg')
flask.safe_join(os.path.join(os.path.dirname(
os.path.abspath(__file__)), '../../'), path),
attachment_filename=f'{path}.jpg',
mimetype='image/jpg')


def run_server(host='0.0.0.0', port=8080, database_url='mongodb://0.0.0.0:27017/'):
"""
Runs the GUI server on host, port that connects to the database.
:param host: The GUI's host address, defaults to '0.0.0.0'
:type host: str, optional
:param port: The GUI's port number, defaults to 8080
Expand All @@ -50,6 +57,6 @@ def run_server(host='0.0.0.0', port=8080, database_url='mongodb://0.0.0.0:27017/
:type database_url: str
"""
f = furl(database_url)
db, db_host, db_port = f.scheme, f.host, f.port
db, db_host, db_port = f.scheme, f.host, f.port
app._db_driver = db_drivers[db].Driver(db_host, db_port)
app.run(host=host, port=port)
app.run(host=host, port=port)

0 comments on commit 184e0ee

Please sign in to comment.