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.

37.5 KB
Raw Blame
"""Shared utility functions for the Research Library."""

import ipaddress
import ntpath
import os
import posixpath
import socket
import subprocess
import sys
from datetime import UTC, datetime
from pathlib import Path
from typing import Optional
from urllib.parse import unquote, urlparse

from fastapi.responses import JSONResponse
from loguru import logger
from sqlalchemy import and_, func, or_

from ...security import redact_url_for_log

from ...config.paths import get_library_directory
from ...constants import FILE_PATH_TEXT_ONLY
from ...database.models.research import ResearchResource
from ...database.models.library import (
    Document,
    DocumentCollection,
    DocumentStatus,
)
from ...utilities.download_url import (
    normalize_url_for_hash as normalize_url_for_hash,
    get_url_hash as get_url_hash,
    get_legacy_url_hashes as get_legacy_url_hashes,
    get_url_hash_candidates as get_url_hash_candidates,
)
from ...security.ip_ranges import PRIVATE_IP_RANGES
from ...security.network_utils import is_private_ip
from ...security.path_validator import PathValidator, resolve_allow_missing


def escape_like(text: str) -> str:
    """Escape SQL LIKE/ILIKE wildcards so user input matches literally.

    ``%`` and ``_`` are LIKE wildcards and ``\\`` is the escape character;
    without escaping, a query like ``my_note`` would treat ``_`` as "any
    character" and ``%`` would match anything. Use the result with
    ``.like(pattern, escape="\\\\")`` / ``.ilike(pattern, escape="\\\\")``.

    This is the single source of truth for LIKE-escaping across the library
    and notes query paths (previously copy-pasted in NoteService,
    LibraryService and unified_search_routes).
    """
    return (
        (text or "")
        .replace("\\", "\\\\")
        .replace("%", "\\%")
        .replace("_", "\\_")
    )


def _decode_ipv4_literal(hostname: str) -> Optional[str]:
    """Decode an ASCII IPv4 literal using the platform's ``inet_aton``.

    This handles decimal, hexadecimal, octal and shortened dotted forms
    such as ``2130706433``, ``0x7f000001``, ``0177.0.0.1`` and ``127.1``.
    It performs no DNS lookup and returns None when parsing fails.
    Callers normalize IDNA hostnames before invoking this helper.
    """
    if not hostname:
        return None
    try:
        packed = socket.inet_aton(hostname)
    except OSError:
        return None
    return socket.inet_ntoa(packed)


def _is_blocked_private_host(hostname: str) -> bool:
    """Identify private, local and CGNAT hosts without resolving DNS.

    IDNA normalization covers Unicode numeric labels and dot separators.
    Trailing dots and legacy IPv4 literals are handled here without
    changing the shared ``is_private_ip`` helper — and this normalization
    (including the trailing-dot strip) applies only to this private-host
    check; it never feeds the allowlist match in ``is_downloadable_domain``,
    which stays an exact/dot-suffix comparison against the literal hostname
    (see ``test_trailing_dot_fqdn_is_refused``). The hostname is also
    percent-decoded one pass via ``urllib.parse.unquote`` before any of the
    above, so an encoded numeric IPv4 disguise such as ``%31%32%37.0.0.1``
    (``127.0.0.1``) is normalized to the same candidate a plain literal
    would produce, rather than sailing past every subsequent check as an
    opaque, non-matching hostname string. Each parsed candidate — and, for
    the IPv4-mapped form, its embedded IPv4 — is checked against the shared
    ``security.ip_ranges.PRIVATE_IP_RANGES`` recognition table (the same
    table the fetch chokepoint uses), so every wrapped IPv6 spelling of a
    private destination that the table knows (6to4, NAT64, IPv4-compatible,
    IPv4-translated, ...) is rejected here too, not just the CGNAT range
    behind the ``ipv4_mapped`` unwrap.
    """
    normalized = unquote(hostname).rstrip(".")
    try:
        normalized = normalized.encode("idna").decode("ascii").rstrip(".")
    except UnicodeError:
        pass
    candidates = [normalized]
    decoded = _decode_ipv4_literal(candidates[0])
    if decoded is not None:
        candidates.append(decoded)

    for candidate in candidates:
        if is_private_ip(candidate):
            return True
        try:
            address = ipaddress.ip_address(candidate)
        except ValueError:
            continue
        addresses = [address]
        if isinstance(address, ipaddress.IPv6Address):
            mapped = address.ipv4_mapped
            if mapped is not None:
                addresses.append(mapped)
        if any(
            addr in network
            for addr in addresses
            for network in PRIVATE_IP_RANGES
        ):
            return True
    return False


