Skip to content

Commit

Permalink
fix path traversal vulnerability (#156)
Browse files Browse the repository at this point in the history
* add test for issue #129
* 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”.

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/HolgerGraef/MSM/blob/6dd2c9557e0285e1270c84375ebd6f8d10e422a4/app/main/views.py#L544

Here, the `path` 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.

The bug can be verified using a proof of concept similar to the one shown below.

```
curl --path-as-is 'http://<domain>/plugins//../../../../etc/passwd"'
```

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.

* [OWASP Path Traversal](https://owasp.org/www-community/attacks/Path_Traversal)
* github/securitylab#669

* coding style: black
* backport to old werkzeug version
* cleanup

---------

Co-authored-by: Porcupiney Hairs <[email protected]>
  • Loading branch information
hgrf and Porcupiney Hairs authored Mar 11, 2023
1 parent 1f593b5 commit bd06f78
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ class ProductionConfig(Config):


class TestingConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") or "sqlite:///" + os.path.join(
basedir, "database/testing.sqlite"
)
LOG_EXCEPTIONS = True
TESTING = True
WTF_CSRF_ENABLED = False


config = {
Expand Down
8 changes: 6 additions & 2 deletions app/main/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask import render_template, redirect, request, jsonify, send_file, abort, current_app
from werkzeug.security import safe_join
from flask_login import current_user, login_required, login_user, logout_user
from .. import db
from .. import plugins
Expand Down Expand Up @@ -678,8 +679,11 @@ def swapactionorder(): # TODO: sort out permissions for this (e.g. who has the
@main.route("/plugins/<path:path>")
@login_required
def static_file(path):
# TODO: this looks a bit unsafe to me
return send_file("../plugins/" + path)
path = safe_join("../plugins/", path)
if path is None:
abort(404)
else:
return send_file(path)


def str_to_bool(str):
Expand Down
26 changes: 26 additions & 0 deletions app/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ def test_instantiate_app():
assert app is not None


def test_issue129():
"""Test that the issue #129 is fixed."""
app = create_app("testing")
migrate = Migrate()

# make sure that testing DB does not exist
db_path = app.config.get("SQLALCHEMY_DATABASE_URI").replace("sqlite:///", "")
if os.path.exists(db_path):
os.remove(db_path)

with app.app_context():
db.init_app(app)
migrate.init_app(app, db)
upgrade()

# recreate app to initialize activity table
app = create_app("testing")

with app.test_client() as c:
# log in via HTTP
r = c.post("/auth/login", data={"username": "admin", "password": "admin"})
assert r.status_code == 302
r = c.get("/plugins/../README.md")
assert r.status_code == 404


def test_db_migrations():
"""Test that the database migrations can be run."""
app = create_app("testing")
Expand Down

0 comments on commit bd06f78

Please sign in to comment.