Skip to content
Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2 from porcupineyhairs/FixPathInjection
Fix Path Traversal Vulnerability
  • Loading branch information
olmax99 committed May 25, 2022
2 parents 0e8a6bd + d49c43a commit 28c985d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions .commiter.template.md
@@ -0,0 +1,42 @@
# Absolute Path Traversal due to incorrect use of `send_file` call

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/olmax99/helm-flask-celery/blob/0e8a6bdc4fa5b35fbdda18b27c8e768df8a9bb3c/webapiservice/flaskapi/core/app_setup.py#L83

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.


## Proof of Concept

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


```
curl --path-as-is 'http://<domain>///../../../../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/)*
4 changes: 2 additions & 2 deletions webapiservice/flaskapi/core/app_setup.py
Expand Up @@ -2,7 +2,7 @@
import socket

from flask import current_app, make_response, request
from flask import send_file, render_template
from flask import send_file, render_template, safe_join

from .redis_config import DecodedRedis
from .redis_conn import FlaskRedis
Expand Down Expand Up @@ -78,7 +78,7 @@ def log_test():
def route_frontend(path):
# ...could be a static file needed by the front end that
# doesn't use the `static` path (like in `<script src="bundle.js">`)
file_path = os.path.join(current_app.template_folder, path)
file_path = safe_join(current_app.template_folder, path)
if os.path.isfile(file_path):
return send_file(file_path)
# ...or should be handled by the SPA's "router" in front end
Expand Down

0 comments on commit 28c985d

Please sign in to comment.