def is_downloadable_domain(url: str) -> bool:
    """Identify likely academic resources and direct PDF links.

    This is a relevance filter. Private IP literals, local names and CGNAT
    are rejected before the academic-domain, PDF and PubMed-path heuristics.
    Literal normalization (IDNA, trailing dots, legacy/percent-encoded IPv4,
    IPv4-mapped IPv6) is best-effort, does not resolve DNS, and applies only
    to that private-host rejection step — the allowlist match below is an
    exact/dot-suffix comparison against the literal (non-normalized)
    hostname, so a public hostname that resolves to a private address can
    still pass, and a trailing-dot FQDN of an allowlisted domain is still
    refused (see ``test_trailing_dot_fqdn_is_refused``).

    Fetches must independently use validated transports such as
    ``security.safe_requests.SafeSession`` or ``safe_get``. Those transports
    use ``security.ssrf_validator`` for address and redirect validation;
    a True result here is never permission to bypass those checks.
    """
    try:
        if not url:
            return False

        parsed = urlparse(url.lower())
        hostname = parsed.hostname or ""
        path = parsed.path or ""
        query = parsed.query or ""

        # Reject recognized private/local literals before any heuristic
        # can accept them. DNS and transport validation happen at fetch time.
        if _is_blocked_private_host(hostname):
            return False

        # Check for direct PDF files
        if path.endswith(".pdf") or ".pdf?" in url.lower():
            return True

        # List of downloadable academic domains
        downloadable_domains = [
            "arxiv.org",
            "biorxiv.org",
            "medrxiv.org",
            "ncbi.nlm.nih.gov",
            "pubmed.ncbi.nlm.nih.gov",
            "pubmed.gov",
            "pubmedcentral.nih.gov",
            "europepmc.org",
            "semanticscholar.org",
            "researchgate.net",
            "academia.edu",
            "sciencedirect.com",
            "springer.com",
            "nature.com",
            "wiley.com",
            "ieee.org",
            "acm.org",
            "plos.org",
            "frontiersin.org",
            "mdpi.com",
            "acs.org",
            "rsc.org",
            "tandfonline.com",
            "sagepub.com",
            "oxford.com",
            "cambridge.org",
            "bmj.com",
            "nejm.org",
            "thelancet.com",
            "jamanetwork.com",
            "annals.org",
            "ahajournals.org",
            "cell.com",
            "science.org",
            "pnas.org",
            "elifesciences.org",
            "embopress.org",
            "journals.asm.org",
            "microbiologyresearch.org",
            "jvi.asm.org",
            "genome.cshlp.org",
            "genetics.org",
            "g3journal.org",
            "plantphysiol.org",
            "plantcell.org",
            "aspb.org",
            "bioone.org",
            "company-of-biologists.org",
            "biologists.org",
            "jeb.biologists.org",
            "dmm.biologists.org",
            "bio.biologists.org",
            "doi.org",
            "ssrn.com",
            "openreview.net",
        ]

        # Check if hostname matches any downloadable domain
        for domain in downloadable_domains:
            if hostname == domain or hostname.endswith("." + domain):
                return True

        # Preserve the host-independent PubMed-path heuristic. Real PubMed
        # hosts match the allowlist above; hostname substrings do not qualify.
        if "/pubmed/" in path:
            return True

        # Check for PDF in path or query parameters
        if "/pdf/" in path or "type=pdf" in query or "format=pdf" in query:
            return True

        return False

    except Exception:
        logger.warning(f"Error parsing URL {redact_url_for_log(url)}")
        return False


def is_downloadable_url(url: str) -> bool:
    """Check if a URL is downloadable (academic domain or direct PDF link).

    This is the single source of truth for downloadability checks.
    Combines domain checking with PDF extension/path detection.

    Args:
        url: The URL to check

    Returns:
        True if the URL is from a downloadable academic domain or is a direct PDF link
    """
    return is_downloadable_domain(url)


