CVE-2024-47164
Directory traversal vulnerability in Gradio's `is_in_or_equal` function.
- CVSS 2.3
- CWE-22
- Input Validation and Sanitization
- Remote
Gradio is an open-source Python package designed for quick prototyping. This vulnerability relates to the **bypass of directory traversal checks** within the `is_in_or_equal` function. This function, intended to check if a file resides within a given directory, can be bypassed with certain payloads that manipulate file paths using `..` (parent directory) sequences. Attackers could potentially access restricted files if they are able to exploit this flaw, although the difficulty is high. This primarily impacts users relying on Gradio’s blocklist or directory access validation, particularly when handling file uploads. Users are advised to upgrade to `gradio>=5.0` to address this issue. As a workaround, users can manually sanitize and normalize file paths in their Gradio deployment before passing them to the `is_in_or_equal` function. Ensuring that all file paths are properly resolved and absolute can help mitigate the bypass vulnerabilities caused by the improper handling of `..` sequences or malformed paths.
- CWE
- CWE-22
- CVSS base score
- 2.3
- Published
- 2024-10-10
- OWASP
- A05 Security Misconfiguration
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Check
- Category
- Input Validation and Sanitization
- Subcategory
- Path Traversal
- Accessibility scope
- Remote
- Impact
- Unauthorized Access
- Affected component
- Gradio
- Fixed by upgrading
- Yes
Solution
Upgrade to `gradio>=5.0`.
Vulnerable code sample
import os
from pathlib import Path
def is_in_or_equal(base_path: str, target_path: str) -> bool:
"""
Returns True if target_path is the same as or a sub‑directory of base_path.
"""
# VULNERABLE: does not resolve ".." sequences, allowing traversal bypass
base = Path(base_path)
target = Path(target_path)
try:
return os.path.commonpath([str(base), str(target)]) == str(base)
except ValueError:
return FalsePatched code sample
import os
from pathlib import Path
def is_in_or_equal(base_path: str, target_path: str) -> bool:
"""
Returns True if target_path is the same as or a sub‑directory of base_path.
"""
# FIX: resolve paths to eliminate ".." traversal before comparison
base = Path(base_path).resolve()
target = Path(target_path).resolve()
try:
return os.path.commonpath([str(base), str(target)]) == str(base)
except ValueError:
return FalsePublished fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Some tweaks to is_in_or_equal (#9020) (gradio-app/gradio)
gradio/blocks.py,gradio/components/base.py,gradio/components/file_explorer.py,gradio/components/file_explorer.py · FileExplorer._safe_join,gradio/components/file_explorer.py · FileExplorer.__init__,gradio/data_classes.py,gradio/exceptions.py,gradio/routes.py,gradio/routes.py · App.create_app,gradio/route_utils.py,gradio/utils.py,gradio/utils.py · abspathand 1 more
Payload
"../restricted_file.txt"
Cite this entry
@misc{vaitp:cve202447164,
title = {{Directory traversal vulnerability in Gradio's `is_in_or_equal` function.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2024},
note = {VAITP Python Vulnerability Dataset, entry CVE-2024-47164},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2024-47164/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
