Vulnerability: Directory Traversal vulnerability in GET/PUT allows attackers to Disclose Information or Write Files via a crafted GET/PUT request ### Impact

#### Information Disclosure

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.

#### File Write

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.

### Patches

PR #12 fixes the issue. The PR has been merged into version 0.4.0 and 0.4.0 has been released and pushed to PyPI. Users are advised to upgrade immediately.

### Workarounds

- Apache can apparently be configured to filter such malicious paths when reverse-proxying. 
- There are no other workarounds known.

### References

- [Pull Request #12](https://github.com/horazont/xmpp-http-upload/pull/12)
Vulnerability ID: GHSA-hwv5-w8gm-fq9f
CWE: CWE-22
Severity score: 3.5
Project: https://github.com/horazont/xmpp-http-upload
Published: 2020-10-06
File: xhu.py
Commit message: 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.
Commit author: Christian Tacke

Vulnerable version (before fix):
 import typing  import flask  app = flask.Flask("xmpp-http-upload") app.config.from_envvar("XMPP_HTTP_UPLOAD_CONFIG")     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")      return data_file, metadata_file  def load_metadata(metadata_file):         return json.load(f)   def get_info(path: str, root: pathlib.Path) -> typing.Tuple[         pathlib.Path,         dict]:     dest_path = sanitized_join(         path,         pathlib.Path(app.config["DATA_ROOT"]),     )      data_file, metadata_file = get_paths(dest_path)      return data_file, load_metadata(metadata_file)  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:         return flask.Response(             "Not Found",             404, 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)      try:         with write_file(data_file) as fout: 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"])         )          stat = data_file.stat()     except (OSError, ValueError):         return flask.Response(             "Not Found",             404, 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):         return flask.Response(             "Not Found",             404,

Fixed version (after fix):
 import typing  import flask import werkzeug.exceptions  app = flask.Flask("xmpp-http-upload") app.config.from_envvar("XMPP_HTTP_UPLOAD_CONFIG")     CORS(app)   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  def load_metadata(metadata_file):         return json.load(f)   def get_info(path: str) -> typing.Tuple[         pathlib.Path,         dict]:     data_file, metadata_file = get_paths(app.config["DATA_ROOT"], path)      return data_file, load_metadata(metadata_file)  def stream_file(src, dest, nbytes): @app.route("/<path:path>", methods=["PUT"]) def put_file(path):     try:         data_file, metadata_file = get_paths(app.config["DATA_ROOT"], path)     except werkzeug.exceptions.NotFound:         return flask.Response(             "Not Found",             404, def put_file(path):         "application/octet-stream",     )      data_file.parent.mkdir(parents=True, exist_ok=True, mode=0o770)      try:         with write_file(data_file) as fout: def generate_headers(response_headers, metadata_headers): @app.route("/<path:path>", methods=["HEAD"]) def head_file(path):     try:         data_file, metadata = get_info(path)          stat = data_file.stat()     except (OSError, werkzeug.exceptions.NotFound):         return flask.Response(             "Not Found",             404, def head_file(path): @app.route("/<path:path>", methods=["GET"]) def get_file(path):     try:         data_file, metadata = get_info(path)     except (OSError, werkzeug.exceptions.NotFound):         return flask.Response(             "Not Found",             404,

Patch:
patch: @@ -29,6 +29,7 @@
 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 @@
     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,
path: xhu.py