~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""URL utility functions for the local deep research application."""
from functools import lru_cache
from typing import Optional
import re
from urllib.parse import SplitResult, unquote, urlsplit, urlunsplit
from loguru import logger
from .chunk_anchor import (
LIBRARY_ALIAS_HOST,
LIBRARY_ROUTE_PREFIXES,
LIBRARY_ROUTE_SUFFIX_ALTERNATION,
MAX_CHUNK_INDEX,
is_safe_document_id,
)
from ..security import redact_url_for_log, validate_url
from ..security.network_utils import is_private_ip
# Re-export for backwards compatibility
__all__ = [
"normalize_url",
"endpoint_origin",
"same_endpoint_origin",
"is_private_ip",
"canonical_url_key",
"content_web_cache_key",
"library_display_url",
"is_safe_custom_llm_endpoint",
]
# Tracking query parameter keys (matched lowercased).
_TRACKING_PARAMS = frozenset(
{
"fbclid",
"gclid",
"msclkid",
"yclid",
"dclid",
"gad_source",
"mc_eid",
"mc_cid",
"ref_src",
"igshid",
"_ga",
"_gl",
}
)
# Tracking param name prefixes (matched lowercased).
_TRACKING_PREFIXES = ("utm_",)
def normalize_url(raw_url: str) -> str:
"""
Normalize a URL to ensure it has a proper scheme and format.
Args:
raw_url: The raw URL string to normalize
Returns:
A properly formatted URL string
Examples:
>>> normalize_url("localhost:11434")
'http://localhost:11434'
>>> normalize_url("https://example.com:11434")
'https://example.com:11434'
>>> normalize_url("http:example.com")
'http://example.com'
"""
if not raw_url:
raise ValueError("URL cannot be empty")
# Clean up the URL
raw_url = raw_url.strip()
# First check if the URL already has a proper scheme
if raw_url.startswith(("http://", "https://")):
return raw_url
# Handle case where URL is malformed like "http:hostname" (missing //)
if raw_url.startswith(("http:", "https:")) and not raw_url.startswith(
("http://", "https://")
):
scheme = raw_url.split(":", 1)[0]
rest = raw_url.split(":", 1)[1]
return f"{scheme}://{rest}"
# Handle URLs that start with //
if raw_url.startswith("//"):
# Remove the // and process
raw_url = raw_url[2:]
# At this point, we should have hostname:port or just hostname
# Determine if this is localhost or an external host
hostname = raw_url.split(":")[0].split("/")[0]
# Handle IPv6 addresses in brackets
if hostname.startswith("[") and "]" in raw_url:
# Extract the IPv6 address including brackets
hostname = raw_url.split("]")[0] + "]"
# Use http for local/private addresses, https for external hosts
scheme = "http" if is_private_ip(hostname) else "https"
return f"{scheme}://{raw_url}"
def endpoint_origin(raw_url) -> tuple[str, str, int] | None:
"""Return the ``(scheme, host, port)`` an LLM client built from ``raw_url`` contacts.
The URL is first passed through :func:`normalize_url`, exactly as the
OpenAI-compatible endpoint provider does before building its client,
so the origin describes the real destination rather than the text the
user typed. Scheme and host are lowercased, a trailing dot on the host
is dropped, a missing port takes the scheme default (80/443), and path,
query, fragment and userinfo are ignored.
Returns ``None`` for anything that is not a cleanly parseable http(s)
URL with a host, so callers comparing origins fail closed.
"""
if not isinstance(raw_url, str) or not raw_url.strip():
return None
try:
normalized = normalize_url(raw_url)
except ValueError:
return None
# Function-local import: the egress validators import this package's
# settings helpers at module load.
from ..security.egress.validators import _engine_url_origin
return _engine_url_origin(normalized)
def same_endpoint_origin(url_a, url_b) -> bool:
"""True only when both URLs parse and resolve to the same origin."""
origin_a = endpoint_origin(url_a)
return origin_a is not None and origin_a == endpoint_origin(url_b)
# Internal library-document routes emitted as citation URLs by the RAG
# search engines (see ``LibraryRAGSearchEngine._get_document_url``). These
# are the only scheme-less URLs whose shape ``canonical_url_key`` knows
# well enough to canonicalize; everything else falls back to the raw
# string. Imported from ``chunk_anchor`` so the producer side (which
# decides what counts as a library hit) and this consumer side cannot
# disagree about the prefix set.
_LIBRARY_ROUTE_PREFIXES = LIBRARY_ROUTE_PREFIXES
# The absolute alias the agent sometimes emits for a library document; the
# fetch resolver accepts it (see ``library_resolver._parse_library_reference``)
# and hands the ORIGINAL string back, which is then registered as a citation
# link. It must key and display as the relative route it denotes, or one
# document occupies two bibliography entries.
_LIBRARY_ALIAS_SCHEME = "https"
_LIBRARY_ALIAS_HOST = LIBRARY_ALIAS_HOST
# Mirrors ``library_resolver._LIBRARY_PATH_RE``: a document id plus at most
# one known view. Anything deeper is not a library route and must not be
# rewritten into one.
_LIBRARY_ALIAS_PATH_RE = re.compile(
rf"^/(?P<doc_id>[^/?#]+)(?:/(?P<suffix>{LIBRARY_ROUTE_SUFFIX_ALTERNATION}))?/?$"
)
# The same shape for the RELATIVE routes, including the ``/lib/`` spelling.
# Both spellings denote one document (``library_resolver._LIBRARY_PATH_RE``
# accepts either), so both normalize to the ``/library/`` form: keying them
# separately fans a document across two bibliography entries, and only
# ``/library/document/<id>`` is a registered Flask route — a ``/lib/...``
# link renders dead.
_LIBRARY_ROUTE_PATH_RE = re.compile(
r"^/(?:library|lib)/document/(?P<doc_id>[^/?#]+)"
rf"(?:/(?P<suffix>{LIBRARY_ROUTE_SUFFIX_ALTERNATION}))?/?$"
)
def _is_library_route(path: str) -> bool:
return path.startswith(_LIBRARY_ROUTE_PREFIXES)
def _has_control_chars(value: str) -> bool:
"""True if *value* carries a C0/DEL character.
These are rendered verbatim into the ``## Sources`` block, where an
embedded newline forges extra lines. The library-route branches build
their result by string-splitting rather than via ``urlunsplit``, so —
unlike the absolute-URL branch — they cannot drop such a character on
their own and must reject it explicitly.
"""
return any(ord(ch) < 0x20 or ord(ch) == 0x7F for ch in value)
def _normalize_library_alias(url: str) -> str | None:
"""Map ``https://library.document/<id>[/pdf|/chunks]`` to its relative route.
Returns ``None`` for anything else. The accepted shape is deliberately
narrow — a document id followed by at most the ``pdf`` or ``chunks``
view, mirroring ``library_resolver._LIBRARY_PATH_RE`` — because the
result is keyed AND displayed as an internal route. A permissive
rewrite lets a crafted alias merge into a real document's bibliography
entry, inherit its title, and replace its link: pasting an unvalidated
path after ``/library/document`` turns
``https://library.document/<id>/../../../admin/x#chunk-1`` into a
same-origin link to ``/admin/x`` rendered under the real document's
name.
Two deliberate differences from the resolver, both narrowing or
identity-preserving rather than widening:
* Only the resolver's bounded ``document_chunk_id`` query is accepted
on a chunk URL; arbitrary server-meaningful state still disqualifies.
* Host comparison uses ``hostname``/``port``, so ``LIBRARY.DOCUMENT``,
``:443`` and userinfo forms name the same document rather than
fanning out into separate bibliography entries. The resolver rejects
those for fetching; they are still the same source for citation
purposes.
The fragment is carried over — it is the chunk anchor.
"""
# Cheap reject first: this runs once per citation inside
# ``format_links_to_markdown``, and almost every real citation is a
# plain external URL that can never match. Skipping ``urlsplit`` for
# those keeps the bibliography pass off a per-link parse.
if "://" not in url or _LIBRARY_ALIAS_HOST not in url.lower():
return None
# Reject tab/newline/CR before ``urlsplit`` sees them: it deletes them
# silently, which would collide ``/a<TAB>b`` with a real ``/ab`` and
# rewrite the citation to a DIFFERENT document. The neighbouring
# manual splits avoid ``urlsplit`` for exactly this reason.
if any(ch in url for ch in "\t\n\r"):
return None
try:
parsed = urlsplit(url)
except ValueError:
return None
if (
parsed.scheme != _LIBRARY_ALIAS_SCHEME
or (parsed.hostname or "") != _LIBRARY_ALIAS_HOST
):
return None
try:
if parsed.port not in (None, 443):
return None
except ValueError: # malformed port
return None
path_match = _LIBRARY_ALIAS_PATH_RE.match(parsed.path)
if not path_match:
return None
doc_id = unquote(path_match.group("doc_id"))
# Same predicate ``extract_document_id`` applies, imported rather than
# restated: the doc id is decoded before BOTH validating and
# re-emitting, so ``/%2D`` and ``/-`` forms of one id give one key.
if not is_safe_document_id(doc_id):
return None
suffix = path_match.group("suffix")
if parsed.query:
match = re.fullmatch(
r"document_chunk_id=([1-9][0-9]{0,18})", parsed.query
)
if (
suffix != "chunks"
or not is_valid_chunk_fragment(parsed.fragment)
or match is None
or int(match.group(1)) > (1 << 63) - 1
):
return None
rel = f"/library/document/{doc_id}"
if suffix:
rel = f"{rel}/{suffix}"
return f"{rel}#{parsed.fragment}" if parsed.fragment else rel
def _parse_library_citation(raw: str) -> tuple[str, str] | None:
"""Return ``(dedup_key, display_url)`` for a library citation, else ``None``.
One parser for every accepted spelling — the relative
``/library/document/<id>`` route, its ``/lib/`` abbreviation, and the
absolute ``https://library.document/<id>`` alias — so the two paths
cannot enforce different rules. They used to: only the alias validated
its document id, which meant the exact traversal this function's
sibling docstring promises to block sailed straight through when the
same URL arrived relative. ``/library/document/7/../../../admin/wipe``
keyed as document ``7`` and *displayed* as ``/admin/wipe``, so a
crafted citation merged into a real document's entry, inherited its
title and citation numbers, and replaced its link.
* ``dedup_key`` is ``/library/document/<id>`` — the ``/pdf``,
``/chunks`` and ``#chunk-<n>`` views of one document are one source
and belong on one bibliography line.
* ``display_url`` keeps the view suffix and the ``#chunk-<n>`` anchor,
which is what makes a citation scroll to the cited text.
Normalisations, all identity-preserving: ``/lib/`` becomes
``/library/``, the document id is percent-decoded (so ``a%2Db`` and
``a-b`` are one key, matching what the resolver looks up), and a query
string is dropped (the route ignores it).
"""
if not raw:
return None
stripped = raw.strip()
# Cheap reject before any parsing: the overwhelming majority of
# citations are ordinary external URLs.
if not (
stripped.startswith(_LIBRARY_ROUTE_PREFIXES)
or ("://" in stripped and _LIBRARY_ALIAS_HOST in stripped.lower())
):
return None
# Refuse anything carrying a control character rather than truncating
# the doc-id segment at it. Truncating keys the crafted URL as the
# real ``/library/document/<id>`` it was prefixed with, merging it
# into that document's bibliography entry and lending it the
# document's title and citation number. Returning ``None`` keeps the
# two sources apart; the payload still cannot forge a Sources line,
# because every field rendered into that block goes through
# ``search_utilities._sanitize_sources_field``.
if _has_control_chars(stripped):
return None
alias = _normalize_library_alias(stripped)
if alias is not None:
stripped = alias
if not _is_library_route(stripped):
return None
# Split fragment and query manually rather than via ``urlsplit``,
# which silently deletes embedded tab/newline and would collide
# ``/a<TAB>b`` with a real ``/ab``.
head, sep, fragment = stripped.partition("#")
path = head.split("?", 1)[0]
path_match = _LIBRARY_ROUTE_PATH_RE.match(path)
if not path_match:
return None
doc_id = unquote(path_match.group("doc_id"))
# Same predicate ``extract_document_id`` applies, imported rather than
# restated: the id is decoded before BOTH validating and re-emitting,
# so ``/%2D`` and ``/-`` forms of one id give one key.
if not is_safe_document_id(doc_id):
return None
key = f"/library/document/{doc_id}"
suffix = path_match.group("suffix")
display = f"{key}/{suffix}" if suffix else key
# Re-attach the fragment ONLY when it is a real chunk anchor. A
# library route carries no other meaningful fragment, so anything else
# is either a producer bug or an unvalidated string that reached us
# from the agent (``fetch_content`` passes its argument through
# ``library_resolver`` verbatim). Re-emitting it built a citation URL
# whose anchor cannot name a chunk — a dead link that still claims to
# cite one — and smuggled arbitrary text into the rendered Sources
# block, bypassing the ``MAX_CHUNK_INDEX``/format contract that
# ``chunk_anchor`` calls itself the single source of truth for.
# Dropping it keeps the citation (the route still resolves to the
# document) and loses only an anchor that never pointed anywhere.
if sep and fragment and is_valid_chunk_fragment(fragment):
display = f"{display}#{fragment}"
return key, display
def library_display_url(raw: str) -> str | None:
"""Return the display URL when *raw* is a library route, else ``None``.
``canonical_url_key`` collapses library routes to a per-document key,
which is right for grouping and wrong for display: the ``#chunk-<n>``
anchor is the entire point of a chunk-targeted citation. Renderers
group on the key and display this.
The returned URL is the *normalized* route (``/library/`` spelling,
percent-decoded id, no query), never the caller's raw string — a
string that does not parse as a library route returns ``None`` rather
than being echoed back into the report.
"""
parsed = _parse_library_citation(raw)
return parsed[1] if parsed is not None else None
# A chunk anchor is ``#chunk-<n>`` with the exact index shape
# ``build_chunk_anchor_url`` emits. ``\d`` is Unicode-wide, so it is spelled
# out here: ``chunk-\u0667`` and ``chunk-12`` would otherwise be accepted
# while ``chunk_anchor.extract_chunk_index`` — which calls itself the
# single source of truth for this — rejects them.
# ``\Z``, not ``$``: Python's ``$`` also matches just before a single
# trailing newline even without ``re.MULTILINE``, so ``chunk-1\n``
# passed. Both current callers strip and reject control characters
# upstream, so nothing reaches here with one today — but this is the
# shared predicate, and a future caller that skips those gates would
# inherit a newline-smuggling hole in the one rule both paths trust.
_CHUNK_FRAGMENT_RE = re.compile(r"^chunk-(0|[1-9][0-9]*)\Z", re.ASCII)
def is_valid_chunk_fragment(fragment: str) -> bool:
"""Return ``True`` if *fragment* (the part after ``#``) is a chunk
anchor that can actually name a chunk.
Shared by :func:`library_display_url` (which drops a fragment failing
this test) and :func:`preferred_chunk_display` (which rejects the whole
URL), so the render path and the store path cannot disagree about what
a valid anchor is.
"""
if not isinstance(fragment, str):
return False
match = _CHUNK_FRAGMENT_RE.match(fragment)
if match is None:
return False
# Bound the digit run BEFORE converting: the pattern admits an
# unbounded one, and int() raises above CPython's 4300-digit limit —
# out of a pure formatter with no caller catching it, which would take
# down the whole Sources block.
if len(match.group(1)) > len(str(MAX_CHUNK_INDEX)):
return False
# Bound it the way the producer does, so a fragment that cannot name a
# real chunk is not treated as though it could.
return int(match.group(1)) <= MAX_CHUNK_INDEX
# Result-dict key holding a chunk-anchored spelling recorded alongside a
# citation whose stored link has no anchor. A separate key, never an
# overwrite of ``link``/``url``: consumers opt in, and a stray write can
# only add an ignored hint rather than corrupt a citation URL.
CHUNK_DISPLAY_KEY = "chunk_display_url"
# Result-dict key holding the page's own ``<title>``, recorded alongside a
# citation whose stored title came from a search engine's result listing. A
# separate key for the same reason as ``CHUNK_DISPLAY_KEY``: never an overwrite.
FETCHED_TITLE_KEY = "fetched_title"
def preferred_chunk_display(raw: str) -> str | None:
"""Return the display URL when *raw* is a library route WITH a valid
chunk anchor, else ``None``.
``library_display_url`` validates the route and keeps a VALID chunk
anchor, dropping any other fragment — a citation without an anchor is
still a citation. This is the stricter question a caller asks when it
wants to *store* an anchored spelling in preference to one it already
has: the anchor has to be present, not merely permitted.
(This paragraph said "re-emits the fragment verbatim" until that
behaviour was removed — the wording was corrected in the twin function
and left stale here, which is the same one-instance-at-a-time slip the
functions themselves keep exhibiting.)
One helper for both callers on purpose. The collector previously
re-derived this rule from a comment saying it matched the renderer's,
and the copy drifted twice — once accepting any ``#chunk-`` substring,
once validating the route while ignoring the fragment entirely.
"""
display = library_display_url(raw)
if display is None:
return None
_, sep, fragment = display.partition("#")
# ``library_display_url`` already drops a fragment that fails this
# test, so the ``is_valid_chunk_fragment`` call here is INERT today:
# mutating it away kills no test, because nothing can reach it with a
# fragment that would fail. It is defence in depth, not a live check —
# said plainly so nobody reads a passing test as evidence it fires.
#
# Kept anyway, deliberately: it makes this function correct
# independently of what ``library_display_url`` does, and the two
# answer different questions ("what do I render" vs "is this worth
# storing"), so a future change to either must not silently make this
# one the looser of the pair — which is exactly how the collector's
# hand-rolled copy of this rule drifted twice before it was
# centralised here. The ``not sep`` half IS load-bearing: it is what
# distinguishes "has an anchor" from "has none".
if not sep or not is_valid_chunk_fragment(fragment):
return None
return display
def _is_wayback_replay_url(url: str, parsed: SplitResult) -> bool:
"""Recognize a capture URL whose embedded target must stay verbatim.
Capture availability and redirect safety are checked by the archive
client. This only keeps citation normalization from changing its target.
"""
if any(character.isspace() or ord(character) < 32 for character in url):
return False
try:
if (
parsed.scheme not in {"http", "https"}
or parsed.hostname != "web.archive.org"
or parsed.port not in {None, 80, 443}
or parsed.username is not None
or parsed.password is not None
):
return False
replay = re.fullmatch(
r"/web/[0-9]{14}(?:id_|if_)?/(https?://.+)", parsed.path
)
if replay is None:
return False
target = urlsplit(replay.group(1))
# Match the archive client's absolute, credential-free target
# boundary without importing transport code into this utility.
if (
not target.hostname
or target.username is not None
or target.password is not None
):
return False
target.port
except ValueError:
return False
return True
@lru_cache(maxsize=1024)
def canonical_url_key(url: str) -> str:
"""Return a canonical form of ``url`` suitable for deduplication and
display in a Sources / citations listing.
The canonical form:
- lowercases scheme and host (paths stay case-sensitive),
- strips userinfo (``user:pass@`` — never leak creds),
- strips default ports (80/http, 443/https),
- strips fragments,
- drops tracking query params (``utm_*``, ``fbclid``, ``gclid``,
``msclkid``, ``yclid``, ``dclid``, ``gad_source``, ``mc_eid``,
``mc_cid``, ``ref_src``, ``igshid``, ``_ga``, ``_gl``),
- trims a trailing ``/`` from non-root paths.
Click-through behavior is preserved — tracking params carry no
content, and mainstream browsers already strip them automatically.
Percent-encoding is not normalized: the raw query spellings
``?q=a+b`` vs ``?q=a%20b`` and ``?a=1;b=2`` vs ``?a=1%3Bb=2`` hash
differently (a server may treat ``+``/``;`` literally, so merging
them would risk linking a resource to the wrong document). Query
param order is preserved as-is here; callers that need
order-insensitivity (e.g. download-tracker hashing) sort afterwards.
Timestamped Wayback replay URLs retain their complete spelling,
including the capture modifier and embedded target's path, query and
fragment. Removing a trailing slash or a tracking parameter there
changes the captured target rather than merely cleaning a live link.
Internal library-document routes (``/library/document/<id>``, plus its
``/pdf`` and ``/chunks#chunk-<n>`` views) collapse to a per-document
key, because they are views of one source and belong on one
bibliography line. That key drops the ``#chunk-<n>`` anchor, so
anything that RENDERS a link must display the original URL instead —
see :func:`library_display_url`.
Falls back to ``url.strip()`` for everything else that is not a
recognizable absolute URL (``mailto:``, ``data:``, protocol-relative
``//host/p``, bare filesystem paths, SPA ``/app#/route`` links), since
canonicalization would be ambiguous — and merging distinct sources is
worse than leaving them separate.
"""
if not url:
return ""
# Library-document routes — relative, ``/lib/``-abbreviated, or the
# absolute ``https://library.document/<id>`` alias the agent emits —
# all key to one per-document route, so every view of one document
# shares a single bibliography entry.
library = _parse_library_citation(url)
if library is not None:
return library[0]
try:
parsed = urlsplit(url)
except Exception:
return url.strip()
# Require both a scheme and a netloc; otherwise canonicalization is
# ambiguous (mailto:, data:, protocol-relative, etc.).
if not parsed.scheme or not parsed.netloc:
# Library routes were already handled above. Everything else that
# is not a recognizable absolute URL falls back to the raw string:
# canonicalization is deliberately NOT generalized to every
# root-relative path, because a LangChain retriever sets a
# result's url from its ``source`` metadata, which is commonly an
# absolute filesystem path. Treating those as routes merges
# genuinely distinct sources — ``/docs/C#/a.md`` and
# ``/docs/C#/b.md`` would both key to ``/docs/C``, a path that
# does not exist — which is worse than the fan-out being fixed.
# The same applies to SPA-style ``/app#/route`` links.
#
# A library route with a control character does NOT reach here as
# a library key: ``_parse_library_citation`` refuses it, so it
# falls through to this verbatim fallback. That is deliberate.
# Truncating the doc-id segment instead would key the crafted URL
# as the real ``/library/document/<id>`` it was prefixed with,
# merging it into that document's bibliography entry and handing
# it the document's title and citation number. The payload cannot
# forge a line from here either: every field rendered into the
# ``## Sources`` block — this URL included — goes through
# ``search_utilities._sanitize_sources_field``, which flattens
# control characters to spaces.
return url.strip()
if _is_wayback_replay_url(url, parsed):
return url
scheme = parsed.scheme.lower()
# Strip userinfo (user:pass@host) from netloc.
netloc = parsed.netloc.rsplit("@", 1)[-1]
# Split host/port carefully so IPv6 literals survive.
if netloc.startswith("["):
end = netloc.find("]")
raw_host = netloc[: end + 1]
# Lowercase IPv6 address portion while preserving case-sensitive zone ID (RFC 6874)
if "%" in raw_host:
addr, zone = raw_host.split("%", 1)
host = f"{addr.lower()}%{zone}"
else:
host = raw_host.lower()
rest = netloc[end + 1 :]
port = rest[1:] if rest.startswith(":") else ""
elif ":" in netloc:
host, _, port = netloc.rpartition(":")
host = host.lower()
else:
host, port = netloc.lower(), ""
if (scheme == "https" and port == "443") or (
scheme == "http" and port == "80"
):
port = ""
netloc = f"{host}:{port}" if port else host
# Filter query params case-insensitively on key; preserve raw
# spellings and order. Operates on the raw ``&``-split pairs (no
# parse_qsl/urlencode round-trip): decoding would merge spellings the
# tracker must keep distinct (``+`` vs ``%20``, ``;`` vs ``%3B`` —
# issue #6758 B2 review). Tracking detection decodes the key with
# ``unquote`` (``%`` only, ``+`` left intact) so an encoded tracking
# key is still stripped, but the kept pairs are re-emitted verbatim.
if parsed.query:
kept_raw: list[str] = []
for raw_pair in parsed.query.split("&"):
if raw_pair == "":
continue
raw_key = raw_pair.split("=", 1)[0]
try:
decoded_key = unquote(raw_key).lower()
except Exception:
decoded_key = raw_key.lower()
if decoded_key in _TRACKING_PARAMS or any(
decoded_key.startswith(p) for p in _TRACKING_PREFIXES
):
continue
kept_raw.append(raw_pair)
query_str = "&".join(kept_raw)
else:
query_str = ""
path = parsed.path
if path and path != "/" and path.endswith("/"):
path = path.rstrip("/")
return urlunsplit((scheme, netloc, path, query_str, ""))
@lru_cache(maxsize=1024)
def content_web_cache_key(url: str) -> str:
"""Return a cache key for *web* content fetches (NOT bibliography dedup).
Same normalisations as :func:`canonical_url_key` — lowercased
scheme/host, stripped default ports, dropped tracking query params —
but with deliberate differences, because bibliographic equivalence
does not establish identical fetched bytes:
* the path is preserved verbatim, including a non-root trailing
``/``: ``/article`` and ``/article/`` are different HTTP resources
and routinely serve different bodies;
* URL userinfo (``user:pass@``) is preserved verbatim (host still
lowercased): ``https://alice@host/p`` and ``https://bob@host/p``
authenticate differently and must never share an entry;
* the fragment is preserved verbatim: client-routed SPAs (hash
routing) render different views per fragment under JS rendering
(Playwright navigates with the fragment intact).
Query handling otherwise matches the bibliography key: only tracking
params (``utm_*``, ``fbclid``, ...) collapse; every other param,
its order and its values are preserved, so ``?a=1`` and ``?a=2``
stay distinct. Dropping tracking parameters accepts the trade-off of
potential over-merge on rare sites that vary content per tracking param
(e.g., campaign- or affiliate-specific landing pages). Raw spellings
(``?q=a+b`` vs ``?q=a%20b``) stay
distinct, matching :func:`canonical_url_key` (#6758 B2). Anything
without a scheme+netloc falls back to the stripped raw string (same as
:func:`canonical_url_key`).
Library routes are NOT handled here — callers resolve those via
``library_display_url`` first. A relative ``/library/...`` URL
therefore falls through to the raw fallback, never to a web key.
"""
if not url:
return ""
try:
parsed = urlsplit(url)
except Exception:
return url.strip()
if not parsed.scheme or not parsed.netloc:
return url.strip()
scheme = parsed.scheme.lower()
# Preserve userinfo verbatim (credentials are case-sensitive and
# select a different origin view); lowercase only the host part.
raw_netloc = parsed.netloc
if "@" in raw_netloc:
userinfo, _, hostport = raw_netloc.rpartition("@")
else:
userinfo, hostport = "", raw_netloc
# Split host/port carefully so IPv6 literals survive.
if hostport.startswith("["):
end = hostport.find("]")
if end == -1:
return url.strip()
host = hostport[: end + 1].lower()
rest = hostport[end + 1 :]
port = rest[1:] if rest.startswith(":") else ""
if rest and not rest.startswith(":"):
return url.strip()
elif ":" in hostport:
host, _, port = hostport.rpartition(":")
host = host.lower()
else:
host, port = hostport.lower(), ""
if (scheme == "https" and port == "443") or (
scheme == "http" and port == "80"
):
port = ""
hostport_norm = f"{host}:{port}" if port else host
netloc = f"{userinfo}@{hostport_norm}" if userinfo else hostport_norm
# Filter query params case-insensitively on key; preserve raw
# spellings and order (same raw-preserving filter as
# :func:`canonical_url_key` — no parse_qsl/urlencode round-trip).
if parsed.query:
kept_raw: list[str] = []
for raw_pair in parsed.query.split("&"):
if raw_pair == "":
continue
raw_key = raw_pair.split("=", 1)[0]
try:
decoded_key = unquote(raw_key).lower()
except Exception:
decoded_key = raw_key.lower()
if decoded_key in _TRACKING_PARAMS or any(
decoded_key.startswith(p) for p in _TRACKING_PREFIXES
):
continue
kept_raw.append(raw_pair)
query_str = "&".join(kept_raw)
else:
query_str = ""
# Path preserved verbatim: case AND trailing slash both select
# distinct HTTP resources. Fragment preserved: client-routed SPAs
# (hash routing) render different content per fragment under JS
# rendering (playwright_html navigates with fragment intact).
path = parsed.path
fragment = parsed.fragment
return urlunsplit((scheme, netloc, path, query_str, fragment))
def is_safe_custom_llm_endpoint(custom_endpoint: Optional[str]) -> bool:
"""SSRF guard for a user-supplied custom LLM endpoint, applied at the
request boundary as fail-fast defense-in-depth.
The endpoint is normalized exactly as the OpenAI-compatible provider
normalizes it (:func:`normalize_url`), so scheme-less local endpoints
such as ``localhost:11434`` or ``192.168.1.10:8000`` are handled the
same way the provider handles them, then validated with
:func:`validate_url` allowing private IPs / localhost. That accepts
local LLM backends (Ollama / LM Studio / vLLM) while still blocking
cloud-metadata and link-local targets. An empty / unset endpoint is
safe (there is nothing to send to). On rejection a redacted warning
is logged (the raw URL may carry credentials).
This is not the sole protection: the OpenAI-compatible provider's
``assert_base_url_safe`` re-validates the same URL before the
LangChain client is constructed. This guard simply rejects early —
before any DB row is written or research thread is spawned — and
keeps the endpoint out of the logs.
Scope: this validates only the submitted URL. This helper does not validate
redirect targets or pin connection-time name resolution.
Callers hand this whatever a JSON body contained, so a non-string
(other than ``None``) is rejected rather than coerced: it cannot be a
usable endpoint, and coercing it would either raise inside ``strip()``
or silently treat ``[]`` / ``{}`` as "unset" and persist them.
"""
if custom_endpoint is None:
return True
if not isinstance(custom_endpoint, str):
logger.warning(
"SSRF protection: rejected non-string custom_endpoint of type {}",
type(custom_endpoint).__name__,
)
return False
endpoint = custom_endpoint.strip()
if not endpoint:
return True
candidate = normalize_url(endpoint)
# allow_private_ips=True is deliberate: a self-hosted LLM backend
# legitimately lives on 127.0.0.1 or an RFC1918 LAN address, so those must
# stay reachable. block_link_local=True is the carve-out inside that --
# cloud instance metadata uses link-local ranges, while self-hosted model
# servers commonly use localhost or RFC1918 addresses. Blocking the range,
# rather than a short literal list, preserves that distinction for IPv4
# and IPv6.
#
# Regression evidence and self-hosted controls:
# tests/security/test_llm_endpoint_link_local_hardening.py
# - test_link_local_endpoint_is_refused_at_the_http_boundary
# - test_self_hosted_endpoint_still_accepted
if validate_url(candidate, allow_private_ips=True, block_link_local=True):
return True
logger.warning(
"SSRF protection: rejected custom_endpoint URL: {}",
redact_url_for_log(candidate),
)
return False