File size: 1,165 Bytes
5893134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""Validation helpers for URL path parameters used to construct filesystem paths."""
from __future__ import annotations

import re

from fastapi import HTTPException

# Allow letters, digits, underscores, hyphens, and dots (for e.g. "city_01.v2").
# Disallow anything that could traverse directories: slashes, null bytes, etc.
_SAFE_SEGMENT = re.compile(r"^[A-Za-z0-9_\-\.]+$")
_MAX_SEGMENT_LEN = 128


def validate_path_segment(value: str, field: str) -> str:
    """Raise HTTP 400 if *value* is not a safe filesystem path component."""
    if not value:
        raise HTTPException(status_code=400, detail=f"{field} must not be empty.")
    if len(value) > _MAX_SEGMENT_LEN:
        raise HTTPException(
            status_code=400,
            detail=f"{field} exceeds maximum length of {_MAX_SEGMENT_LEN} characters.",
        )
    if not _SAFE_SEGMENT.match(value):
        raise HTTPException(
            status_code=400,
            detail=(
                f"{field} contains invalid characters. "
                "Only letters, digits, underscores, hyphens, and dots are allowed."
            ),
        )
    return value