CVE-2020-15239 (published 2020-10-06T19:15Z)

In xmpp-http-upload before version 0.4.0, when the GET method is attacked, attackers can read files which have a `.data` suffix and which are accompanied by a JSON file with the `.meta` suffix. This can lead to Information Disclosure and in some shared-hosting scenarios also to circumvention of authentication or other limitations on the outbound (GET) traffic. For example, in a scenario where a single server has multiple instances of the application running (with separate DATA_ROOT settings), an attacker who has knowledge about the directory structure is able to read files from any other instance to which the process has read access. If instances have individual authentication (for example, HTTP authentication via a reverse proxy, source IP based filtering) or other restrictions (such as quotas), attackers may circumvent those limits in such a scenario by using the Directory Traversal to retrieve data from the other instances. If the associated XMPP server (or anyone knowing the SECRET_KEY) is malicious, they can write files outside the DATA_ROOT. The files which are written are constrained to have the `.meta` and the `.data` suffixes; the `.meta` file will contain the JSON with the Content-Type of the original request and the `.data` file will contain the payload. The issue is patched in version 0.4.0.

Severity: MEDIUM
CVSS v2 base score: 4.0 (AV:N/AC:L/Au:S/C:P/I:N/A:N)
CVSS v3 base score: 3.5, LOW (CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:C/C:L/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/horazont/xmpp-http-upload
commit_details: repo_url: https://github.com/horazont/xmpp-http-upload
author: Christian Tacke
author_date: 2020-10-06 01:06:21+02:00
msg: Simplify path handling, use safe_join

The current implementation of sanitized_join did not handle
".." properly. The problem is, that .absolute() does not do
what .resolve() does, but .resolve() does not work on non
existant paths.

Anyway, flask has a function exactly for this: safe_join.

So let's use that one.

While at it, simplified the whole path handling a bit.
repository: repo_url: https://github.com/horazont/xmpp-http-upload
repo_name: horazont/xmpp-http-upload
description: An external HTTP upload (XEP-0363) implementation for use with e.g. Prosody
repo_language: Python
owner: horazont
file_changes: filename: xhu.py
change_type: ModificationType.MODIFY
diff: @@ -29,6 +29,7 @@ import pathlib
 import typing
 
 import flask
+import werkzeug.exceptions
 
 app = flask.Flask("xmpp-http-upload")
 app.config.from_envvar("XMPP_HTTP_UPLOAD_CONFIG")
@@ -39,16 +40,11 @@ if app.config['ENABLE_CORS']:
     CORS(app)
 
 
-def sanitized_join(path: str, root: pathlib.Path) -> pathlib.Path:
-    result = (root / path).absolute()
-    if not str(result).startswith(str(root) + "/"):
-        raise ValueError("resulting path is outside root")
-    return result
-
-
-def get_paths(base_path: pathlib.Path):
-    data_file = pathlib.Path(str(base_path) + ".data")
-    metadata_file = pathlib.Path(str(base_path) + ".meta")
+def get_paths(root: str, sub_path: str) \
+        -> typing.Tuple[pathlib.Path, pathlib.Path]:
+    base_path = flask.safe_join(root, sub_path)
+    data_file = pathlib.Path(base_path + ".data")
+    metadata_file = pathlib.Path(base_path + ".meta")
 
     return data_file, metadata_file
 
@@ -58,15 +54,10 @@ def load_metadata(metadata_file):
         return json.load(f)
 
 
-def get_info(path: str, root: pathlib.Path) -> typing.Tuple[
+def get_info(path: str) -> typing.Tuple[
         pathlib.Path,
         dict]:
-    dest_path = sanitized_join(
-        path,
-        pathlib.Path(app.config["DATA_ROOT"]),
-    )
-
-    data_file, metadata_file = get_paths(dest_path)
+    data_file, metadata_file = get_paths(app.config["DATA_ROOT"], path)
 
     return data_file, load_metadata(metadata_file)
 
@@ -104,11 +95,8 @@ def stream_file(src, dest, nbytes):
 @app.route("/<path:path>", methods=["PUT"])
 def put_file(path):
     try:
-        dest_path = sanitized_join(
-            path,
-            pathlib.Path(app.config["DATA_ROOT"]),
-        )
-    except ValueError:
+        data_file, metadata_file = get_paths(app.config["DATA_ROOT"], path)
+    except werkzeug.exceptions.NotFound:
         return flask.Response(
             "Not Found",
             404,
@@ -134,8 +122,7 @@ def put_file(path):
         "application/octet-stream",
     )
 
-    dest_path.parent.mkdir(parents=True, exist_ok=True, mode=0o770)
-    data_file, metadata_file = get_paths(dest_path)
+    data_file.parent.mkdir(parents=True, exist_ok=True, mode=0o770)
 
     try:
         with write_file(data_file) as fout:
@@ -189,13 +176,10 @@ def generate_headers(response_headers, metadata_headers):
 @app.route("/<path:path>", methods=["HEAD"])
 def head_file(path):
     try:
-        data_file, metadata = get_info(
-            path,
-            pathlib.Path(app.config["DATA_ROOT"])
-        )
+        data_file, metadata = get_info(path)
 
         stat = data_file.stat()
-    except (OSError, ValueError):
+    except (OSError, werkzeug.exceptions.NotFound):
         return flask.Response(
             "Not Found",
             404,
@@ -214,11 +198,8 @@ def head_file(path):
 @app.route("/<path:path>", methods=["GET"])
 def get_file(path):
     try:
-        data_file, metadata = get_info(
-            path,
-            pathlib.Path(app.config["DATA_ROOT"])
-        )
-    except (OSError, ValueError):
+        data_file, metadata = get_info(path)
+    except (OSError, werkzeug.exceptions.NotFound):
         return flask.Response(
             "Not Found",
             404,

programming_language: Python