def get_document_for_resource(session, resource):
    """Get Document for a ResearchResource.

    Checks resource.document_id first (library resources point directly
    to existing Documents), falls back to Document.resource_id lookup
    (web downloads create Documents with resource_id set).
    """
    if resource.document_id:
        return (
            session.query(Document).filter_by(id=resource.document_id).first()
        )
    return session.query(Document).filter_by(resource_id=resource.id).first()


def resource_document_clause():
    """Join resources to their canonical document, honoring explicit links.

    Mirrors ``get_document_for_resource``: retained documents can still
    name a resource as their owner after that resource links elsewhere.
    """
    return or_(
        ResearchResource.document_id == Document.id,
        and_(
            ResearchResource.document_id.is_(None),
            ResearchResource.id == Document.resource_id,
        ),
    )


def document_awaits_pdf(document) -> bool:
    """Whether a completed research download still needs its PDF.

    "Sync library" keeps a downloaded document whose PDF file was removed
    from disk if the document still has its extracted text and its own
    research resource (``resource_id``). It labels the document a PDF and
    stores it the way text-only documents are stored (``file_type`` "pdf",
    ``storage_mode`` "none", ``file_path`` ``FILE_PATH_TEXT_ONLY``), resets
    the tracker that identifies the document (see
    ``find_tracker_for_document``) if it names that file or no file, and
    reports the file missing, so every document it reports that way matches
    here
    (see ``LibraryService._keep_text_report_missing_pdf``). Everywhere else
    a completed document counts as downloaded, so ``download_resource`` and
    the PDF queueing paths (``queue_research_downloads`` and the "Download
    All New" route, through ``document_awaits_pdf_clause``) ask this, for
    the document's own resource and for the resources linked to it by
    content (``ResearchResource.document_id``), and download the PDF again
    into the same document. One download refills it, so the queueing paths
    queue one of those resources (see ``DownloadService.refill_rank``).
    Once the document holds its PDF again, ``download_resource`` reports
    the document's own resource as already downloaded, and for a linked
    one whose URL the egress policy allows, ``DownloadService._download_pdf``
    reuses that stored PDF instead of fetching it. The
    Download Manager asks too (``LibraryService``'s
    ``get_research_list_with_stats``,
    ``get_download_manager_summary_stats`` and ``get_pdf_previews_batch``):
    such a document counts as available to download, not as downloaded,
    and shows no "Downloaded" badge. So do the library routes
    ``get_research_sources`` and ``check_downloads``, which report it as
    not downloaded, and ``download_source``, which downloads it again
    rather than answer "Already downloaded" (see
    ``document_counts_as_downloaded``). Text extraction does not ask: such
    a document already holds its text.

    A document whose resource is gone (``resource_id`` NULL) never matches,
    so nothing asks for its PDF. The sync stores such a document text-only
    too, without reporting it, so that a later download of the same PDF
    bytes for another resource stores the PDF in it (see
    ``LibraryService._clear_missing_pdf``).

    This checks ``file_type`` "pdf", not only that storage marker. Zotero
    imports and uploads write the same marker for PDFs and differ only in
    having no ``resource_id``. A text extraction (``file_type`` "text") has
    the same ``file_path`` and differs only in the ``"database"`` default of
    ``storage_mode``, so a writer that set it to "none", the value for a
    document without a stored PDF, would make every extracted text ask for
    a PDF. The sync can set the label itself because what it found missing
    was a PDF: the file paths of documents and trackers come only from PDF
    storage (``PDFStorageManager.save_pdf``), and the sync converts a document
    only for a missing file that its own ``file_path`` names or, for a
    document without a path of its own, that the tracker identifying it
    names. A tracker counts for a document only when its content hash or
    explicit document link identifies the document, and its ``"database"``
    path names no file and so leads to no conversion. So a document it
    converts with another label, such as a failed-download placeholder that
    a PDF download completed before ``_download_pdf`` labelled such
    documents "pdf", is picked up too.

    The one other writer of that shape is a download whose PDF the PDF
    storage size limit refuses (``_keep_refused_pdf_text_only`` in the
    download service): its document keeps the extracted text and awaits
    its PDF too, with the refused size in ``file_size``, and
    ``download_resource`` does not fetch it again while the limit still
    refuses that size (``DownloadService._pdf_refused_at_current_limit``).
    Otherwise a "none"-mode download keeps ``file_path=None``, "Remove PDF
    to save space" writes ``blob_deleted``, and a text extraction is
    ``file_type`` "text". A download into the document in storage mode
    "none", which stores no PDF, clears the mark, see
    ``DownloadService._download_pdf``.
    """
    return (
        document.status == DocumentStatus.COMPLETED
        and document.file_type == "pdf"
        and document.storage_mode == "none"
        and document.file_path == FILE_PATH_TEXT_ONLY
        and document.resource_id is not None
    )


