~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
"""Pin the validated IP for the actual outbound connection.
Closes the resolve-vs-connect gap in the SSRF guard. ``validate_url``
resolves a hostname via ``socket.getaddrinfo`` and checks the resulting
addresses against the private/internal/metadata block lists, but
``requests``/``urllib3`` then resolve the hostname *again, independently*
at connect time. An attacker who controls a DNS authority can answer with
a public address while the guard is looking and a private / cloud-metadata
address a moment later at connect — so the address that was validated is
not the address that gets connected to.
This module makes the fetch connect to the SAME address that was
validated. A thread-local registry of pinned ``host -> addrinfo`` entries
is consulted by a process-wide ``socket.getaddrinfo`` shim installed once
at import. While a host is pinned (for the duration of a single request,
re-established at each redirect hop), the shim returns ONLY the
pre-validated addresses for that host; every other lookup falls through to
the real resolver untouched.
Why a ``getaddrinfo`` shim rather than a custom ``HTTPAdapter``:
* It covers BOTH sinks uniformly — the module-level ``requests.get`` /
``requests.post`` used by ``safe_get`` / ``safe_post`` AND the
``SafeSession.send`` path — without threading a mounted adapter through
every call site or per-request session.
* TLS is preserved for free. The request URL keeps the original hostname,
so ``urllib3`` still sets the TLS SNI and verifies the server
certificate against that hostname. Only name resolution is redirected,
so there is NO certificate-verification bypass and no
``server_hostname`` / ``assert_hostname`` plumbing to get wrong.
* It behaves identically for ``http`` and ``https`` and re-pins cleanly at
every redirect hop.
* Thread safety: the pin registry is thread-local, so concurrent requests
in LDR's thread pools never observe each other's pins, and every
unpinned lookup is an untouched pass-through. This mirrors the existing
``security/egress`` ``socket.connect`` audit hook, which also installs a
process-wide, thread-local-gated interposition.
The outbound-notification path (Apprise) reuses the same shim through
:func:`pinned_notification_send`. Apprise fans out to arbitrary user URLs
via ``requests``/``urllib3`` and re-resolves at send time — the exact
resolve-vs-connect gap the shim closes — but it also follows redirects and
can be pointed at a *new*, unpinned host. So the notification window pins
every raw-webhook host in the batch AND activates a thread-local
"block-private" mode (:func:`block_private_resolution`): for its duration,
any UNPINNED lookup that resolves to a private/loopback/link-local/
cloud-metadata address is refused at the ``getaddrinfo`` layer (a
``socket.gaierror``), so a redirect-to-internal is blocked at the socket
layer before a connection is made. Public unpinned hosts resolve normally.
The block is thread-local, so it only affects the sending thread and can
never wrongly block a legitimate private request running concurrently in
another thread. It is activated only after Apprise is forced into
synchronous, in-thread delivery (``AppriseAsset(async_mode=False)``); with
the default async fan-out the send would run in worker threads that carry
no thread-local pin/block and the guard would silently not apply.
The PDF export resource fetcher (``web/services/pdf_service._SafeUrlFetcher``)
wraps each WeasyPrint ``URLFetcher`` fetch (urllib, redirects disabled) in
:func:`pinned_request` nested with :func:`block_private_resolution`, using the
same default policy as its ``validate_url`` call, and refuses the fetch when
:func:`_shim_installed` is False. When the fetcher has a forward proxy
configured for the URL's scheme, the proxy's exact (host, port) lookup is
relaxed with :func:`allow_resolution` so a private proxy keeps working, while
metadata addresses stay refused. urllib connects on the calling thread, so the
thread-local pin applies. The block window covers any lookup that misses the
pin; the fetcher also refuses URLs with userinfo, since urllib would look up
``user@host`` rather than the pinned host.
Import-order dependency: the pin only works while ``socket.getaddrinfo``
is still THIS module's shim. If something replaces ``socket.getaddrinfo``
wholesale AFTER this module is imported — e.g. ``gevent``/``eventlet``
monkey-patching, or any other socket shim — that later patch silently
overwrites ours and the pin is dropped fail-open (validation still runs,
but the connect-time re-resolution it is meant to catch goes unguarded
again). LDR runs Socket.IO in threading ``async_mode`` and does not
monkeypatch sockets, so this does not currently apply — but if that ever
changes, any such monkeypatching must happen BEFORE this module (or
``security.safe_requests``, which imports it) is first imported.
"""
import contextlib
import ipaddress
import socket
import threading
from typing import Iterator, List, Optional, Tuple
import requests
from loguru import logger
from urllib3.exceptions import LocationParseError
from urllib3.util import parse_url
from . import ssrf_validator
from .log_sanitizer import redact_and_bound_for_log
# Attribute stamped on our shim (see below) so any later capture of the
# "real" resolver can recognize it and refuse to capture the shim as if it
# were the genuine resolver.
_SHIM_MARKER = "_ldr_dns_pin_shim"
def _capture_real_getaddrinfo():
"""Return the genuine resolver, never our own shim.
On a normal import ``socket.getaddrinfo`` is still the stdlib resolver
and is captured as-is. But if this module is *reloaded*
(``importlib.reload``) after :func:`install` has already swapped in our
shim, ``socket.getaddrinfo`` is that shim — capturing it here would make
the shim's pass-through call itself, recursing forever (``RecursionError``)
on every unpinned lookup process-wide. In that case fall back to the real
resolver captured by the previous load, which survives in this module's
globals across a reload.
"""
current = socket.getaddrinfo
if not getattr(current, _SHIM_MARKER, False):
return current
# Reload after install(): don't re-capture our own shim. Reuse the real
# resolver the previous load already captured (guaranteed not to be a
# shim, thanks to this very guard).
previous = globals().get("_real_getaddrinfo")
if previous is not None and not getattr(previous, _SHIM_MARKER, False):
return previous
return current
# The genuine resolver, captured at import BEFORE the shim is installed.
# Referenced by module-global name at call time (never closed over) so a
# test can patch this single seam to control what BOTH the validation-time
# resolution and the pin-time resolution observe. Captured via the guard
# above so a reload cannot make the shim call itself.
_real_getaddrinfo = _capture_real_getaddrinfo()
# host (normalized) -> list of addrinfo 5-tuples that are safe to connect
# to. Thread-local so concurrent fetches in different worker threads cannot
# see one another's pins.
#
# The same thread-local also carries an optional ``block_private`` entry —
# a ``(allow_localhost, allow_private_ips, block_link_local)`` tuple set by
# :func:`block_private_resolution`. While present, every UNPINNED lookup on
# this thread is resolved and re-checked against the SSRF policy, and a
# result containing any blocked (private/loopback/link-local/metadata)
# address is refused. See :func:`_resolve_maybe_block`.
_thread_state = threading.local()
# Schemes whose URL host is a real, resolvable connection target that the
# client will connect to verbatim — so pinning the validated address is both
# possible and worthwhile. These are the raw-webhook Apprise schemes
# (``json``/``xml``/``form`` and their TLS variants) plus plain
# ``http``/``https``.
#
# Every OTHER reachable Apprise scheme is deliberately excluded from pinning,
# for one of two reasons — but both remaining kinds are still resolve-time
# block-checked by the block-private window (:func:`block_private_resolution`),
# so excluding them from pinning never leaves a cloud-metadata hole:
#
# * Token-host schemes (``discord://``, ``slack://``, ``tgram://`` …) put an
# opaque credential/token in ``parse_url``'s host field, not a hostname.
# Pinning would try to resolve that token as a name and break the send;
# these plugins POST to their own hardcoded public API endpoints anyway.
# * Self-hosted plugin schemes (``gotify://``, ``ntfy(s)://``, ``mattermost://``,
# ``matrix://``, ``rocketchat://``, ``teams://`` …) DO carry a real,
# resolvable host — the "opaque token" description does not apply to them.
# They are left unpinned because their per-plugin URL→endpoint mapping is
# plugin-specific (path/port rewriting) rather than a plain connect to the
# URL host. The send-time block-private window enforces the selected
# destination policy even when there is no precomputed pin.
#
# This block-window guarantee holds only for schemes whose network I/O resolves
# through the shimmed ``socket.getaddrinfo`` — which every scheme reachable
# today does (they send via ``requests``/``urllib3`` or ``smtplib``, both of
# which call ``getaddrinfo``). Which schemes can reach this send path at all is
# gated upstream by ``NotificationURLValidator.ALLOWED_SCHEMES``; a scheme that
# connects via a primitive the shim cannot see (e.g. a raw ``socket.sendto``
# datagram, as Apprise's ``rsyslog://`` uses) would bypass BOTH the pin and the
# block window, so such a scheme must never be added to that allowlist.
_PINNABLE_SCHEMES = frozenset(
{
"http",
"https",
"json",
"jsons",
"xml",
"xmls",
"form",
"forms",
}
)
_installed = False
def _normalize_host(host: Optional[str]) -> Optional[str]:
"""Normalize a host into the key form used by the pin registry.
Lower-cases (DNS is case-insensitive, and both ``urllib3.parse_url``
and ``urllib3``'s connection layer already lower-case the host before
it reaches ``getaddrinfo``), strips IPv6 brackets, and drops the
trailing root dot (``urllib3`` calls ``self._dns_host.rstrip(".")``
before resolving). Applying the same transform on both the pinning and
the lookup side guarantees the key matches.
"""
if not host:
return None
host = host.strip().lower()
if host.startswith("[") and host.endswith("]"):
host = host[1:-1]
host = host.rstrip(".")
return host or None
def _host_for_log(host) -> str:
"""Best-effort text form of a host for a log message.
``getaddrinfo`` accepts a ``bytes``/``bytearray`` host as well as a
``str``; decode one to text so the block-window warning reads cleanly.
Decoding is for the log line only — the SSRF check runs on resolved IPs,
never on the host string — so it must never raise: ``surrogateescape``
round-trips any byte sequence. (The ``idna`` codec is deliberately NOT
used here: it does not support an error handler and raises on non-hostname
bytes.)
"""
if isinstance(host, (bytes, bytearray)):
return bytes(host).decode("utf-8", errors="surrogateescape")
return str(host)
def _get_pins() -> dict:
pins = getattr(_thread_state, "pins", None)
if pins is None:
pins = {}
_thread_state.pins = pins
return pins
def reset_ssrf_block() -> None:
"""Clear the per-thread "an SSRF block occurred" marker.
Called before a guarded ``notify()`` so :func:`consume_ssrf_block`
reflects only that attempt. See :func:`_resolve_maybe_block`.
"""
_thread_state.ssrf_block_occurred = False
def consume_ssrf_block() -> bool:
"""Return whether the block-private window refused a lookup on this
thread since the last :func:`reset_ssrf_block`, and clear the marker.
Apprise swallows the ``socket.gaierror`` the block raises inside its
plugin, surfacing only a generic delivery failure. The sender
(``notifications.service._send_with_retry``) consults this after a failed
send to tell a confirmed security block apart from a transient error, so
the block can fail fast instead of being retried 3x.
"""
occurred = getattr(_thread_state, "ssrf_block_occurred", False)
_thread_state.ssrf_block_occurred = False
return bool(occurred)
# Send-time DNS resolution is bounded so a slow / hostile DNS authority
# cannot hang the sending thread — which, on the notification path, is an
# HTTP request handler (test_service is reached straight from the "Send Test
# Notification" endpoint). Mirrors
# ``notification_validator._resolve_hostname_ips``.
_RESOLVE_TIMEOUT_SECONDS = 5
def _getaddrinfo_bounded(host, port, family, type, proto, flags):
"""Call the genuine resolver with a bounded timeout; fail closed.
Runs ``_real_getaddrinfo`` on a short-lived DAEMON thread (thread-safe
timeout, no ``socket.setdefaulttimeout`` process-global mutation) so a
hostile DNS authority that never answers cannot block the caller past
``_RESOLVE_TIMEOUT_SECONDS``. On timeout a ``socket.gaierror`` is raised
— the lookup is REFUSED, never returned as a blank/partial answer a
caller might treat as "resolved" — so the timeout can only ever fail
closed. A resolution error raised by the resolver itself
(``socket.gaierror`` / ``OSError``) propagates unchanged.
The worker is a DAEMON thread deliberately: a ``getaddrinfo`` call that
hangs in C cannot be cancelled, and a non-daemon worker (as a
``concurrent.futures`` pool thread is) would be joined by the interpreter
at shutdown — letting an in-flight hostile lookup block graceful process
exit / restart. A daemon worker is abandoned on timeout and never blocks
interpreter exit.
"""
result: dict = {}
def _worker() -> None:
try:
result["value"] = _real_getaddrinfo(
host, port, family, type, proto, flags
)
except BaseException as exc: # noqa: BLE001 - propagated to caller
result["error"] = exc
worker = threading.Thread(
target=_worker,
name="ldr-dns-pin-resolve",
daemon=True,
)
worker.start()
worker.join(_RESOLVE_TIMEOUT_SECONDS)
if worker.is_alive():
# Timed out. The daemon worker is abandoned (it dies with the
# process) and the lookup is refused — fail closed.
raise socket.gaierror(
getattr(socket, "EAI_AGAIN", socket.EAI_FAIL),
f"DNS resolution for {host!r} timed out after "
f"{_RESOLVE_TIMEOUT_SECONDS}s during a pinned resolution (fail closed)",
)
if "error" in result:
# Re-raise the resolver's own error unchanged (e.g. gaierror for
# NXDOMAIN) so existing gaierror/OSError handling stays intact.
raise result["error"]
return result["value"]
def _pinned_getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
"""Process-wide ``socket.getaddrinfo`` replacement.
Returns the pinned addresses when the calling thread has an active pin
for ``host``; otherwise delegates unchanged to the real resolver, so
this is a transparent pass-through for every lookup that is not part of
a pinned request.
"""
key = _normalize_host(host) if isinstance(host, str) else None
if key is not None:
entry = _get_pins().get(key)
if entry is not None:
results = []
for fam, socktype, sockproto, canon, sockaddr in entry:
# Honor a caller-requested address family (AF_UNSPEC == 0
# means "any").
if family not in (0, socket.AF_UNSPEC, fam):
continue
# Honor a caller-requested socket type (0 == "any"). Every
# pinned entry is resolved as SOCK_STREAM (see
# _resolve_and_validate), which is also what urllib3's own
# connection lookups always request explicitly — so this
# never filters anything out of the normal HTTP(S) path. It
# only guards a hypothetical caller-requested SOCK_DGRAM (or
# other non-stream) lookup for a host that happens to be
# mid-pin, which would otherwise get back a SOCK_STREAM /
# IPPROTO_TCP entry mislabeled with the caller's requested
# type.
if type not in (0, socktype):
continue
# Rewrite the port from THIS lookup while preserving the
# IPv6 flowinfo / scope_id captured at pin time.
new_sockaddr = (sockaddr[0], port) + tuple(sockaddr[2:])
results.append(
(
fam,
type or socktype,
proto or sockproto,
canon,
new_sockaddr,
)
)
if results:
return results
# A pin exists but nothing matches the requested family/type.
# Fail closed rather than silently falling through to a fresh
# (attacker-controllable) resolution.
raise socket.gaierror(
getattr(socket, "EAI_ADDRFAMILY", socket.EAI_FAIL),
"No pinned address for the requested family/socktype",
)
# Not pinned. Delegate to the real resolver, but if a block-private
# window is active on this thread, refuse any answer that resolves to a
# blocked address (redirect-to-internal / rebind-to-metadata guard).
return _resolve_maybe_block(host, port, family, type, proto, flags)
def _resolve_maybe_block(host, port, family, type, proto, flags):
"""Real resolution for an unpinned host, honoring the block window.
When no block-private window is active this is a transparent
pass-through to the genuine resolver — with NO added timeout/thread
overhead, so ordinary process-wide DNS is completely unaffected by the
shim. When a window IS active (set by :func:`block_private_resolution`,
i.e. during a notification send), the resolution is (a) bounded by a
timeout so a slow / hostile DNS authority cannot hang the sending thread
(fail closed — a timeout refuses the lookup, it never returns a blank
answer), and (b) the resolved addresses are checked against the SSRF
policy and the lookup is refused with ``socket.gaierror`` if ANY of them
is blocked — closing a redirect-to-internal or rebind-to-metadata that
targets a host that was never pinned. The check runs on the SAME address
list the caller will connect to (this IS the ``getaddrinfo`` the client
uses), so there is no second resolution to race against.
No failure mode inside an active window escapes as a bare
``ValueError``: an IDNA-unencodable host raises a native
``UnicodeError`` (a ``ValueError`` subclass), which is caught below and
re-raised as ``socket.gaierror`` instead, since a ``ValueError``
reaching the sender would otherwise be mislabelled a confirmed
security block. A resolution error raised by the resolver itself
(``socket.gaierror`` / ``OSError``) propagates unchanged. The
IDNA case reaches this function through an allowed-but-not-pinnable scheme
(``ntfy``/``ntfys``, whose Apprise plugin parses with
``verify_host=False``); see the inline comment below.
``_resolve_and_validate`` enforces the same invariant at pin time.
"""
block = getattr(_thread_state, "block_private", None)
if block is None:
# No window active: transparent, zero-overhead pass-through.
return _real_getaddrinfo(host, port, family, type, proto, flags)
# Window active — bound the resolution (send path) and fail closed on a
# timeout by letting the gaierror propagate to the client.
#
# ``socket.getaddrinfo`` raises ``UnicodeError`` (a ``ValueError``
# subclass) for a host that cannot be IDNA-encoded — a label over 63
# bytes, or an empty label. Letting that escape from inside an active
# window would break the same invariant ``_resolve_and_validate``
# protects one function away: at this layer a ``ValueError`` reaching
# the sender means a CONFIRMED SSRF block, so an unencodable name would
# be mislabelled as a security block (``SecurityBlockError``) instead
# of an ordinary resolution failure.
#
# This path IS reachable in practice, but not via a pin-time failure:
# ``pin_hosts`` only considers :data:`_PINNABLE_SCHEMES`, and for those
# Apprise's ``add()`` (``verify_host=True``) rejects an
# IDNA-unencodable host before a pin is ever attempted. The live route
# is a scheme that is ALLOWED but NOT pinnable — ``ntfy://`` /
# ``ntfys://``, whose Apprise plugin calls
# ``NotifyBase.parse_url(url, verify_host=False)`` and therefore
# accepts such a host. ``pin_hosts`` skips it on the scheme check, so
# it stays unpinned and its send-time lookup lands here, inside the
# active block-private window. Refuse it the way every other failed
# lookup in this shim is refused: ``socket.gaierror``, fail closed.
try:
results = _getaddrinfo_bounded(host, port, family, type, proto, flags)
except UnicodeError as exc:
raise socket.gaierror(
getattr(socket, "EAI_NONAME", getattr(socket, "EAI_FAIL", -1)),
f"DNS resolution for {_host_for_log(host)!r} failed: host is "
f"not encodable to IDNA (refused during a pinned resolution window)",
) from exc
# A ``None`` host is an AF_PASSIVE bind
# (``getaddrinfo(None, port, ..., AI_PASSIVE)`` — a local listening-socket
# setup, not an outbound name to rebind), so it carries no attacker-
# controllable destination and is exempt from the block check. EVERY other
# host is checked, INCLUDING a ``bytes``/``bytearray`` host: ``getaddrinfo``
# accepts a bytes name and a bytes host (e.g. ``b"169.254.169.254"``)
# resolves to metadata/link-local exactly as a ``str`` host would, so
# returning it unchecked would be the one guard that fails OPEN. The check
# itself runs on the resolved IPs regardless of the host type — only the
# log line needs a text host, so a bytes host is decoded for display.
if host is None:
return results
host_display = _host_for_log(host)
allow_localhost, allow_private_ips, block_link_local = block
# An exact (host, port) exemption registered by :func:`allow_resolution`
# swaps in its own (relaxed) policy for that one lookup. The check below
# still runs, so the always-blocked metadata addresses stay refused.
exemptions = getattr(_thread_state, "block_exemptions", None)
if exemptions and isinstance(host, str):
relaxed = exemptions.get((host, port))
if relaxed is not None:
allow_localhost, allow_private_ips, block_link_local = relaxed
for info in results:
ip_str = str(info[4][0])
if ssrf_validator.is_ip_blocked(
ip_str,
allow_localhost=allow_localhost,
allow_private_ips=allow_private_ips,
block_link_local=block_link_local,
):
logger.warning(
"Blocked resolution of {} to internal/private/metadata IP "
"{} during a pinned resolution window "
"(possible SSRF / redirect-to-internal)",
host_display,
ip_str,
)
# Mark the thread so the sender can tell this confirmed security
# block apart from a transient failure once Apprise has swallowed
# the gaierror below — a confirmed block must fail fast, not be
# retried. Consumed by ``consume_ssrf_block``.
_thread_state.ssrf_block_occurred = True
raise socket.gaierror(
getattr(socket, "EAI_FAIL", -1),
"Blocked resolution to an internal/private/metadata IP "
"during a pinned resolution window",
)
return results
# Stamp our shim so a later capture (e.g. after ``importlib.reload``) can tell
# it apart from the genuine resolver and refuse to capture it as the "real"
# one — see :func:`_capture_real_getaddrinfo`.
setattr(_pinned_getaddrinfo, _SHIM_MARKER, True)
def install() -> None:
"""Install the process-wide ``getaddrinfo`` shim (idempotent).
A no-op after the first call. The shim is transparent until a thread
activates a pin via :func:`pinned_request`, so importing this module
has no behavioral effect on code that does not fetch through the safe
request helpers.
Idempotent under module reload too: if a shim (this load's or a prior
load's) is already the active resolver, it is not stacked on top of
itself.
"""
global _installed
if _installed:
return
# Install our shim unless it is ALREADY EXACTLY this module's shim
# (identity check, not just the marker). A reload resets ``_installed`` to
# False and rebuilds ``_pinned_getaddrinfo`` as a fresh function object,
# while ``socket.getaddrinfo`` still points at the PREVIOUS load's shim —
# a stale, marker-stamped resolver whose thread-local reads live in the
# old module. Reinstalling here keeps ``socket.getaddrinfo`` in lockstep
# with the reloaded module so ``_shim_installed()`` — the invariant the
# notification send fails closed on — holds after a reload, instead of
# silently reporting the shim as missing forever.
#
# This cannot chain shim-onto-shim: ``_real_getaddrinfo`` is captured via
# ``_capture_real_getaddrinfo``, which never returns a shim, so the newly
# installed shim's pass-through calls the genuine resolver, not the old
# shim.
if socket.getaddrinfo is not _pinned_getaddrinfo:
socket.getaddrinfo = _pinned_getaddrinfo
_installed = True
def _shim_installed() -> bool:
"""True iff THIS module's shim is still the active ``getaddrinfo``.
The pin and block-private guarantees only hold while
``socket.getaddrinfo`` is exactly this module's ``_pinned_getaddrinfo``:
the thread-local pin/block state lives here, so a wholesale replacement
after import (``gevent``/``eventlet`` monkeypatch, another socket shim,
or a stale post-reload shim) would consult different state and silently
drop the guard. The check is strict identity — a *different* shim, even
one stamped with our marker, does not satisfy it because it would read a
different ``_thread_state``. Callers on the send path treat a False here
as fail-closed rather than sending unguarded.
"""
return socket.getaddrinfo is _pinned_getaddrinfo
def _extract_scheme_host(
url: Optional[str],
) -> Tuple[Optional[str], Optional[str], bool]:
"""Return ``(scheme, normalized_host, is_ip_literal)`` for ``url``.
``scheme`` is lower-cased (``None`` when absent). ``host`` is ``None``
when the URL is empty or the parser rejects it. Uses
``urllib3.util.parse_url`` — the same parser ``requests`` uses
internally and the same one ``ssrf_validator.validate_url`` validates
against — so the host we pin is the host the client will resolve.
"""
if not url:
return None, None, False
try:
parsed = parse_url(url)
except (LocationParseError, ValueError):
return None, None, False
scheme = (parsed.scheme or "").lower() or None
host = _normalize_host(parsed.host)
if host is None:
return scheme, None, False
try:
ipaddress.ip_address(host)
return scheme, host, True # IP literal — no DNS to rebind
except ValueError:
return scheme, host, False
def _extract_host(url: Optional[str]) -> Tuple[Optional[str], bool]:
"""Return ``(normalized_host, is_ip_literal)`` for ``url``.
Thin wrapper over :func:`_extract_scheme_host` for callers that don't
need the scheme (e.g. :func:`pinned_request`).
"""
_scheme, host, is_literal = _extract_scheme_host(url)
return host, is_literal
def _resolve_and_validate(
host: str,
allow_localhost: bool,
allow_private_ips: bool,
block_link_local: bool = False,
) -> List[tuple]:
"""Resolve ``host`` once and validate every returned address.
Uses the real resolver (never the shim, to avoid recursion and to
ignore any pin) and applies exactly the ``is_ip_blocked`` policy that
``validate_url`` applies — honoring ``allow_localhost`` /
``allow_private_ips`` (and ``block_link_local`` for the notification
path) while the ``ALWAYS_BLOCKED_METADATA_IPS`` set keeps firing
regardless. This is the connect-time re-validation that the pin adds: an
answer that changed since ``validate_url`` ran is caught here.
Returns the addrinfo list to pin (all entries validated safe).
Raises:
ValueError: if any resolved address is blocked. SSRF-style message
so existing callers/tests catch it uniformly. NOTE: this is
raised ONLY for a confirmed SSRF block (below) — a resolver
failure is always routed through ``requests.ConnectionError``
instead, specifically so ``send()``'s ``except ValueError``
handler (which reports a confirmed security block) is never
reached by an ordinary resolution failure.
requests.ConnectionError: if the host no longer resolves at pin
time, if resolution times out (a slow / hostile DNS authority
cannot hang the send), OR if the host is unencodable to IDNA
(``socket.getaddrinfo`` raises ``UnicodeError`` — a
``UnicodeEncodeError`` is a ``ValueError`` subclass — for a
label over 63 bytes or an empty label). This particular catch
is currently unreachable in practice: it runs only for a
scheme in :data:`_PINNABLE_SCHEMES`, and those Apprise plugins
parse with ``verify_host=True``, so ``Apprise.add()`` rejects
an IDNA-unencodable host before ``_dispatch`` ever calls
``pin_hosts``. The catch keeps the
ValueError-means-SSRF-block invariant true at THIS layer
rather than relying on that upstream reject. (The SEND-time
sibling in :func:`_resolve_maybe_block` is genuinely
reachable — see its comment — via a non-pinnable scheme.)
Fail closed as an ordinary transport error so the existing
retry / ``RequestException`` handling stays intact.
"""
try:
addr_info = _getaddrinfo_bounded(
host, None, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, 0
)
except (socket.gaierror, UnicodeError) as exc:
raise requests.ConnectionError(
f"DNS resolution failed while pinning host {host}"
) from exc
if not addr_info:
raise requests.ConnectionError(
f"No addresses resolved while pinning host {host}"
)
for info in addr_info:
ip_str = str(info[4][0])
if ssrf_validator.is_ip_blocked(
ip_str,
allow_localhost=allow_localhost,
allow_private_ips=allow_private_ips,
block_link_local=block_link_local,
):
logger.warning(
"Blocked connect-time address for host {}: resolves to "
"internal/private/metadata IP {} "
"(resolve-vs-connect guard)",
host,
ip_str,
)
raise ValueError(
"URL failed security validation (possible SSRF): "
f"host {host} resolved to blocked IP {ip_str} at connect time"
)
return addr_info
@contextlib.contextmanager
def pinned_request(
url: str,
allow_localhost: bool = False,
allow_private_ips: bool = False,
block_link_local: bool = False,
) -> Iterator[Optional[List[tuple]]]:
"""Pin DNS for the duration of a single outbound request to ``url``.
Resolves the URL host ONCE, validates every returned address against
the SSRF policy, and pins those addresses so the connection made by
``requests`` / ``urllib3`` inside this context cannot be steered to a
rebind target: the address validated here is the address connected to.
``block_link_local`` is forwarded to the connect-time validation so a
caller that keeps link-local blocked under ``allow_private_ips`` (e.g.
``SafeSession(block_link_local=True)``) is not undone by a resolver
answer that moved into ``169.254.0.0/16`` between validation and
connect.
Yields the validated address list so the caller can apply any run-level
egress scope to the exact addresses that will be connected to. Yields
``None`` without pinning when the host is an IP literal (no DNS to
rebind — the caller's ``validate_url`` already vetted the literal) or
when the URL host cannot be extracted.
Must wrap every outbound request AND every redirect hop, so each hop
re-resolves, re-validates, and re-pins.
Raises:
ValueError: if the host resolves to a blocked address at connect
time.
requests.ConnectionError: if the host fails to resolve at pin time.
"""
host, is_literal = _extract_host(url)
if host is None or is_literal:
yield None
return
entry = _resolve_and_validate(
host,
allow_localhost,
allow_private_ips,
block_link_local=block_link_local,
)
pins = _get_pins()
# Save/restore any prior pin for this host so nested contexts (e.g. a
# redirect chain that loops back to the same host) are safe.
had_previous = host in pins
previous = pins.get(host)
pins[host] = entry
try:
yield entry
finally:
if had_previous:
pins[host] = previous
else:
pins.pop(host, None)
@contextlib.contextmanager
def block_private_resolution(
allow_localhost: bool = False,
allow_private_ips: bool = False,
block_link_local: bool = False,
) -> Iterator[None]:
"""Refuse UNPINNED lookups that resolve to a blocked address.
While this context is active on the calling thread, any host that is
NOT covered by an active pin and resolves to a private / loopback /
link-local / cloud-metadata address (per ``ssrf_validator.is_ip_blocked``
with the supplied flags) is refused at the ``getaddrinfo`` layer with a
``socket.gaierror``. Pinned hosts are unaffected (they short-circuit to
their validated addresses), and public unpinned hosts resolve normally.
``block_link_local`` (every notification partition): when True, the
whole link-local range stays refused even under ``allow_private_ips=True``
— closing metadata reachable in link-local beyond the always-blocked
literals without over-blocking RFC1918 /
loopback / non-link-local ULA self-hosted notifiers.
This closes redirect-to-internal / rebind-to-metadata for hosts that
were never pinned (e.g. a webhook that 302-redirects to
``169.254.169.254`` or ``127.0.0.1``): the block runs on the very
resolution the client is about to connect to, so there is no window to
race.
Thread-local: only the calling thread is affected, so a legitimate
private request in another thread is never disturbed. Windows nest
(inner replaces outer, restored on exit).
"""
prev = getattr(_thread_state, "block_private", None)
_thread_state.block_private = (
allow_localhost,
allow_private_ips,
block_link_local,
)
try:
yield
finally:
_thread_state.block_private = prev
@contextlib.contextmanager
def allow_resolution(
host: str,
port: int,
allow_localhost: bool = True,
allow_private_ips: bool = True,
block_link_local: bool = True,
) -> Iterator[None]:
"""Relax an active block-private window for one exact lookup.
While this context is active on the calling thread, an UNPINNED lookup
inside a :func:`block_private_resolution` window whose ``host`` string
and ``port`` exactly equal the given ones is checked with these flags
instead of the window's. It is still checked:
``ssrf_validator.ALWAYS_BLOCKED_METADATA_IPS`` stay refused whatever the
flags, and ``block_link_local`` (on by default) keeps the rest of the
link-local range refused too. Every other lookup keeps the window's
policy. Outside a window this has no effect, and a pinned host is
unaffected (pins short-circuit before the window is consulted).
Meant for a connection the caller deliberately makes to a fixed,
operator-configured endpoint, e.g. a forward proxy from
``HTTP(S)_PROXY``, whose address may legitimately be private. Match is
on the exact host string and port the client passes to
``getaddrinfo`` (no normalisation), so an IP-literal endpoint works
the same as a name. Thread-local; contexts nest and restore on exit.
"""
prev = getattr(_thread_state, "block_exemptions", None)
exemptions = dict(prev or {})
exemptions[(host, port)] = (
allow_localhost,
allow_private_ips,
block_link_local,
)
_thread_state.block_exemptions = exemptions
try:
yield
finally:
_thread_state.block_exemptions = prev
@contextlib.contextmanager
def pin_hosts(
urls,
allow_localhost: bool = False,
allow_private_ips: bool = False,
block_link_local: bool = False,
) -> Iterator[None]:
"""Pin the validated address of every pinnable host in ``urls``.
For each URL whose scheme is in :data:`_PINNABLE_SCHEMES` and whose host
is a real (non-literal) name, resolves once, validates every returned
address against the SSRF policy, and pins those addresses for the
duration of the context. IP literals, token-host plugin schemes, and
unparseable URLs are skipped (nothing to rebind, or the host is not a
resolvable name). A host that fails to resolve right now is also skipped
rather than aborting the whole batch — it is left unpinned so the
accompanying block-private window still governs its send-time lookup and
Apprise fails only that one URL.
``block_link_local`` is forwarded to the connect-time validation so a
raw-webhook host that resolves into link-local (metadata territory) is
refused even under ``allow_private_ips=True`` (notification lenient
partition).
Raises:
ValueError: if a pinnable host resolves to a blocked address (the
batch is refused — fail closed).
"""
pins = _get_pins()
saved: List[Tuple[str, bool, Optional[list]]] = []
pinned_now = set()
try:
for url in urls:
scheme, host, is_literal = _extract_scheme_host(url)
if scheme not in _PINNABLE_SCHEMES:
continue
if host is None or is_literal or host in pinned_now:
continue
try:
entry = _resolve_and_validate(
host,
allow_localhost,
allow_private_ips,
block_link_local=block_link_local,
)
except requests.ConnectionError:
# Skipping leaves the host unpinned, so its send-time
# lookup goes through ``_resolve_maybe_block``, which
# applies the same policy inside the block window. This
# nominally includes the IDNA-unencodable host
# (``UnicodeError`` routed to ``ConnectionError`` by
# ``_resolve_and_validate``), though that shape does not
# reach here today: for a pinnable scheme Apprise's
# ``add()`` rejects such a host first. The reachable
# IDNA route into ``_resolve_maybe_block`` is a scheme
# skipped by the ``_PINNABLE_SCHEMES`` check above.
logger.opt(lazy=True).debug(
"Skipping pin for currently-unresolvable host {}",
# (#6938: a request-supplied host is unbounded;
# redacted only when a DEBUG sink takes the line.)
lambda: redact_and_bound_for_log(host, 256),
)
continue
saved.append((host, host in pins, pins.get(host)))
pins[host] = entry
pinned_now.add(host)
yield
finally:
for host, had_previous, previous in reversed(saved):
if had_previous:
pins[host] = previous
else:
pins.pop(host, None)
class NotificationGuardUnavailableError(RuntimeError):
"""The DNS-pin shim required for a guarded notification send is not
installed as the active ``socket.getaddrinfo`` resolver.
Raised ONLY by :func:`pinned_notification_send` (never by
:func:`pinned_request`, the general-purpose safe_get/safe_post guard,
which does not depend on this pre-check). This is a deliberate
fail-closed SECURITY REFUSAL, not an incidental runtime error: the
pin/block-private window cannot be guaranteed without the shim, so the
send is refused outright.
Subclasses ``RuntimeError`` (not just ``Exception``) so any code that
still generically catches ``RuntimeError`` around a guarded send
continues to behave the same. It is a dedicated subclass — rather than
a bare ``RuntimeError`` — so ``notifications.service`` can catch this
specific refusal and re-raise it as its own ``SecurityBlockError``
(non-retryable, INVALID_URL classification) without this general
security module importing exception types from the higher-level
``notifications`` package (which would invert the module layering and
risk a circular import — ``notifications.service`` imports this
module).
"""
@contextlib.contextmanager
def pinned_notification_send(
urls,
allow_localhost: bool = False,
allow_private_ips: bool = False,
block_link_local: bool = False,
) -> Iterator[None]:
"""Guard a single in-thread Apprise ``notify()`` over ``urls``.
Combines :func:`pin_hosts` (pin every raw-webhook host in the batch to
its validated address) with :func:`block_private_resolution` (refuse any
unpinned lookup — a redirect target or a token-scheme endpoint — that
resolves to a blocked address). Both are thread-local, so the caller
MUST run the ``notify()`` synchronously in this thread
(``AppriseAsset(async_mode=False)``); an async fan-out would resolve in
worker threads that carry neither the pin nor the block.
``allow_private_ips`` is the operator flag for every notification
partition (HTTP, host-bearing plugins and fixed vendor endpoints). It
is public-only by default; with the opt-in, private targets and a
private forward proxy (``HTTPS_PROXY``) are allowed.
``block_link_local`` is True for every notification partition so the
whole link-local range stays blocked even when private LAN addresses
are operator-approved. It applies to both pin and send-time resolution.
Raises:
NotificationGuardUnavailableError: if the ``getaddrinfo`` shim is not
the active resolver (see :func:`_shim_installed`). The
pin/block cannot be guaranteed in that state, so the send is
REFUSED (fail closed) rather than proceeding unguarded. A
``RuntimeError`` subclass — see its docstring for why it is
not a bare ``RuntimeError``.
"""
# Fail closed if our shim is not installed: without it the pin registry
# and the block-private window are never consulted, so proceeding would
# send completely unguarded (silent fail-open). Refuse instead.
if not _shim_installed():
raise NotificationGuardUnavailableError(
"DNS pin shim is not the active socket.getaddrinfo; refusing the "
"notification send (fail closed). The resolve-vs-connect pin and "
"block-private window cannot be guaranteed — see "
"security.dns_pinning."
)
with contextlib.ExitStack() as stack:
stack.enter_context(
pin_hosts(
urls,
allow_localhost,
allow_private_ips,
block_link_local=block_link_local,
)
)
stack.enter_context(
block_private_resolution(
allow_localhost,
allow_private_ips,
block_link_local=block_link_local,
)
)
yield
# Install on import: safe_requests imports this module, so the shim is in
# place before any fetch. Transparent until a pin is active.
install()