{"id":1503,"cve":"CVE-2025-66645","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2025-66645\/","cwe":"CWE-22","cwe_number":22,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/22","cvss_score":7.5,"published":"2025-12-09","owasp":"A01 Broken Access Control","odc":"Checking","cdc":"Missing Check","category":"Input Validation and Sanitization","subcategory":"Path Traversal","scope":"Remote","impact":"Information Disclosure","summary":"NiceGUI <= 3.3.1 has a directory traversal vulnerability in add_media_files.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2025-66645\/","description":"NiceGUI is a Python-based UI framework. Versions 3.3.1 and below are vulnerable to  directory traversal through the App.add_media_files() function, which allows a remote attacker to read arbitrary files on the server filesystem. This issue is fixed in version 3.4.0.","solution":"Upgrade NiceGUI to version 3.4.0 or later.","affected_component":"NiceGUI","fixed_by_upgrading":true,"vulnerable_code":"import os\nimport uvicorn\nfrom fastapi import FastAPI, HTTPException\nfrom starlette.responses import FileResponse\nfrom pathlib import Path\n\n# NOTE: This code is a representation of the described vulnerability.\n# The CVE number is fictional, and this code uses FastAPI (NiceGUI's foundation)\n# to simulate the directory traversal flaw.\n\n# --- Environment Setup for Demonstration ---\n# Create a directory structure to demonstrate the traversal\nAPP_ROOT = Path(__file__).parent\nMEDIA_DIR = APP_ROOT \/ \"media\"\nSECRET_FILE = APP_ROOT \/ \"secret.txt\"\n\nos.makedirs(MEDIA_DIR, exist_ok=True)\n\n# Create a safe file inside the intended media directory\nwith open(MEDIA_DIR \/ \"public.txt\", \"w\") as f:\n    f.write(\"This is a public file.\")\n\n# Create a secret file outside the media directory that the attacker will try to access\nwith open(SECRET_FILE, \"w\") as f:\n    f.write(\"This is a sensitive server secret.\")\n# -----------------------------------------\n\napp = FastAPI()\n\n# This function simulates the behavior of the vulnerable `App.add_media_files()`\n# by creating a route that serves files from a local directory.\ndef add_media_files_vulnerable(url_path: str, local_directory: Path):\n    @app.get(f\"\/{url_path}\/{{filepath:path}}\")\n    async def serve_file(filepath: str):\n        # VULNERABILITY: The user-provided 'filepath' is directly joined with the\n        # base directory path without any sanitization or security checks.\n        # An attacker can use '..' path components to traverse up the filesystem.\n        requested_path = os.path.join(local_directory, filepath)\n\n        # The application checks if the file exists but does not validate that\n        # the resolved path is still within the intended 'local_directory'.\n        if os.path.exists(requested_path) and os.path.isfile(requested_path):\n            return FileResponse(requested_path)\n        \n        raise HTTPException(status_code=404, detail=\"File not found\")\n\n# A developer using the vulnerable version of the library would call this function.\nadd_media_files_vulnerable(url_path=\"media\", local_directory=MEDIA_DIR)\n\n\nif __name__ == \"__main__\":\n    # To test the vulnerability:\n    # 1. Install dependencies: pip install fastapi uvicorn\n    # 2. Run this script: python your_script_name.py\n    # 3. Access the legitimate file: http:\/\/127.0.0.1:8000\/media\/public.txt\n    # 4. Exploit the traversal to access the secret file: http:\/\/127.0.0.1:8000\/media\/..\/secret.txt\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)","patched_code":"import pathlib\nimport os\n\nclass App:\n    \"\"\"\n    This is a simplified, conceptual representation of the NiceGUI App class\n    to demonstrate how a directory traversal vulnerability can be fixed.\n    \"\"\"\n    def __init__(self):\n        # In a real application, this would be the root directory for media files.\n        # .resolve() is used to get the canonical absolute path of the directory.\n        self.media_root = pathlib.Path('nicegui_media_root').resolve()\n        # For demonstration, ensure the base directory exists.\n        self.media_root.mkdir(exist_ok=True)\n\n    def add_media_files(self, url_path: str, local_folder_from_user: str) -> None:\n        \"\"\"\n        A conceptual, fixed version of the vulnerable add_media_files function.\n\n        This version prevents directory traversal by ensuring the resolved\n        file path is safely within the intended media root directory before\n        it is used.\n        \"\"\"\n        # Step 1: Resolve the path provided by the user against the media root.\n        # The `resolve()` method is critical as it canonicalizes the path,\n        # processing any '..' components and symbolic links.\n        # A vulnerable implementation might just use os.path.join() without resolving.\n        try:\n            requested_path = (self.media_root \/ local_folder_from_user).resolve()\n        except FileNotFoundError:\n            # If the base path doesn't exist, we can't serve it. This is a\n            # standard error, not a security flaw in itself.\n            # In a real app, this might log an error or simply do nothing.\n            return\n\n        # Step 2: THE FIX - Check if the resolved path is within the allowed media root.\n        # This is the core of the directory traversal prevention. It ensures\n        # that the resulting path has not \"escaped\" the intended parent directory.\n        # For Python 3.9+, one could use: `requested_path.is_relative_to(self.media_root)`\n        # The method below is compatible with older Python versions.\n        common_path = os.path.commonpath([self.media_root, requested_path])\n\n        if str(self.media_root) != common_path:\n            # If the longest common path is not the media root itself, it means the\n            # user-provided path has traversed outside the allowed directory.\n            raise PermissionError(\n                f\"Directory traversal attempt blocked. The path '{local_folder_from_user}' \"\n                \"resolves outside the allowed media directory.\"\n            )\n\n        # Step 3: If the check passes, the application can safely proceed.\n        # The original logic to register the path for serving would go here.\n        # For example: self._register_media_route(url_path, requested_path)\n        pass","payload_needed":true,"payload":"..\/..\/..\/..\/..\/..\/etc\/passwd","payload_status":"available","published_fix":[{"repository":"zauberzeug\/nicegui","commit_url":"https:\/\/github.com\/zauberzeug\/nicegui\/commit\/a1b89e2a24e1911a40389ace2153a37f4eea92a9","commit_subject":"Merge commit from fork","units":[{"file":"nicegui\/app\/app.py","function":"App.add_media_files"}]}],"references":null}