def document_awaits_pdf_clause():
    """:func:`document_awaits_pdf` as a SQL condition on ``Document``.

    The nullable columns are compared through ``coalesce`` and ``status``
    is NOT NULL, so on a ``Document`` row the condition is never NULL and
    its negation is true for every document the function rejects.
    """
    return and_(
        Document.status == DocumentStatus.COMPLETED,
        func.coalesce(Document.file_type, "") == "pdf",
        func.coalesce(Document.storage_mode, "") == "none",
        func.coalesce(Document.file_path, "") == FILE_PATH_TEXT_ONLY,
        Document.resource_id.isnot(None),
    )


def document_counts_as_downloaded(document) -> bool:
    """Whether a loaded ``Document`` counts as downloaded.

    A completed document counts, unless it awaits its PDF (see
    ``document_awaits_pdf``), which the Download Manager offers for
    download again. Its counts apply the same rule in SQL
    (``LibraryService``'s ``_counts_as_downloaded``), and the source
    previews under each research (``get_pdf_previews_batch``) apply this
    function. So do the library routes that tell whether a research's
    source is downloaded (``get_research_sources`` and ``check_downloads``)
    and ``download_source``, which answers "Already downloaded" only for a
    document that counts.

    ``status`` loads as a ``DocumentStatus`` member. ``DocumentStatus`` is
    a plain ``enum.Enum``, so a member never equals its string value
    (``DocumentStatus.COMPLETED == "completed"`` is False), and the status
    is compared with the member.
    """
    return document.status == DocumentStatus.COMPLETED and not (
        document_awaits_pdf(document)
    )


# When this server process started. This module is imported while the app
# starts, before any download can claim a queue row. See
# download_claim_is_live.
_PROCESS_STARTED_AT = datetime.now(UTC)


def download_claim_is_live(claimed_at: Optional[datetime]) -> bool:
    """Whether a PROCESSING download-queue row is still being downloaded.

    ``claimed_at`` is the row's ``last_attempt_at``, which the download
    that claims it sets (``_claim_download_queue_item`` in the library
    routes). A download finishes or releases the row it claimed before it
    ends, and it cannot outlive this server process, which serves with one
    worker (see ``web/app.py``). So a row claimed before this process
    started was cut off by a restart, as is one claimed before claims
    recorded their time (``claimed_at`` None): nothing finishes or
    releases it, so it does not stand for a download in progress. A row
    whose release failed in this process (which is logged) still counts
    as in progress until the next restart.

    Both times are wall-clock readings, so a clock that goes back defeats
    the rule. If this process started with its clock behind the time an
    earlier process recorded for a claim, as on a device without a
    real-time clock that starts with a stale time and has it corrected
    later, that claim counts as in progress until the next restart, and no
    other resource of its document refills the document until then. A
    clock set back while this process runs can make a claim it made count
    as cut off, so that another resource of the document can be queued
    while that download runs. The queue row records nothing else that
    tells the process that claimed it.
    """
    if claimed_at is None:
        return False
    if claimed_at.tzinfo is None:
        claimed_at = claimed_at.replace(tzinfo=UTC)
    return claimed_at >= _PROCESS_STARTED_AT


