Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.0 KB
Raw Blame
"""Filename sanitization for file uploads.

Wraps werkzeug's secure_filename with additional safety checks.
All file upload endpoints should use sanitize_filename() instead of
importing secure_filename directly.
"""

from __future__ import annotations

import hashlib
from typing import Optional

from werkzeug.utils import secure_filename

# Maximum filename length (including extension)
MAX_FILENAME_LENGTH = 255


class UnsafeFilenameError(ValueError):
    """Raised when a filename cannot be sanitized to a safe value."""


def sanitize_filename(
    filename: Optional[str],
    *,
    allowed_extensions: Optional[set[str]] = None,
    max_length: int = MAX_FILENAME_LENGTH,
) -> str:
    """Sanitize an uploaded filename for safe filesystem storage.

    Args:
        filename: Raw filename from the upload.
        allowed_extensions: Optional set of allowed extensions
            (lowercase, with dot, e.g. {".pdf", ".txt"}).
            If None, all extensions are allowed.
        max_length: Maximum allowed filename length.

    Returns:
        Sanitized filename safe for filesystem use. If the stem (the
        part before the last dot) sanitizes to nothing but still
        contains letters or digits — as happens for non-Latin scripts
        such as CJK or Cyrillic, which werkzeug's secure_filename()
        drops entirely — the stem is replaced with a deterministic
        ``upload-<12 hex chars>`` name derived from a hash of the
        original filename, and the sanitized extension is preserved.

    Raises:
        UnsafeFilenameError: If the filename is empty, becomes empty
            after sanitization, has a disallowed extension,
            ``max_length`` is not positive, or the extension alone
            exceeds ``max_length`` (leaving no room for a stem).
    """
    if not filename:
        raise UnsafeFilenameError("No filename provided")
    if max_length < 1:
        raise UnsafeFilenameError("Maximum filename length must be positive")

    # Strip null bytes before passing to secure_filename
    cleaned = filename.replace("\x00", "")

    # Apply werkzeug's path traversal protection
    safe_name = secure_filename(cleaned)

    raw_stem, separator, raw_extension = cleaned.rpartition(".")
    if not separator:
        raw_stem = cleaned

    if not secure_filename(raw_stem) and any(
        character.isalnum() for character in raw_stem
    ):
        extension = ""
        if separator:
            extension_source = secure_filename(f"x.{raw_extension}")
            extension_index = extension_source.rfind(".")
            if extension_index > 0:
                extension = extension_source[extension_index:]

        # A surrogate can occur in a filesystem-derived or decoded name.
        # It only contributes to the digest; the generated name stays ASCII.
        digest = hashlib.sha256(
            cleaned.encode("utf-8", errors="surrogatepass")
        ).hexdigest()[:12]
        safe_name = f"upload-{digest}{extension}"

    if not safe_name:
        raise UnsafeFilenameError(
            "Filename contains no safe characters after sanitization"
        )

    # Enforce length limit
    if len(safe_name) > max_length:
        # Preserve extension when truncating
        dot_idx = safe_name.rfind(".")
        if dot_idx > 0:
            ext = safe_name[dot_idx:]
            stem_length = max_length - len(ext)
            if stem_length < 1:
                raise UnsafeFilenameError(
                    "Filename extension exceeds maximum length"
                )
            safe_name = safe_name[:stem_length] + ext
        else:
            safe_name = safe_name[:max_length]

    # Validate extension if allowlist provided
    if allowed_extensions is not None:
        dot_idx = safe_name.rfind(".")
        ext = safe_name[dot_idx:].lower() if dot_idx > 0 else ""
        # Normalize allowlist for case-insensitive comparison
        normalized = {e.lower() for e in allowed_extensions}
        if ext not in normalized:
            raise UnsafeFilenameError("File type not allowed")

    return safe_name