Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Path Traversal Vulnerability #351

Merged
merged 2 commits into from Apr 29, 2022
Merged

Fix Path Traversal Vulnerability #351

merged 2 commits into from Apr 29, 2022

Conversation

porcupineyhairs
Copy link
Contributor

Fixes #350

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://<domain>/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/)*
@onlaj
Copy link
Owner

onlaj commented Apr 29, 2022

Thank you for your contribution and the detailed explanation of the vulnerability.
Although the application should only run on a local network and should not be vulnerable to attacks it is indeed a dangerous bug and should be patched.

I checked your pull request and it seems to work as expected, but PyCharm returns me a warning
flask.helpers.safe_join' is deprecated and will be removed in Flask 2.1. Use 'werkzeug.utils.safe_join' instead.

I assume that adding import werkzeug and then changing safe_join to werkzeug.utils.safe_join should be enough to make it more future proof.

@porcupineyhairs
Copy link
Contributor Author

@onlaj changes done! can you please request a GHSA advisory for this?

@onlaj
Copy link
Owner

onlaj commented Apr 29, 2022

Sure, I can do it. Should I copy into the description what you wrote in the #350?

@porcupineyhairs
Copy link
Contributor Author

@onlaj Yes, please do. #350 should cover most of what you require

@onlaj
Copy link
Owner

onlaj commented Apr 29, 2022

Two more questions if you don't mind.
What is the purpose of creating security advisory in that case?
What should I put in "ecosystem" tab?
chrome_A29Y1SoOM2

@porcupineyhairs
Copy link
Contributor Author

@onlaj Ecosystem means the primary language ecosystem to which the project belongs. In this case, since the bug is in python code, you may select pip.

As for severity, please select assess severity using CVSS. Then please paste the following CVSS vector in the input.
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:L/A:L

For CWE, please put CWE-073. In the CVE field, select request CVE ID later. Github will go through the submission and issue a CVE. You can use this CVE ID to notify downstream users of the vulnerability and the fix.

@onlaj
Copy link
Owner

onlaj commented Apr 29, 2022

I made a draft and added you as collaborator, but I still don't fully understand what is it for.
I can press "Request CVE" and it says that: Once requested, GitHub will review this advisory in order to assign a CVE.
If I understand it correctly they will use it to notify other users about that vulnerability if it is found in their repositories, right?

@porcupineyhairs
Copy link
Contributor Author

@onlaj I have made a bunch to the draft advisory. PTAL.

A CVE ID is used to identify a security vulnerability in a product. By issuing a CVE you allow the users of your software to be notified about a security vulnerability. If this software is used by other downstream libraries, a CVE would nudge them to upgrade to the patched version. Also sometimes, Github may even create a dependabot alert for users to auto-patch the bug.

@onlaj
Copy link
Owner

onlaj commented Apr 29, 2022

Ok, I understand now.
Should I "Request CVE" or "Publish advisory"?

@porcupineyhairs
Copy link
Contributor Author

@onlaj I don't know. You should be doing both. I think request CVE would automatically publish the advisory so try that.

@onlaj onlaj merged commit 3f10602 into onlaj:master Apr 29, 2022
scaraude pushed a commit to scaraude/Piano-LED-Visualizer that referenced this pull request Jun 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Security Vulnerability Found
2 participants