def find_tracker_by_url(session, url: str):
    """Find a ``DownloadTracker`` for ``url`` across hash schemes.

    Tries the canonical B2 hash first, then the pre-B2 legacy spellings
    (see :func:`get_legacy_url_hashes`). Returns ``None`` when no row
    matches. Read-compat only — callers creating a tracker must still
    write the canonical hash.

    Implemented as sequential ``filter_by`` lookups (not a single
    ``IN``) so existing ``MagicMock`` tests that force
    ``filter_by(...).first() -> None`` keep forcing a miss on every
    candidate.
    """
    from ...database.models.download_tracker import DownloadTracker

    candidates = get_url_hash_candidates(url)
    for candidate in candidates:
        tracker = (
            session.query(DownloadTracker).filter_by(url_hash=candidate).first()
        )
        if tracker is not None:
            return tracker
    return None


def find_tracker_for_document(session, document: Document):
    """Find a URL tracker whose content identity belongs to this document.

    URL equivalence alone is insufficient for destructive operations: legacy
    trackers can contain different PDF versions at the same canonical URL.
    Require a matching content hash or explicit document link, reject
    contradictory identity fields, and keep looking through legacy hashes
    when the canonical tracker belongs to another document.
    """
    from ...database.models.download_tracker import DownloadTracker

    if not document.original_url:
        return None
    for candidate in get_url_hash_candidates(document.original_url):
        tracker = (
            session.query(DownloadTracker).filter_by(url_hash=candidate).first()
        )
        if tracker is None:
            continue
        if tracker.library_document_id and (
            tracker.library_document_id != document.id
        ):
            continue
        if tracker.file_hash and tracker.file_hash != document.document_hash:
            continue
        if tracker.library_document_id == document.id or (
            tracker.file_hash and tracker.file_hash == document.document_hash
        ):
            return tracker
    return None


def ensure_in_collection(
    session, document_id: str, collection_id: str
) -> "DocumentCollection":
    """Get or create a DocumentCollection link between a document and a collection.

    Args:
        session: SQLAlchemy session
        document_id: UUID of the document
        collection_id: UUID of the collection

    Returns:
        The existing or newly created DocumentCollection row
    """
    existing = (
        session.query(DocumentCollection)
        .filter_by(document_id=document_id, collection_id=collection_id)
        .first()
    )
    if existing:
        return existing

    doc_collection = DocumentCollection(
        document_id=document_id,
        collection_id=collection_id,
        indexed=False,
    )
    session.add(doc_collection)
    return doc_collection


def _reject_unsafe_username_component(username: str) -> None:
    """Fail closed unless ``username`` is a single, safe path component.

    ``apply_user_subdir`` joins the username directly into a filesystem path,
    so any value that could let a downstream ``.resolve()`` escape the
    library base would be an arbitrary-directory read/write primitive. This
    mirrors registration's *exact* predicate
    (``username.replace('_','').replace('-','').isalnum()``) so the two
    checks can never diverge: any Unicode letters/digits registration accepts
    (e.g. accented Latin, Cyrillic, CJK) are accepted here too, alongside
    plain ``[A-Za-z0-9_-]``. ``str.isalnum()`` still rejects every
    path-escape vector — separators (``/``, ``\\``), ``..``, ``:``, dots,
    spaces, and control characters are never alphanumeric — so this remains a
    fail-closed traversal guard; it does not attempt to additionally block
    reserved device names or homoglyphs, since neither is meaningfully closed
    by matching registration's charset. We reject rather than sanitize/hash
    so a valid account keeps a human-readable per-user directory, and reject
    rather than fall back to the shared base (which would risk cross-user
    co-mingling).
    """
    stripped = username.replace("_", "").replace("-", "")
    if not stripped or not stripped.isalnum():
        raise ValueError("Unsafe username for per-user library path")


