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.

3.0 KB
Raw Blame
"""Shared asset lookup for current and legacy static URLs."""

import re
from functools import lru_cache
from pathlib import Path

from fastapi.responses import FileResponse, Response

from ..security.path_validator import PathValidator


_HASHED_FILENAME_RE = re.compile(r"\.[A-Za-z0-9_-]{8,}\.")
_REVALIDATE = "public, max-age=0, must-revalidate"
_IMMUTABLE = "public, max-age=31536000, immutable"


@lru_cache(maxsize=1)
def _combined_theme_css() -> str:
    from .themes import theme_registry

    return theme_registry.get_combined_css()


def static_file_response(path: str) -> FileResponse | Response | None:
    """Serve a contained asset, preferring Vite output over source files.

    Except for CSS composed from bundled themes, the root is always the
    app's ``STATIC_DIR``, read per call. There is deliberately no directory
    parameter, so no caller can point this helper at another tree (e.g. the
    shared research-outputs directory).

    Explicit ``dist/`` URLs and implicit build paths use the same cache
    policy. Invalid paths, failed lookups and symlink escapes are misses;
    callers retain their route's existing 404 response format.
    """
    # A clean wheel does not contain this generated file, and an installed
    # package may be read-only for the service user. Serve it from the bundled
    # theme sources instead of relying on the best-effort startup write.
    if path == "css/themes.css":
        return Response(
            _combined_theme_css(),
            media_type="text/css",
            headers={"Cache-Control": _REVALIDATE},
        )

    from .fastapi_app import STATIC_DIR

    try:
        root = Path(STATIC_DIR).resolve(strict=True)
    except (ValueError, OSError, RuntimeError):
        return None

    dist = root / "dist"
    lookups = []
    if path.startswith("dist/"):
        lookups.append((dist, path[len("dist/") :], True))
    lookups.extend(((dist, path, True), (root, path, False)))

    for base, relative_path, is_dist in lookups:
        try:
            validated = PathValidator.validate_safe_path(
                relative_path, base, allow_absolute=False
            )
            if validated is None:
                continue
            candidate = validated.resolve(strict=True)
            if not candidate.is_relative_to(root) or not candidate.is_file():
                continue
        except (ValueError, OSError, RuntimeError):
            # Includes NUL bytes, overlong names, missing files and loops.
            continue

        cache_control = (
            _IMMUTABLE
            if is_dist and _HASHED_FILENAME_RE.search(relative_path)
            else _REVALIDATE
        )
        return FileResponse(
            str(candidate),
            # Infer the asset type from its public name while opening only
            # the resolved, contained target. Static assets render inline.
            filename=validated.name,
            content_disposition_type="inline",
            headers={"Cache-Control": cache_control},
        )

    return None