Vulnerability: Arbitrary file reading vulnerability in Aim ### Impact
A path traversal attack 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.

Vulnerable code: https://github.com/aimhubio/aim/blob/0b99c6ca08e0ba7e7011453a2f68033e9b1d1bce/aim/web/api/views.py#L9-L16

### Patches
The vulnerability issue is resolved in Aim v3.1.0.

### References
https://owasp.org/www-community/attacks/Path_Traversal
Vulnerability ID: GHSA-8phj-f9w2-cjcc
CWE: CWE-22
Severity score: 8.6
Project: https://github.com/aimhubio/aim
Published: 2021-11-23
File: aim/web/api/views.py
Commit message: Fix security issue when incorrect path is given to the endpoint that serves static files which can lead to a leak of non wanted files (e.g. /static-files/../../../../etc/passwd)
Commit author: mihran113

Vulnerable version (before fix):
 import os  from aim.web.api.utils import APIRouter  # wrapper for fastapi.APIRouter from fastapi.responses import FileResponse  statics_router = APIRouter()   @statics_router.get('/static-files/{path:path}/') async def serve_static_files(path):     from aim import web     static_file_name = os.path.join(os.path.dirname(web.__file__), 'ui', 'build', path)     compressed_file_name = '{}.gz'.format(static_file_name)     if os.path.exists(compressed_file_name):         return FileResponse(compressed_file_name, headers={'Content-Encoding': 'gzip'})

Fixed version (after fix):
 import os from pathlib import Path  from aim.web.api.utils import APIRouter  # wrapper for fastapi.APIRouter from fastapi.responses import FileResponse from fastapi import HTTPException  statics_router = APIRouter()   @statics_router.get('/static-files/{path:path}/') async def serve_static_files(path):     from aim import web     static_file_root = os.path.join(os.path.dirname(web.__file__), 'ui', 'build')     static_file_name = os.path.join(static_file_root, path)      # check if path is leading inside ui/build directory     if not Path(static_file_root) in Path(static_file_name).resolve().parents:         raise HTTPException(404)      compressed_file_name = '{}.gz'.format(static_file_name)     if os.path.exists(compressed_file_name):         return FileResponse(compressed_file_name, headers={'Content-Encoding': 'gzip'})

Patch:
patch: @@ -1,15 +1,23 @@
 import os
+from pathlib import Path
 
 from aim.web.api.utils import APIRouter  # wrapper for fastapi.APIRouter
 from fastapi.responses import FileResponse
+from fastapi import HTTPException
 
 statics_router = APIRouter()
 
 
 @statics_router.get('/static-files/{path:path}/')
 async def serve_static_files(path):
     from aim import web
-    static_file_name = os.path.join(os.path.dirname(web.__file__), 'ui', 'build', path)
+    static_file_root = os.path.join(os.path.dirname(web.__file__), 'ui', 'build')
+    static_file_name = os.path.join(static_file_root, path)
+
+    # check if path is leading inside ui/build directory
+    if not Path(static_file_root) in Path(static_file_name).resolve().parents:
+        raise HTTPException(404)
+
     compressed_file_name = '{}.gz'.format(static_file_name)
     if os.path.exists(compressed_file_name):
         return FileResponse(compressed_file_name, headers={'Content-Encoding': 'gzip'})
path: aim/web/api/views.py