def apply_user_subdir(
    base_path, username: Optional[str], shared_library: bool = False
) -> Path:
    """Return the per-user library directory under ``base_path``.

    Single source of truth for the per-user library layout (issue #5521):
    downloaded PDFs live under ``<base>/<username>`` so two users' per-user
    autoincrement ``resource_id`` filenames (e.g. ``pdfs/5.pdf``) can no
    longer collide in one shared directory. In shared-library mode — or when
    no username is available — the base directory itself is returned.

    The ``username`` is validated as a single safe path component before it is
    joined (see :func:`_reject_unsafe_username_component`); an unsafe value
    raises ``ValueError`` rather than escaping the base or silently sharing it.

    Unlike :func:`get_library_storage_path`, this works from an
    already-resolved base path plus an explicit ``shared_library`` flag, so
    background threads (the scheduler download path) and snapshot-based
    callers (collection/library search engines) get the same layout without
    depending on request-context settings resolution.
    """
    # Expand $VARs (parity with the write path in DownloadService.__init__);
    # the read-side resolvers previously only expanduser()'d, so a storage_path
    # containing an env-var token resolved to a different, non-existent path on
    # read than on write (files became unfindable, tracker state corrupted).
    base = Path(os.path.expandvars(str(base_path))).expanduser().resolve()
    # ``shared_library`` removes the per-user directory boundary. Its flag is
    # user-editable, so shared mode only takes effect when an operator has
    # explicitly enabled it via the environment. The storage path itself is
    # operator-only; a path saved before that policy remains in place.
    if (shared_library and _shared_library_allowed()) or not username:
        return base
    _reject_unsafe_username_component(username)
    return base / username


def _shared_library_allowed() -> bool:
    """Whether the operator enabled shared-library mode (env-only gate).

    Environment-only, mirroring ``filesystem_pdf_storage_allowed`` and
    ``policy.allow_unprotected_egress``: shared mode drops the per-user
    directory isolation, so it cannot be turned on through the user-writable
    settings API — only via
    ``LDR_RESEARCH_LIBRARY_ALLOW_SHARED_LIBRARY=true``.
    """
    from ...settings.env_registry import get_env_setting

    return bool(get_env_setting("research_library.allow_shared_library", False))


def get_library_storage_path(username: str) -> Path:
    """
    Get the storage path for a user's library.

    Uses the settings system which respects environment variable overrides:
    - research_library.storage_path: Base path for library storage
    - research_library.shared_library: If true, all users share the same directory

    Args:
        username: The username

    Returns:
        Path to the library storage directory
    """
    from ...utilities.db_utils import get_settings_manager

    settings = get_settings_manager()

    # Get the base path from settings (uses centralized path, respects LDR_DATA_DIR)
    base_path = settings.get_setting(
        "research_library.storage_path",
        str(get_library_directory()),
    )

    # Check if shared library mode is enabled
    shared_library = settings.get_setting(
        "research_library.shared_library", False
    )

    storage_path = apply_user_subdir(base_path, username, shared_library)

    # Lazy import to avoid import cycles (this module is imported early by
    # several bootstrap paths).
    from ...security.directory_creation import create_directory

    # ``apply_user_subdir`` has already expanded/resolved the base path and
    # validated the per-user component; route the actual mkdir through the
    # audited chokepoint, keeping containment to the resolved base so a
    # symlinked parent that escapes it is still rejected.
    resolved_base = (
        Path(os.path.expandvars(str(base_path))).expanduser().resolve()
    )
    create_directory(
        storage_path,
        context="per-user library storage",
        root=resolved_base,
    )
    return storage_path


def open_file_location(file_path: str) -> bool:
    """
    Open the file location in the system file manager.

    Args:
        file_path: Path to the file

    Returns:
        True if successful, False otherwise
    """
    try:
        # Validate path is safe (blocks system dirs, path traversal)
        validated = PathValidator.validate_local_filesystem_path(file_path)
        folder = str(validated.parent)
        if sys.platform == "win32":
            os.startfile(folder)
        elif sys.platform == "darwin":  # macOS
            result = subprocess.run(
                ["open", folder],
                capture_output=True,
                text=True,
                shell=False,
                timeout=10,
            )
            if result.returncode != 0:
                logger.error(f"Failed to open folder on macOS: {result.stderr}")
                return False
        else:  # Linux
            result = subprocess.run(
                ["xdg-open", folder],
                capture_output=True,
                text=True,
                shell=False,
                timeout=10,
            )
            if result.returncode != 0:
                logger.error(f"Failed to open folder on Linux: {result.stderr}")
                return False
        return True
    except Exception:
        logger.exception("Failed to open file location")
        return False


