CVE-2021-41185: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: Mycodo is an environmental monitoring and regulation system. An exploit in versions prior to 8.12.7 allows anyone with access to endpoints to download files outside the intended directory. A patch has been applied and a release made. Users should upgrade to version 8.12.7. As a workaround, users may manually apply the changes from the fix commit.
CWE 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.
CVSS v3 base score: 6.5
Published: 2021-10-26T15:15Z
Repository: https://github.com/kizniche/Mycodo
Language: Python
Files changed: CHANGELOG.md, mycodo/mycodo_flask/routes_general.py
Commit date: 2021-10-25 21:46:10 -0400
Commit message: Fix file exploit vulnerability (#1105)

Diff: diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5c273e20..2825d825 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,14 +1,17 @@
-## 8.12.7 (Unreleased)
+## 8.12.7 (2021-10-25)
+
+This is a bugfix release that includes a fix to a severe security vulnerability. It is recommended that all users that have Mycodo exposed to the internet and allow guest access upgrade to patch this vulnerability. Users that only run Mycodo on a local network and/or don't allow unknown user (i.e. guest) access likely won't be affected.
 
 ### Bugfixes
 
  - Fix refreshing LCD Display Function line options when changing number of lines
  - Fix installation of Function Action dependencies
  - Fix error when unauthenticated users attempting to land on the home page
  - Fix Gauge Widget dependencies ([#1100](https://github.com/kizniche/mycodo/issues/1100))
  - Fix installation of pigpiod
+ - Fix file exploit vulnerability ([#1105](https://github.com/kizniche/mycodo/issues/1105))
 
 ### Features
 
  - Add ability to install on most Debian-based systems
  - Add ability for Actions to work on Function Controllers
diff --git a/mycodo/mycodo_flask/routes_general.py b/mycodo/mycodo_flask/routes_general.py
index c513c36d..498a622e 100644
--- a/mycodo/mycodo_flask/routes_general.py
+++ b/mycodo/mycodo_flask/routes_general.py
@@ -115,11 +115,12 @@ def page_settings():
 def send_note_attachment(filename):
     """Return a file from the note attachment directory"""
     file_path = os.path.join(PATH_NOTE_ATTACHMENTS, filename)
     if file_path is not None:
         try:
-            return send_file(file_path, as_attachment=True)
+            if os.path.abspath(file_path).startswith(PATH_NOTE_ATTACHMENTS):
+                return send_file(file_path, as_attachment=True)
         except Exception:
             logger.exception("Send note attachment")
 
 
 @blueprint.route('/camera/<camera_unique_id>/<img_type>/<filename>')
@@ -147,11 +148,12 @@ def camera_img_return_path(camera_unique_id, img_type, filename):
                  if os.path.isfile(os.path.join(path, files)))
     else:
         files = []
     if filename in files:
         path_file = os.path.join(path, filename)
-        return send_file(path_file, mimetype='image/jpeg')
+        if os.path.abspath(path_file).startswith(path):
+            return send_file(path_file, mimetype='image/jpeg')
 
     return "Image not found"
 
 
 @blueprint.route('/camera_acquire_image/<image_type>/<camera_unique_id>/<max_age>')

Vulnerable code: ## 8.12.7 (Unreleased)
            return send_file(file_path, as_attachment=True)
        return send_file(path_file, mimetype='image/jpeg')
Fixed code: ## 8.12.7 (2021-10-25)

This is a bugfix release that includes a fix to a severe security vulnerability. It is recommended that all users that have Mycodo exposed to the internet and allow guest access upgrade to patch this vulnerability. Users that only run Mycodo on a local network and/or don't allow unknown user (i.e. guest) access likely won't be affected.
 - Fix file exploit vulnerability ([#1105](https://github.com/kizniche/mycodo/issues/1105))
            if os.path.abspath(file_path).startswith(PATH_NOTE_ATTACHMENTS):
                return send_file(file_path, as_attachment=True)
        if os.path.abspath(path_file).startswith(path):
            return send_file(path_file, mimetype='image/jpeg')