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.

6.1 KB
Raw Blame
"""URL identity shared by download writers, readers and research deletion.

No database, service or web imports are needed to load these helpers.
"""

import hashlib
import re

from loguru import logger


def normalize_url_for_hash(url: str) -> str:
    """Canonical normalization for URL-identity hashing (issue #6758 B2).

    Composes the shared :func:`canonical_url_key` (strips tracking query
    params, fragments, default ports, and userinfo; lowercases scheme and
    host) with the historical scheme/``www``/trailing-slash stripping,
    query-param sorting, and full lowercasing, so ``http`` vs ``https``,
    ``www`` variants, tracking-param decor, and param order all hash alike.

    Lowercasing happens BEFORE query-param sorting: ``sort(raw-case).lower()
    != sort(lower(raw))`` whenever mixed-case query keys change relative
    order (e.g. ``?q=x&Lang=fr&UserId=1``), and the tracker writer
    re-normalizes an already-lowercased string — sorting after lowering on
    one path but before lowering on the other yields different orders and
    different hashes (issue #6760 follow-up). Sorting the lowered form is
    deterministic on both paths and keeps the function idempotent.

    The ``canonical_url_key`` import is lazy: this module is imported at
    load time across both the database and research-library stacks. Defer
    loading the utilities/security stack until normalization is needed.
    """
    from .url_utils import canonical_url_key

    try:
        normalized = canonical_url_key(url)
    except Exception:
        logger.warning("canonical_url_key failed, hashing raw URL")
        normalized = url if isinstance(url, str) else ""
    normalized = re.sub(r"^https?://", "", normalized or "")
    normalized = re.sub(r"^www\.", "", normalized)
    normalized = normalized.rstrip("/")
    # Lowercase first so the sort below is on the lowered form (see
    # docstring): otherwise mixed-case query keys sort differently before
    # vs after lowering and a double-normalized writer diverges from a
    # single-normalized reader.
    normalized = normalized.lower()
    if "?" in normalized:
        base, query = normalized.split("?", 1)
        params = sorted(query.split("&"))
        # nosemgrep: semgrep.rules.sql-string-concatenation, reason: URL query-param sorting only; this module builds no SQL
        normalized = f"{base}?{'&'.join(params)}"
    return normalized


def get_url_hash(url: str) -> str:
    """
    Generate a SHA256 hash of a URL.

    Args:
        url: The URL to hash

    Returns:
        The SHA256 hash of the canonical normalized URL (issue #6758 B2)
    """
    return hashlib.sha256(normalize_url_for_hash(url).encode()).hexdigest()


def _legacy_normalize_download_service(url: str) -> str:
    """Pre-B2 (#6758) DownloadService._normalize_url spelling.

    Stripped scheme/``www``/trailing-slash, sorted raw ``&`` query pairs,
    lowercased — but did NOT strip tracking params, fragments, default
    ports, or userinfo. Rows written before B2 still carry this hash.
    """
    if not isinstance(url, str) or not url:
        return ""
    normalized = re.sub(r"^https?://", "", url)
    normalized = re.sub(r"^www\.", "", normalized)
    normalized = normalized.rstrip("/")
    if "?" in normalized:
        base, query = normalized.split("?", 1)
        params = sorted(query.split("&"))
        # nosemgrep: semgrep.rules.sql-string-concatenation, reason: URL query-param sorting only; this module builds no SQL
        normalized = f"{base}?{'&'.join(params)}"
    return normalized.lower()


def _legacy_normalize_library_service(url: str) -> str:
    """Pre-B2 (#6758) LibraryService._get_url_hash spelling.

    Same stripping as the download-service variant but WITHOUT query-param
    sorting. A separate candidate is required for multi-param URLs whose
    order differed.
    """
    if not isinstance(url, str) or not url:
        return ""
    normalized = re.sub(r"^https?://", "", url)
    normalized = re.sub(r"^www\.", "", normalized)
    normalized = normalized.rstrip("/")
    return normalized.lower()


def get_legacy_url_hashes(url: str) -> list[str]:
    """Return pre-B2 (#6758) url_hash candidates for ``url``.

    Three legacy spellings existed in production:
    - raw ``sha256(url.lower())`` (cascade_helper looked up trackers with
      a bare ``get_url_hash`` that performed no stripping);
    - download-service normalized (scheme/``www``/trailing-slash strip +
      query sort);
    - library-service normalized (same strip, no sort).

    Used only as a read-compat fallback — new rows are always written with
    the canonical :func:`get_url_hash`. Callers must try the canonical hash
    first and fall back to these, never write them.
    """
    if not isinstance(url, str) or not url:
        return []
    candidates: list[str] = []
    raw_hash = hashlib.sha256(url.lower().encode()).hexdigest()
    candidates.append(raw_hash)
    dl_hash = hashlib.sha256(
        _legacy_normalize_download_service(url).encode()
    ).hexdigest()
    if dl_hash not in candidates:
        candidates.append(dl_hash)
    lib_hash = hashlib.sha256(
        _legacy_normalize_library_service(url).encode()
    ).hexdigest()
    if lib_hash not in candidates:
        candidates.append(lib_hash)
    return candidates


def get_url_hash_candidates(url: str) -> list[str]:
    """Return ``[canonical, *legacy]`` url_hash candidates for ``url``.

    B2 (#6758) re-keys every URL that ``canonical_url_key`` mutates
    (tracking params, fragments, default ports, userinfo). Pre-upgrade
    ``download_tracker`` rows still carry a legacy hash, so a lookup by
    the canonical hash alone misses them — which previously drove
    duplicate tracker rows and, worse, ``sync_library_with_filesystem``'s
    "No tracker or path" branch deleting the document. Query
    ``DownloadTracker.url_hash.in_(candidates)`` (canonical first) to stay
    compatible with mixed-scheme tables without a data migration.
    """
    canonical = get_url_hash(url)
    out = [canonical]
    for legacy in get_legacy_url_hashes(url):
        if legacy != canonical and legacy not in out:
            out.append(legacy)
    return out