def get_absolute_library_path(
    relative_path: str, username: str
) -> Optional[Path]:
    """
    Get the absolute path from a relative library path.

    Uses PathValidator to prevent path traversal attacks, with the same
    trusted-storage-leaf carve-out as every other library resolver (see
    ``_resolve_within_root``).

    Args:
        relative_path: The relative path from library root
        username: The username

    Returns:
        The absolute path, or None if the path is unsafe
    """
    library_root = get_library_storage_path(username)
    return _resolve_within_root(relative_path, library_root)


# The one sanctioned operator storage layout: the hard-coded "pdfs"
# leaf under the library root is commonly a symlink onto a larger
# disk (pinned by test_saves_to_symlinked_pdfs_directory). Containment
# for stored paths under this leaf is enforced against the *resolved*
# pdfs directory; every other stored path is confined to the root
# itself (GHSA-gv87-qcw7-gjr8). This is the single shared
# implementation of that rule: PDFStorageManager._safe_path_in_root
# and every settings-based resolver below resolve through it, so the
# write side and the read/delete side can never drift apart.
_TRUSTED_STORAGE_LEAF = "pdfs"

# The path module whose separator stored library paths are written with.
# PDFStorageManager stores ``str(path.relative_to(library_root))``, so a
# stored path separates its segments with "/" on POSIX, where a backslash
# is an ordinary filename character, and with a backslash on Windows.
_STORED_PATH_MODULE = ntpath if os.name == "nt" else posixpath


def _containment_base(
    relative_path: str, library_root: Path
) -> tuple[Path, str]:
    """Return ``(base, remainder)`` for checking a stored library path.

    ``base`` is the directory ``relative_path`` must stay inside and
    ``remainder`` is the part of it validated against ``base``. A path
    whose first segment is the trusted ``pdfs`` leaf
    (``_TRUSTED_STORAGE_LEAF``) is checked against ``library_root / "pdfs"``
    when that leaf resolves to a directory; containment then runs against
    the directory the leaf resolves to. The leaf test reads the path as
    ``validate_safe_path`` does (surrounding whitespace stripped, then
    ``posixpath.normpath``), so ``pdfs//x.pdf``, `` pdfs/x.pdf`` and
    ``pdfs/../pdfs/x.pdf`` count as ``pdfs/x.pdf``. On Windows a backslash
    is first read as "/", because the store writes ``pdfs\\x.pdf`` there,
    so the remainder returned with the pdfs base uses "/" only, the one
    separator werkzeug's ``safe_join`` accepts on Windows. Every other
    path, and any value that is not a string, is checked unchanged against
    ``library_root`` itself. ``library_root`` must already be a ``Path``.
    """
    if isinstance(relative_path, str):
        normalized = relative_path.strip()
        if _STORED_PATH_MODULE.sep != "/":
            normalized = normalized.replace(_STORED_PATH_MODULE.sep, "/")
        normalized = posixpath.normpath(normalized)
        first, sep, rest = normalized.partition("/")
        if first == _TRUSTED_STORAGE_LEAF and sep and rest:
            trusted = library_root / first
            try:
                resolved_trusted = resolve_allow_missing(trusted)
            except OSError:
                resolved_trusted = None
            if resolved_trusted is not None and resolved_trusted.is_dir():
                return trusted, rest
    return library_root, relative_path


def _resolve_within_root(
    relative_path: str, library_root: Path
) -> Optional[Path]:
    """Validate ``relative_path`` inside ``library_root`` (traversal + symlink
    safe). Returns the absolute path, or ``None`` if unsafe.

    For a stored path whose FIRST segment is the sanctioned ``pdfs``
    leaf (``_TRUSTED_STORAGE_LEAF``), containment is checked against
    the RESOLVED pdfs target directory rather than the library root —
    exactly as PDFStorageManager does on the write side. No other path
    shape is loosened: symlink escapes anywhere else still fail closed,
    and a second-level escape planted inside the resolved pdfs target
    is caught by the same realpath containment. ``_containment_base``
    decides which directory applies, and states the accepted spellings.
    """
    base, remainder = _containment_base(relative_path, Path(library_root))
    try:
        # Use PathValidator to safely join and validate the path
        safe_path = PathValidator.validate_safe_path(remainder, str(base))
        result = Path(safe_path)
        # Block symbolic links to prevent symlink-based escapes
        if result.is_symlink():
            logger.warning(f"Symlink blocked: {relative_path}")
            return None
        return result
    except ValueError:
        logger.warning(f"Path traversal blocked: {relative_path}")
        return None


