VAITP Dataset

← Back to the dataset

CVE-2023-29159

Directory traversal vuln. in Starlette 0.13.5 to 0.26.0 lets remote attackers view web service files

  • CVSS 7.5
  • CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
  • Input Validation and Sanitization
  • Remote

Directory traversal vulnerability in Starlette versions 0.13.5 and later and prior to 0.27.0 allows a remote unauthenticated attacker to view files in a web service which was built using Starlette.

CVSS base score
7.5
Published
2023-06-01
OWASP
A05 Security Misconfiguration
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Input Validation and Sanitization
Subcategory
Path Traversal
Accessibility scope
Remote
Impact
Information Disclosure
Affected component
Starlette
Fixed by upgrading
Yes

Solution

Update Starlette to version 0.27.0 or higher.

Vulnerable code sample

from starlette.applications import Starlette
from starlette.responses import FileResponse
from starlette.routing import Route

async def serve_file(request):
    file_path = request.path_params['file_name']
    # Vulnerable to directory traversal
    return FileResponse(file_path)

app = Starlette(routes=[
    Route('/files/{file_name:path}', serve_file),
])

Patched code sample

from starlette.applications import Starlette
from starlette.responses import FileResponse
from starlette.routing import Route
import os

# Define a safe base directory for serving files
BASE_DIR = '/safe/base/directory'

async def serve_file(request):
    file_path = request.path_params['file_name']
    # Ensure the file path is safe and within the base directory
    safe_path = os.path.join(BASE_DIR, file_path)
    
    # Check if the resolved path starts with the base directory
    if not os.path.commonpath([safe_path, BASE_DIR]) == BASE_DIR:
        return PlainTextResponse("Forbidden", status_code=403)
    
    return FileResponse(safe_path)

app = Starlette(routes=[
    Route('/files/{file_name:path}', serve_file),
])

Published 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.

Cite this entry

@misc{vaitp:cve202329159,
  title        = {{Directory traversal vuln. in Starlette 0.13.5 to 0.26.0 lets remote attackers view web service files}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2023},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2023-29159},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2023-29159/}}
}

Download this entry as JSON Download the whole dataset

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 ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.