Vulnerability: The woduq1414/munhak-moa repository before 2022-05-03 on GitHub allows absolute path traversal because the Flask send_file function is used unsafely.
Vulnerability ID: CVE-2022-31564
CWE: CWE-22
Severity score: 4.9
Project: https://github.com/woduq1414/munhak-moa
Published: 2022-07-11
File: app.py
Commit message: # 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”.

## Common Weakness Enumeration category
CWE - 36

## 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/woduq1414/munhak-moa/blob/cdcc98959879c2e88a89eea164e806b3b0e09972/app.py#L273

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>/images//../../../../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 `werkzeug.utils.safe_join` to join untrusted paths or replace `flask.send_file` calls with `flask.send_from_directory` calls.

## Common Vulnerability Scoring System Vector

The attack can be carried over the network. A complex non-standard configuration or a specialized condition is not required for the attack to be successfully conducted. There is no user interaction required for successful execution. The attack can affect components outside the scope of the target module. The attack can be used to gain access to confidential files like passwords, login credentials and other secrets. It cannot be directly used to affect a change on a system resource. Hence has limited to no impact on integrity. Using this attack vector a attacker may make multiple requests for accessing huge files such as a database. This can lead to a partial system denial service. However, the impact on availability is quite low in this case. Taking this account an appropriate CVSS v3.1 vector would be

(AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:N/A:L)[https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator?vector=AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:N/A:L&version=3.1]

This gives it a base score of 9.3/10 and a severity rating of critical.

## 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/)*
Commit author: Porcupiney Hairs

Vulnerable version (before fix):
 import configparser  from flask import Flask, render_template, session, request, flash, redirect, url_for, Response, abort, jsonify, send_file import socket import os import random def get_absolute_path(path):         import os         script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in         rel_path = path         abs_file_path = os.path.join(script_dir, rel_path)         return abs_file_path      return send_file(

Fixed version (after fix):
 import configparser  from flask import Flask, render_template, session, request, flash, redirect, url_for, Response, abort, jsonify, send_file from werkzeug.utils import safe_join import socket import os import random def get_absolute_path(path):         import os         script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in         rel_path = path         abs_file_path = safe_join(script_dir, rel_path)         return abs_file_path      return send_file(

Patch:
patch: @@ -1,6 +1,7 @@
 import configparser
 
 from flask import Flask, render_template, session, request, flash, redirect, url_for, Response, abort, jsonify, send_file
+from werkzeug.utils import safe_join
 import socket
 import os
 import random
@@ -267,7 +268,7 @@ def get_absolute_path(path):
         import os
         script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in
         rel_path = path
-        abs_file_path = os.path.join(script_dir, rel_path)
+        abs_file_path = safe_join(script_dir, rel_path)
         return abs_file_path
 
     return send_file(
path: app.py