def get_absolute_path_from_settings(
    relative_path: str,
    username: Optional[str] = None,
    *,
    settings_manager=None,
) -> Optional[Path]:
    """Resolve a path inside the user's configured library root.

    ``settings_manager`` supplies an already-scoped manager for background
    threads; otherwise use the ambient manager. The returned path is checked
    for traversal and symlink escapes, even when the file is missing.

    Missing files never fall back to the old shared directory. Legacy file
    names alone cannot establish ownership; recover those files explicitly
    after verifying their owner. The retired legacy-read environment flag has
    no effect. Operator-enabled shared-library mode remains an explicit root
    selection, not a fallback from a missing per-user file.

    A nonempty path requires a username for both reads and deletes. An empty
    path may still return the configured base root without a user context for
    trusted cleanup callers that need a containment boundary.

    Returns the absolute path, or None if it is unsafe. Raises ValueError for
    a nonempty path without a username.
    """
    if relative_path and not username:
        raise ValueError(
            "Refusing to resolve a library file without a user context"
        )

    if settings_manager is None:
        from ...utilities.db_utils import get_settings_manager

        settings = get_settings_manager()
    else:
        settings = settings_manager
    base_path = (
        Path(
            os.path.expandvars(
                settings.get_setting(
                    "research_library.storage_path",
                    str(get_library_directory()),
                )
            )
        )
        .expanduser()
        .resolve()
    )
    shared_library = settings.get_setting(
        "research_library.shared_library", False
    )
    per_user_root = apply_user_subdir(base_path, username, shared_library)

    if not relative_path:
        # An empty path asks for the (per-user) library root itself.
        return per_user_root

    return _resolve_within_root(relative_path, per_user_root)


def get_containment_root_from_settings(
    relative_path: str,
    username: Optional[str] = None,
    *,
    settings_manager=None,
) -> Optional[Path]:
    """Return the resolved directory a stored library path is confined to.

    This is the directory ``get_absolute_path_from_settings`` validates
    ``relative_path`` against for the same ``username``: ``<root>/pdfs``
    for a stored ``pdfs/...`` path when that leaf resolves to a directory
    (including the sanctioned layout that symlinks it onto a larger disk),
    and ``<root>`` itself for every other path, where ``<root>`` is what
    ``get_absolute_path_from_settings("", username)`` returns. The bare
    library root cannot serve as the unlink boundary: in the symlinked
    layout every PDF's real location is on the other disk, so the re-check
    would refuse the delete and leave the file orphaned there.

    The directory is returned fully resolved, as it is at the time of the
    call. Destructive callers call this BEFORE resolving the file, and pass
    the result as the ``allowed_root`` of
    ``CascadeHelper.delete_filesystem_file`` with
    ``allowed_root_is_resolved=True``. The unlink re-check then compares
    the file's real location with this pinned boundary instead of resolving
    the boundary again. Otherwise a ``pdfs`` directory swapped for a symlink
    between validation and delete would move the boundary along with the
    file (the #5481 swap).

    Returns None if the library root lookup returns None, or if the
    directory cannot be resolved safely.
    """
    library_root = get_absolute_path_from_settings(
        "", username, settings_manager=settings_manager
    )
    if library_root is None:
        return None
    base = _containment_base(relative_path, library_root)[0]
    try:
        return resolve_allow_missing(base)
    except OSError:
        return None


def handle_api_error(operation: str, error: Exception, status_code: int = 500):
    """
    Handle API errors consistently - log internally, return generic message to user.

    This prevents information exposure by logging full error details internally
    while returning a generic message to the user.

    Args:
        operation: Description of the operation that failed (for logging)
        error: The exception that occurred
        status_code: HTTP status code to return (default: 500)

    Returns:
        Flask JSON response tuple (response, status_code)
    """
    # Log the full error internally with stack trace
    logger.exception(f"Error during {operation}")

    # Return generic message to user (no internal details exposed)
    return JSONResponse(
        {
            "success": False,
            "error": "An internal error occurred. Please try again or contact support.",
        },
        status_code=status_code,
    )