~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 Validator for SSRF Prevention
Validates URLs to prevent Server-Side Request Forgery (SSRF) attacks
by blocking requests to internal/private networks and enforcing safe schemes.
"""
import ipaddress
import re
import socket
from contextvars import ContextVar
from urllib.parse import unquote, urlparse
from typing import Optional
from loguru import logger
from urllib3.exceptions import LocationParseError
from urllib3.util import parse_url
from .ip_ranges import PRIVATE_IP_RANGES as BLOCKED_IP_RANGES
from .log_sanitizer import redact_and_bound_for_log
from .ip_ranges import NAT64_PREFIXES
from .legacy_ipv4 import (
is_ambiguous_numeric_ipv4_host,
has_percent_encoded_numeric_ipv4_authority,
has_percent_escape_in_authority_host,
is_percent_encoded_numeric_ipv4_host,
)
# Set to True by ``validate_url`` when it refuses a URL ONLY because the
# local resolver raised ``socket.gaierror`` (every pre-DNS check had already
# passed). ``assert_base_url_safe`` resets it before calling ``validate_url``
# and reads it afterwards to choose its error message. It never affects
# whether a URL is accepted.
_REFUSED_FOR_UNRESOLVABLE_HOST: ContextVar[bool] = ContextVar(
"_REFUSED_FOR_UNRESOLVABLE_HOST", default=False
)
# Cloud-provider metadata endpoints — always blocked, even with
# allow_localhost=True or allow_private_ips=True. These IPs expose IAM /
# instance-role credentials and are never legitimate destinations.
#
# Entries are matched by canonical string form: is_ip_blocked parses the
# candidate with ipaddress.ip_address first, so any textual variant
# (uppercase / zero-padded / expanded IPv6) is normalized to the canonical
# form below before the membership test.
# nosec B104 - Hardcoded IPs are intentional for SSRF prevention
ALWAYS_BLOCKED_METADATA_IPS = frozenset(
{
"169.254.169.254", # AWS IMDSv1/v2, Azure, OCI, DigitalOcean
"169.254.170.2", # AWS ECS task metadata v3
"169.254.170.23", # AWS EKS Pod Identity credentials
"169.254.0.23", # Tencent Cloud
"100.100.100.200", # AlibabaCloud
# Oracle Compute Classic and Cloud at Customer expose instance
# metadata here. This address falls outside the explicit private
# and link-local ranges, so policy flags must never allow it.
"192.0.0.192", # Oracle Compute Classic IMDS
# Azure WireServer: the host-side endpoint Azure VMs use for
# provisioning and extension settings. It is a public-range address,
# so neither the private nor the link-local checks catch it.
"168.63.129.16", # Azure WireServer
# AWS native IPv6 IMDS endpoint. Documented by AWS as the IPv6
# instance-metadata address ([fd00:ec2::254]). It is a ULA
# (fc00::/7), NOT an IPv4-mapped / NAT64-wrapped form of
# 169.254.169.254, so the IPv4 entries above and the NAT64
# embedded-IPv4 check do not cover it — it must be listed
# explicitly or it stays reachable under allow_private_ips=True
# (which permits fc00::/7).
"fd00:ec2::254", # AWS IMDS over IPv6
"fd00:ec2::23", # AWS EKS Pod Identity credentials over IPv6
"fd20:ce::254", # GCP metadata server over IPv6
# Scaleway instance metadata (IPv4 and IPv6). The IPv4 address is
# link-local, which the LLM provider guard otherwise accepts (rootless
# Podman maps host.containers.internal into 169.254.0.0/16), so it
# must be listed explicitly to stay blocked there.
"169.254.42.42", # Scaleway instance metadata
"fd00:42::42", # Scaleway instance metadata over IPv6
# Further IPv6 metadata endpoints. The ULAs are permitted by
# allow_private_ips=True (fc00::/7) and fe80::a9fe:a9fe by the
# link-local allowance, so each is listed explicitly. Scope IDs
# (fe80::a9fe:a9fe%eth0) are stripped before the membership test.
"fd00:a9fe:a9fe::1", # Akamai / Linode metadata over IPv6
"fd00:c1::a9fe:a9fe", # Oracle Cloud (OCI) IMDS over IPv6
"fe80::a9fe:a9fe", # OpenStack Nova / Hetzner metadata over IPv6
}
)
# Cloud-provider metadata endpoints reachable by HOSTNAME — always blocked,
# under every flag combination, by every validator that classifies a
# destination host (``validate_url`` and therefore ``assert_base_url_safe`` /
# ``safe_requests``; the egress policy's ``evaluate_url``; the notification
# validator; the search-engine URL settings validator). This is the single
# source of truth; callers use ``is_metadata_hostname`` rather than keeping
# their own list.
#
# * ``metadata.google.internal`` / ``metadata.goog`` — GCP IMDS. These resolve
# to 169.254.169.254, so the IP block above already catches them after DNS;
# the name block is defense in depth (e.g. a hosts-file / resolver override).
# * ``api.service.softlayer.com`` — IBM Cloud classic infrastructure
# (SoftLayer) API, which serves ``SoftLayer_Resource_Metadata`` to classic
# instances over the private network. It resolves to ``10.0.80.88``, an
# RFC1918 address, so it is reachable whenever ``allow_private_ips=True``
# and the IP checks cannot stop it. ``10.0.80.88`` is deliberately NOT in
# ``ALWAYS_BLOCKED_METADATA_IPS``: an always-block on an RFC1918 address
# would break users who legitimately run a service (e.g. a local LLM
# server) at that address on their own LAN. KNOWN LIMIT: this is a name
# block only. A different DNS name (or a literal ``10.0.80.88``) that
# reaches the same service is NOT blocked under ``allow_private_ips=True``.
# (``161.26.0.6`` is NOT IBM metadata — it is IBM's shared package mirror /
# NTP address — and is not listed anywhere here.)
#
# Matching is exact on the normalized name (see ``is_metadata_hostname``), so
# subdomains such as ``metadata.google.internal.attacker.example`` are not
# matched by name; they are judged by what they resolve to.
ALWAYS_BLOCKED_METADATA_HOSTNAMES = frozenset(
{
"metadata.google.internal", # GCP IMDS
"metadata.goog", # GCP IMDS
"api.service.softlayer.com", # IBM Cloud classic (SoftLayer) metadata
}
)
# Characters IDNA treats as label separators (RFC 3490 section 3.1): the
# ASCII full stop plus the ideographic, fullwidth and halfwidth ideographic
# full stops. Mapped to "." before trailing-dot stripping so a trailing
# "\u3002" is as insignificant as a trailing ".".
_IDNA_DOT_TRANSLATION = str.maketrans(
{"\u3002": ".", "\uff0e": ".", "\uff61": "."}
)
def _idna_folded_names(name: str) -> Optional[list]:
"""ASCII forms a resolver or HTTP client may turn a non-ASCII ``name``
into, or None when no IDNA encoder can encode it.
* ``socket.getaddrinfo`` encodes a ``str`` host with Python's ``idna``
codec (IDNA 2003 nameprep). That codec folds fullwidth letters to
ASCII, treats the U+3002 / U+FF0E / U+FF61 full stops as ".", and
drops soft hyphens and zero-width characters, so
``api\u3002service\u3002softlayer\u3002com`` and
``\uff41\uff50\uff49.service.softlayer.com`` resolve as
``api.service.softlayer.com``. Code that hands such a name to the
resolver (raw sockets, Apprise plugins) reaches the listed host.
* urllib3 (and so ``requests``) and ``httpx`` encode the lower-cased
name with the ``idna`` package in IDNA 2008 mode, without the UTS #46
mapping. They refuse fullwidth letters and soft hyphens outright
(``httpx`` does accept the ideographic full stops as separators), but
they accept names the IDNA 2003 codec rejects, such as a
right-to-left label ending in a digit.
* The ``idna`` package's UTS #46 form is included as defence in depth
for any client that applies that mapping.
Every form an encoder produces is returned; an encoder that rejects the
name (or the absent ``idna`` package) adds none. Returns None only when
no encoder produces a form: no client can then look the name up, and
the caller fails CLOSED on that.
"""
folded = []
try:
folded.append(name.encode("idna").decode("ascii"))
except (UnicodeError, ValueError):
# The resolver cannot encode it; an HTTP client's IDNA 2008 encoder
# may still, so the forms below are what is left to check.
pass
try:
import idna as _idna_pkg
except ImportError: # pragma: no cover - idna ships with requests/httpx
_idna_pkg = None
if _idna_pkg is not None:
for encode in (
lambda n: _idna_pkg.encode(n.lower()),
lambda n: _idna_pkg.encode(n, uts46=True),
):
try:
folded.append(encode(name).decode("ascii"))
except Exception: # noqa: silent-exception - encoder rejects it
# This encoder refuses the name, so a client using it
# cannot connect to anything by it: no form to match.
pass
return folded or None
def is_metadata_hostname(host: object) -> bool:
"""Return True if ``host`` names an always-blocked cloud-metadata endpoint.
Normalizes the way the resolver / HTTP client would before matching:
percent-decoding, surrounding whitespace, case, IPv6 brackets, any
trailing root dots (``getaddrinfo`` ignores them) and, for a non-ASCII
name, every ASCII form an IDNA encoder produces for it
(``_idna_folded_names``): the folding ``getaddrinfo`` applies (stdlib
``idna`` codec, IDNA 2003), the ``idna`` package's IDNA 2008 encoding
that urllib3 and ``httpx`` use, and its UTS #46 mapping as defence in
depth. So ``API.Service.SoftLayer.com.``,
``api\u3002service\u3002softlayer\u3002com`` and fullwidth
``\uff41\uff50\uff49.service.softlayer.com`` all match
``api.service.softlayer.com``, while a public name that only IDNA 2008
accepts (a right-to-left label ending in a digit, which the IDNA 2003
codec rejects) is judged by the name those HTTP clients connect to.
Fails CLOSED: a non-ASCII name that none of those encoders can encode
returns True. No client can look such a name up through them, and one
with yet another encoder, or a proxy that resolves names itself, is
refused here deliberately. Non-string input returns False.
"""
if not isinstance(host, str):
return False
name = unquote(host).strip().lower()
if name.startswith("[") and name.endswith("]"):
name = name[1:-1]
name = name.translate(_IDNA_DOT_TRANSLATION).rstrip(".")
if name in ALWAYS_BLOCKED_METADATA_HOSTNAMES:
return True
if name.isascii():
# IDNA leaves ASCII labels unchanged apart from case, which is
# already folded, so no other spelling can reach a listed name.
return False
folded = _idna_folded_names(name)
if folded is None:
return True
return any(
f.lower().rstrip(".") in ALWAYS_BLOCKED_METADATA_HOSTNAMES
for f in folded
)
# Link-local ranges. When ``block_link_local`` is set (notification
# validation and send, egress-policy host classification, PRIVATE_ONLY
# content fetching, search-result full-content fetching and the
# content-cache check, and the request-boundary custom LLM endpoint check),
# these stay blocked even under ``allow_private_ips=True`` — cloud-provider
# metadata may live here beyond the always-blocked literals. The LLM
# provider guard (``assert_base_url_safe``) and the private search-engine
# instance-URL checks do not set it. Kept as a distinct list (a subset of
# the private ranges) so the carve-out is explicit and testable.
# Module-level (built once at import) rather than rebuilt on every
# ``is_ip_blocked`` call.
# nosec B104 - Hardcoded ranges are intentional for SSRF prevention
LINK_LOCAL_RANGES = [
ipaddress.ip_network("169.254.0.0/16"), # IPv4 link-local
ipaddress.ip_network("fe80::/10"), # IPv6 link-local
]
# Allowed URL schemes
ALLOWED_SCHEMES = {"http", "https"}
# Per-component cap for URL parts that reach a log line (#6938). The URLs
# this module logs are often request-supplied (e.g. the api router's
# add-resource body), and neither ``urlparse`` nor urllib3 bounds the
# scheme or host length, so a 100,000-char scheme or host would otherwise
# be written in full to every sink. ``redact_and_bound_for_log`` keeps
# ``max_length - 3`` chars plus ``...``, so a 256 cap never shortens a
# hostname within the 253-char DNS name limit.
_LOG_COMPONENT_MAX_CHARS = 256
# RFC 6052 §2.2 places the embedded IPv4 at prefix-length-specific byte
# positions — NOT always the trailing 32 bits. Only /96 puts the IPv4 in the
# low 32 bits; shorter prefixes split it around the reserved octet at byte 8.
# Reading the wrong bytes lets a crafted address (e.g. a /48 form) hide a real
# metadata/internal target at the RFC positions while a decoy sits in the
# trailing 32 bits. Map each supported prefix length to the four byte indices
# (of the 16-byte address) that hold the embedded IPv4, in order.
#
# All six RFC 6052 lengths are mapped on purpose, though NAT64_PREFIXES
# currently carries only /48 and /96 — so the 32/40/56/64 rows are unreachable
# today and untested. That is deliberate: adding a prefix to NAT64_PREFIXES
# should not also require editing this table, because a missing row here fails
# CLOSED but silently (extraction returns None, the caller blocks, and a
# legitimate NAT64 deployment simply stops resolving). A spec-complete table
# makes that impossible. Anything added here must come from RFC 6052 §2.2, not
# from inference.
_NAT64_V4_BYTE_INDICES = {
32: (4, 5, 6, 7),
40: (5, 6, 7, 9),
48: (6, 7, 9, 10),
56: (7, 9, 10, 11),
64: (9, 10, 11, 12),
96: (12, 13, 14, 15),
}
def nat64_embedded_ipv4(
ip: ipaddress._BaseAddress,
) -> Optional[ipaddress.IPv4Address]:
"""Return the IPv4 embedded in a NAT64-prefixed IPv6 address per RFC 6052
§2.2 (keyed on the matched prefix's length), or ``None`` if ``ip`` is not
inside a known NAT64 prefix (or the prefix length has no defined embedding).
Using the trailing 32 bits for every length misreads a non-/96 embedding —
see ``_NAT64_V4_BYTE_INDICES``. Centralized so ``is_ip_blocked`` and the
two wrap checks below extract identically and cannot drift.
Byte 8 — the octet RFC 6052 §2.2 reserves as zero for every embedding
shorter than /96 — is deliberately NOT validated: a malformed embedding is
policy-CHECKED, not rejected. Extraction reads the RFC byte positions
either way, so a crafted address still has its real embedded target
policy-checked (it cannot under-block). Rejecting malformed embeddings
would also fail closed (``None`` → the caller blocks), but would over-block
anything a lenient translator would in fact route.
"""
if not isinstance(ip, ipaddress.IPv6Address):
return None
packed = ip.packed
for nat64_prefix in NAT64_PREFIXES:
if ip in nat64_prefix:
idx = _NAT64_V4_BYTE_INDICES.get(nat64_prefix.prefixlen)
if idx is None:
return None
return ipaddress.IPv4Address(bytes(packed[i] for i in idx))
return None
def is_nat64_wrapped_metadata_ip(ip: ipaddress._BaseAddress) -> bool:
"""True iff ``ip`` is an IPv6 address inside a NAT64 prefix whose embedded
IPv4 (extracted per RFC 6052 §2.2) is in ``ALWAYS_BLOCKED_METADATA_IPS``.
Both ``is_ip_blocked`` and ``NotificationURLValidator._ip_matches_blocked_range``
consult this before honoring the ``security.allow_nat64`` operator
opt-in, so cloud-metadata access cannot be re-opened through an
IPv6-wrapped destination on a NAT64-equipped host.
"""
embedded_v4 = nat64_embedded_ipv4(ip)
return (
embedded_v4 is not None
and str(embedded_v4) in ALWAYS_BLOCKED_METADATA_IPS
)
def is_nat64_wrapped_link_local_ip(ip: ipaddress._BaseAddress) -> bool:
"""True iff ``ip`` is an IPv6 address inside a NAT64 prefix whose embedded
IPv4 (per RFC 6052 §2.2) is IPv4 link-local (``169.254.0.0/16``).
Mirrors ``is_nat64_wrapped_metadata_ip`` for the ``block_link_local``
notification guard. The ``security.allow_nat64`` opt-in re-opens general
IPv4 reachability via NAT64, but it must not re-open link-local — where
cloud-provider metadata may live beyond the always-blocked literals.
Consulted only when ``block_link_local`` is
set, so non-notification callers (which permit link-local under
``allow_private_ips``) keep the opt-in's reachability unchanged.
"""
embedded_v4 = nat64_embedded_ipv4(ip)
return embedded_v4 is not None and any(
isinstance(r, ipaddress.IPv4Network) and embedded_v4 in r
for r in LINK_LOCAL_RANGES
)
# RFC 3986 forbids these characters in URLs; their presence in a URL signals
# a parser-differential attempt (GHSA-g23j-2vwm-5c25). \s covers space, \t,
# \n, \r, \v, \f. Backslash is the load-bearing payload — Python's urlparse
# treats it as a literal char while requests/urllib3 treat it as a path
# delimiter, so a crafted URL like ``http://127.0.0.1\@1.1.1.1`` would
# pass the urlparse-based hostname check but actually connect to 127.0.0.1.
RFC_FORBIDDEN_URL_CHARS_RE = re.compile(r"[\\\s\x00-\x1f\x7f]")
def is_ip_blocked(
ip_str: str,
allow_localhost: bool = False,
allow_private_ips: bool = False,
allow_nat64: Optional[bool] = None,
block_link_local: bool = False,
) -> bool:
"""
Check if an IP address is in a blocked range.
Args:
ip_str: IP address as string
allow_localhost: Whether to allow localhost/loopback addresses
allow_private_ips: Whether to allow all private/internal IPs plus localhost.
This includes RFC1918 (10.x, 172.16-31.x, 192.168.x), CGNAT (100.64.x.x
used by Podman/rootless containers), link-local (169.254.x.x,
fe80::/10 -- unless ``block_link_local`` is set), and IPv6 ULA
(fc00::/7). Use for trusted self-hosted services such as an
LLM backend reached via ``host.containers.internal``.
Note: cloud metadata endpoints in ``ALWAYS_BLOCKED_METADATA_IPS``
(AWS / Azure / GCP / OCI / Scaleway / Akamai / OpenStack /
Hetzner / DigitalOcean / AlibabaCloud / Tencent / Oracle
Compute Classic) are ALWAYS blocked regardless of these flags.
block_link_local: When True, the entire link-local range — IPv4
``169.254.0.0/16`` and IPv6 ``fe80::/10`` — is treated as blocked
EVEN under ``allow_private_ips=True``. Set by the notification
validation and send paths (see ``security.dns_pinning`` /
``notification_validator``), egress-policy host
classification, PRIVATE_ONLY content fetching
(``egress.fetch``), search-result full-content fetching
(``full_search``) and the content-cache check
(``search_engine_base``), and the request-boundary custom LLM
endpoint check (``url_utils.is_safe_custom_llm_endpoint``):
those allow private LAN targets, but cloud-provider metadata
may live in link-local beyond the always-blocked literals, so
it stays blocked there. Has no effect unless
``allow_private_ips=True`` — without the opt-in link-local is
already blocked. Default False keeps link-local allowed under
the opt-in for callers that do not set it — notably the LLM
provider guard ``assert_base_url_safe`` (rootless Podman maps
``host.containers.internal`` to ``169.254.1.2``) and the
private search-engine instance-URL checks.
allow_nat64: Override for the ``security.allow_nat64`` carve-out.
``None`` (default) reads the env setting — the behavior every
existing caller relies on. An explicit ``bool`` answers a
hypothetical ("would enabling NAT64 unblock this?") without
mutating env; used by the notification "Test" admin hint to
decide whether to surface ``LDR_SECURITY_ALLOW_NAT64``. The
cloud-metadata always-block above fires first either way, so
this can never reopen IMDS.
Returns:
True if IP is blocked, False otherwise
"""
# Loopback ranges that can be allowed for trusted internal services
# nosec B104 - These hardcoded IPs are intentional for SSRF allowlist
LOOPBACK_RANGES = [
ipaddress.ip_network("127.0.0.0/8"), # IPv4 loopback
ipaddress.ip_network("::1/128"), # IPv6 loopback
]
# Private/internal network ranges - allowed with allow_private_ips=True
# nosec B104 - These hardcoded IPs are intentional for SSRF allowlist
PRIVATE_RANGES = [
# RFC1918 Private Ranges
ipaddress.ip_network("10.0.0.0/8"), # Class A private
ipaddress.ip_network("172.16.0.0/12"), # Class B private
ipaddress.ip_network("192.168.0.0/16"), # Class C private
# Container/Virtual Network Ranges
ipaddress.ip_network(
"100.64.0.0/10"
), # CGNAT - used by Podman/rootless containers
ipaddress.ip_network(
"169.254.0.0/16"
), # Link-local (cloud metadata IPs blocked separately via ALWAYS_BLOCKED_METADATA_IPS)
# IPv6 Private Ranges
ipaddress.ip_network("fc00::/7"), # IPv6 Unique Local Addresses
ipaddress.ip_network("fe80::/10"), # IPv6 Link-Local
]
try:
ip = ipaddress.ip_address(ip_str)
# Normalise an IPv6 zone identifier (``fd00:ec2::254%eth0``) away
# before any check: a scoped address compares unequal to the same
# unscoped literal, so the metadata set below would not match it.
if isinstance(ip, ipaddress.IPv6Address) and ip.scope_id:
ip = ipaddress.IPv6Address(ip.packed)
# Unwrap IPv4-mapped IPv6 addresses (e.g. ::ffff:127.0.0.1 → 127.0.0.1)
# These bypass IPv4 range checks if not converted.
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped:
ip = ip.ipv4_mapped
# ALWAYS block cloud-metadata endpoints - critical SSRF target
# for credential theft (AWS IMDS/ECS/EKS, Azure IMDS/WireServer,
# GCP, OCI, Scaleway, Akamai, OpenStack/Hetzner, DigitalOcean,
# AlibabaCloud, Tencent Cloud, Oracle Compute Classic). These are
# never legitimate destinations regardless of allow_localhost /
# allow_private_ips. Endpoints reached by NAME (GCP, IBM Cloud
# classic) are blocked by is_metadata_hostname, not here.
if str(ip) in ALWAYS_BLOCKED_METADATA_IPS:
return True
# Also block metadata IPs reached via NAT64 wrap. NAT64 prefixes
# embed the IPv4 destination in the low 32 bits; even when the
# operator has set LDR_SECURITY_ALLOW_NAT64=true the metadata
# block is "always" — an opt-in for IPv4 reachability does NOT
# license IMDS exposure.
if is_nat64_wrapped_metadata_ip(ip):
return True
# For the notification send path (``block_link_local``), a NAT64-wrapped
# link-local address must also stay blocked even under the allow_nat64
# opt-in — otherwise the carve-out below `continue`s past the link-local
# check for the IPv6-wrapped form of a link-local address.
# (When NAT64 is off the whole prefix is already blocked in the loop, so
# this only changes the opt-in case; gated on block_link_local so
# non-notification callers are unaffected.)
if block_link_local and is_nat64_wrapped_link_local_ip(ip):
return True
# Operator escape hatch for IPv6-only deployments using DNS64+NAT64.
# Read lazily (not at import) so test monkeypatching works and so the
# value is not cached across env mutations. Cloud-metadata IPs are
# ALWAYS blocked above, so this carve-out cannot reopen IMDS via
# the IPv6-wrapped form.
#
# allow_nat64 overrides the env read: None (default) preserves every
# existing caller; an explicit bool lets the notification "Test"
# admin hint ask "would LDR_SECURITY_ALLOW_NAT64=true unblock this?"
# without touching process env.
if allow_nat64 is None:
from ..settings.env_registry import get_env_setting
nat64_allowed = bool(get_env_setting("security.allow_nat64", False))
else:
nat64_allowed = allow_nat64
# Check if IP is in any blocked range
for blocked_range in BLOCKED_IP_RANGES:
if ip in blocked_range:
# NAT64 carve-out: when the operator has opted in, the two
# NAT64 prefixes don't block outright. 6to4 / Teredo / discard
# / IPv4-Compatible (::/96, except ::1) / IPv4-Translated
# (::ffff:0:0:0/96) remain blocked unconditionally.
if nat64_allowed and blocked_range in NAT64_PREFIXES:
# The opt-in permits reaching only what a DIRECT connection
# to the embedded IPv4 would be allowed to reach — it is not
# a blanket bypass of the private / loopback / link-local /
# metadata policy. Re-apply the same policy to the embedded
# IPv4: block if the direct form would be blocked, otherwise
# allow the NAT64 reach. This closes the gap where
# allow_nat64=True ALONE made NAT64-wrapped RFC1918 /
# loopback reachable regardless of allow_private_ips.
# (Metadata and, when block_link_local is set, link-local
# wraps are already blocked above.) The embedded IPv4 is
# extracted per RFC 6052 §2.2 (byte positions vary by prefix
# length) so a crafted /48 form cannot hide a blocked target
# at the RFC positions behind a decoy in the trailing 32
# bits; fail closed if extraction is impossible. Recursion
# terminates at depth 1: the embedded value is plain IPv4,
# never in a NAT64 prefix, and allow_nat64=False prevents
# re-entry.
embedded_v4 = nat64_embedded_ipv4(ip)
if embedded_v4 is None or is_ip_blocked(
str(embedded_v4),
allow_localhost=allow_localhost,
allow_private_ips=allow_private_ips,
allow_nat64=False,
block_link_local=block_link_local,
):
return True
continue
# If allow_private_ips is True, skip blocking for private + loopback
if allow_private_ips:
is_loopback = any(ip in lr for lr in LOOPBACK_RANGES)
is_private = any(ip in pr for pr in PRIVATE_RANGES)
# Notification path: link-local stays blocked even under
# the private-IP opt-in (metadata lives here beyond the
# always-blocked literals; no legitimate self-hosted
# notifier does). Fires before the private/loopback skip so
# it cannot be un-blocked by it. Metadata literals already
# returned True above, so this only governs the rest of the
# link-local range.
if block_link_local and any(
ip in llr for llr in LINK_LOCAL_RANGES
):
return True
if is_loopback or is_private:
continue
# If allow_localhost is True, skip blocking for loopback only
elif allow_localhost:
is_loopback = any(ip in lr for lr in LOOPBACK_RANGES)
if is_loopback:
continue
return True
return False
except ValueError:
# Invalid IP address
return False
def validate_url(
url: str,
allow_localhost: bool = False,
allow_private_ips: bool = False,
block_link_local: bool = False,
) -> bool:
"""
Validate URL to prevent SSRF attacks.
``block_link_local`` keeps the whole link-local range -- IPv4
``169.254.0.0/16`` and IPv6 ``fe80::/10``, plus the NAT64-wrapped form --
blocked EVEN under ``allow_private_ips=True``. It is a DEFENCE-IN-DEPTH
control: callers that set it are narrowing an intentionally permissive
``allow_private_ips`` so cloud instance metadata cannot hide inside it. It is forwarded to
``is_ip_blocked`` at both the literal-IP and the resolved-hostname check,
so a DNS name pointing at link-local is caught too. Off by default: callers
that legitimately accept private targets keep today's behaviour unless they
opt in.
Checks:
1. URL scheme is allowed (http/https only)
2. The host is not percent-encoded (any ``%`` outside a bracketed IP
literal is refused, whatever the private-IP policy)
3. Hostname is not a cloud-metadata hostname
(``ALWAYS_BLOCKED_METADATA_HOSTNAMES``, under every flag)
4. Hostname is not an internal/private IP address (the cloud-metadata
addresses in ``ALWAYS_BLOCKED_METADATA_IPS`` under every flag)
5. Hostname does not resolve to an internal/private IP
Args:
url: URL to validate
allow_localhost: Whether to allow localhost/loopback addresses.
Set to True for trusted internal services like self-hosted
search engines (e.g., searxng). Default False.
allow_private_ips: Whether to allow all private/internal IPs plus localhost.
This includes RFC1918 (10.x, 172.16-31.x, 192.168.x), CGNAT (100.64.x.x
used by Podman/rootless containers), link-local (169.254.x.x,
fe80::/10 -- unless ``block_link_local`` is set), and IPv6 ULA
(fc00::/7). Use for trusted self-hosted services such as an
LLM backend reached via ``host.containers.internal``.
Note: cloud metadata endpoints in ``ALWAYS_BLOCKED_METADATA_IPS``
(AWS / Azure / GCP / OCI / Scaleway / Akamai / OpenStack /
Hetzner / DigitalOcean / AlibabaCloud / Tencent / Oracle
Compute Classic) and the hostnames in
``ALWAYS_BLOCKED_METADATA_HOSTNAMES`` (GCP, IBM Cloud classic)
are ALWAYS blocked regardless of these flags.
Returns:
True if URL is safe, False otherwise
"""
if not isinstance(url, str):
return False
try:
url = url.strip()
# Layer 1: reject RFC-illegal characters that drive parser-differential
# attacks (backslash, whitespace, control bytes). The URL is omitted
# from this log line because userinfo (RFC 3986 §3.2.1) may contain
# credentials and rejected URLs are by definition adversarial-shaped.
if RFC_FORBIDDEN_URL_CHARS_RE.search(url):
logger.warning("Blocked URL containing RFC-illegal characters")
return False
parsed = urlparse(url)
# Check scheme
if parsed.scheme.lower() not in ALLOWED_SCHEMES:
logger.warning(
f"Blocked URL with invalid scheme: {redact_url_for_log(url)}"
)
return False
# urllib3 2.8 normalizes unreserved escapes in HTTP(S) hosts. Keep
# rejecting encoded numeric authorities before their spelling is
# lost, regardless of the private-address opt-in.
raw_hostname = parsed.hostname
if raw_hostname and is_percent_encoded_numeric_ipv4_host(raw_hostname):
logger.warning("Blocked URL with encoded numeric IPv4 host")
return False
# Layer 2: extract host using urllib3, the same parser ``requests``
# uses internally. ``urlparse`` and urllib3 disagree on URLs like
# ``http://127.0.0.1\@1.1.1.1`` — urlparse says ``1.1.1.1``,
# urllib3 says ``127.0.0.1``. Validating against urllib3 means the
# validator and the HTTP client cannot disagree on destination.
try:
u3 = parse_url(url)
except LocationParseError:
logger.warning("Blocked URL: urllib3 parser rejected it")
return False
# Encoded numeric IPv4 authorities are refused on the raw URL text,
# before the parsed host is looked at: urllib3 >= 2.8 percent-decodes
# http(s) hosts, so ``http://127%2e0%2e0%2e1/`` would otherwise parse
# to a plain IP literal and be accepted under allow_private_ips.
if has_percent_encoded_numeric_ipv4_authority(url):
logger.warning("Blocked URL with encoded numeric IPv4 host")
return False
# Any other percent escape in the host is refused too. urllib3 >= 2.8
# decodes ``ev%69l.example`` to ``evil.example`` while httpx (the LLM
# SDKs' client) resolves the escaped text unchanged, so checking the
# decoded host would vet a different name from the one those clients
# connect to. No real DNS name needs a percent escape.
if has_percent_escape_in_authority_host(url):
logger.warning("Blocked URL with percent-encoded host")
return False
hostname = u3.host
# Authority must be ASCII printable. urllib3 currently rejects
# non-ASCII via LocationParseError, but this guard keeps us
# independent of that staying constant — CVE-2019-9636 showed
# Python's stdlib loosened a similar restriction previously.
# Brackets/colon used in IPv6 hosts are within 0x20-0x7e, so this
# runs cleanly before bracket-strip.
if hostname and any(ord(c) < 0x20 or ord(c) > 0x7E for c in hostname):
logger.warning("Blocked URL with non-ASCII / control bytes in host")
return False
# Strip IPv6 brackets so ipaddress.ip_address can parse the host.
if hostname and hostname.startswith("[") and hostname.endswith("]"):
hostname = hostname[1:-1]
# rstrip(".") matches getaddrinfo behaviour — trailing dots are
# ignored at resolution time.
if hostname:
hostname = hostname.rstrip(".")
if not hostname:
logger.warning(
f"Blocked URL with no hostname: {redact_url_for_log(url)}"
)
return False
# Cloud-metadata endpoints reachable by NAME (GCP, IBM Cloud classic).
# Checked before the IP / DNS checks and independent of every flag:
# IBM's api.service.softlayer.com resolves to RFC1918, so the
# resolved-IP check below would pass it under allow_private_ips=True.
if is_metadata_hostname(hostname):
logger.warning(
f"Blocked URL to cloud-metadata hostname: {redact_url_for_log(url)}"
)
return False
# Check if hostname is an IP address
try:
ip = ipaddress.ip_address(hostname)
if is_ip_blocked(
str(ip),
allow_localhost=allow_localhost,
allow_private_ips=allow_private_ips,
block_link_local=block_link_local,
):
logger.warning(
f"Blocked URL with internal/private IP: {redact_url_for_log(url)}"
)
return False
except ValueError:
if is_percent_encoded_numeric_ipv4_host(hostname):
logger.warning("Blocked URL with encoded numeric IPv4 host")
return False
if is_ambiguous_numeric_ipv4_host(hostname):
logger.warning("Blocked URL with ambiguous numeric IPv4 host")
return False
# Resolve hostname to IP and check.
#
# NOTE: this is the validation-time check. On the ``safe_requests``
# path and in the PDF export resource fetcher
# (``web/services/pdf_service._SafeUrlFetcher``, urllib-based) it is
# now backed by ``security.dns_pinning``, which pins the address
# validated here (or re-resolved+re-validated at connect time) so
# the client connects to exactly that address rather than
# re-resolving the hostname independently — closing the
# resolve-vs-connect gap for those paths. Callers that hand the URL to
# an external client that re-resolves on its own (e.g. an LLM SDK,
# Apprise) still carry the residual window; see SECURITY.md.
try:
# Get all IP addresses for hostname
# nosec B104 - DNS resolution is intentional for SSRF prevention (checking if hostname resolves to private IP)
addr_info = socket.getaddrinfo(
hostname, None, socket.AF_UNSPEC, socket.SOCK_STREAM
)
for info in addr_info:
ip_str = str(
info[4][0]
) # Extract IP address from addr_info tuple
if is_ip_blocked(
ip_str,
allow_localhost=allow_localhost,
allow_private_ips=allow_private_ips,
block_link_local=block_link_local,
):
# The parsed host is not logged raw: for a userinfo
# holding an unencoded delimiter it is a credential
# fragment. redact_url_for_log applies the ambiguity rule.
logger.warning(
f"Blocked URL - host resolves to internal/private "
f"IP: {ip_str} - {redact_url_for_log(url)}"
)
return False
except socket.gaierror:
logger.warning(
f"Failed to resolve hostname: {redact_url_for_log(url)}"
)
_REFUSED_FOR_UNRESOLVABLE_HOST.set(True)
return False
except Exception:
logger.exception("Error during hostname resolution")
return False
# URL passes all checks
return True
except Exception:
logger.exception(f"Error validating URL {redact_url_for_log(url)}")
return False
def assert_base_url_safe(base_url: str, *, setting_key: str) -> str:
"""Validate an LLM provider base_url. Raises ValueError on SSRF.
Args:
base_url: The URL to validate.
setting_key: The settings dot-path that produced this URL
(e.g. ``"llm.ollama.url"``). Embedded into the error message
so operators know which setting to fix. Pass ``cls.url_setting``
from the OpenAI-compat parent or ``"llm.ollama.url"`` from
the Ollama provider — NEVER ``cls.provider_name`` which is a
display string ("xAI Grok", "llama.cpp") not a settings key.
Uses ``allow_localhost=True, allow_private_ips=True`` because the
legitimate destinations for LLM SDKs are localhost (Ollama, LM Studio,
llama.cpp) and RFC1918 (Docker / private network deployments).
Link-local (``169.254.0.0/16``, ``fe80::/10``) is deliberately NOT
excluded here, unlike the notification and content-fetch guards.
The link-local mapping is Podman's: rootless Podman 5.3+ (pasta) maps
``host.containers.internal`` to ``169.254.1.2``, and under Podman the
``host-gateway`` value the bundled docker-compose.yml uses for
``host.docker.internal`` resolves to that same address. (Docker's own
``host-gateway`` and Docker Desktop's ``host.docker.internal`` are not
link-local.) Excluding link-local would break host-side Ollama /
LM Studio backends under Podman. The
``ALWAYS_BLOCKED_METADATA_IPS`` set and the
``ALWAYS_BLOCKED_METADATA_HOSTNAMES`` name block (via ``validate_url``)
still fire under those flags and prevent the auth-gated SSRF that would
otherwise reach cloud-credential endpoints (AWS IMDS / ECS / EKS Pod
Identity, Azure IMDS and WireServer, GCP, OCI, Scaleway, Akamai /
Linode, OpenStack, DigitalOcean, AlibabaCloud, Tencent, Oracle Compute
Classic, and by name GCP and IBM Cloud classic). The IBM name block does
not cover another DNS name, or the literal RFC1918 address, that reaches
the same service. See SECURITY.md ("LLM Provider URL Validation").
Residual risk (documented, not closed here): this guard validates once
at provider construction, and the LLM SDK re-resolves the hostname on
every inference call through its own HTTP client. Unlike the
``safe_requests`` path — which pins the validated address via
``security.dns_pinning`` so the connection cannot be re-steered — the
SDK exposes no resolver/adapter seam to pin without patching its
internals, so the resolve-vs-connect window remains. Egress restriction
at the firewall is the operator-side mitigation. See SECURITY.md
("LLM Provider URL Validation").
The hostname must resolve through the LOCAL resolver: ``validate_url``
fails closed when ``getaddrinfo`` cannot resolve it, even when the
eventual request would go through an HTTP(S) proxy that resolves the
name itself. That case raises a distinct "could not resolve host"
message so it is not mistaken for a blocked destination. The message
is chosen only when ``validate_url`` refused SOLELY because its own
``getaddrinfo`` call raised ``socket.gaierror``, after every pre-DNS
check (scheme, forbidden characters, encoded or ambiguous numeric
IPv4 host, IP literal) had passed; every other refusal keeps the
generic "refusing to send" message. No second DNS lookup is made.
Under a proxy the check is advisory: the proxy does its own
resolution and connects to whatever address it gets, which need not
be the one validated here.
"""
token = _REFUSED_FOR_UNRESOLVABLE_HOST.set(False)
try:
is_safe = validate_url(
base_url, allow_localhost=True, allow_private_ips=True
)
unresolvable = _REFUSED_FOR_UNRESOLVABLE_HOST.get()
finally:
_REFUSED_FOR_UNRESOLVABLE_HOST.reset(token)
if not is_safe:
if unresolvable:
raise ValueError(
f"base_url failed SSRF validation: could not resolve host "
f"for SSRF validation (the host must resolve locally, even "
f"when a proxy is configured). Check {setting_key} config "
f"and DNS."
)
raise ValueError(
f"base_url failed SSRF validation: refusing to send "
f"inference traffic. Check {setting_key} config."
)
return base_url
def get_safe_url(
url: Optional[str], default: Optional[str] = None
) -> Optional[str]:
"""
Get URL if it's safe, otherwise return default.
Args:
url: URL to validate
default: Default value if URL is unsafe
Returns:
URL if safe, default otherwise
"""
if not url:
return default
if validate_url(url):
return url
logger.warning(f"Unsafe URL rejected: {redact_url_for_log(url)}")
return default
#: Schemes ``redact_url_for_log`` may echo for a host-less URL. Anything
#: else may be the username of a scheme-less ``user:pass@host`` string.
_HOSTLESS_LOGGABLE_SCHEMES = frozenset(
{
"http",
"https",
"file",
"ftp",
"data",
"javascript",
"mailto",
"ws",
"wss",
}
)
#: Leading characters both parsers skip before the scheme: ``urlsplit``
#: strips C0 controls and space, ``redact_url_for_log`` strips Unicode
#: whitespace before ``parse_url``.
_URL_LEADING_STRIP_RE = re.compile(r"^[\x00-\x20\s]*")
#: An optional scheme followed by ``//``: where a parsed authority begins.
_RAW_AUTHORITY_START_RE = re.compile(r"(?:[A-Za-z][A-Za-z0-9+.\-]*:)?//")
#: Characters ``urlsplit`` deletes anywhere in a URL before parsing.
_URL_REMOVED_CHARS = str.maketrans("", "", "\t\r\n")
def _split_raw_authority(url: str, delimiters: str) -> tuple[str, str]:
"""Split *url* as written into ``(authority, rest)``.
See ``_raw_after_authority`` for how the authority is delimited.
"""
s = url.translate(_URL_REMOVED_CHARS)
s = s[_URL_LEADING_STRIP_RE.match(s).end() :]
match = _RAW_AUTHORITY_START_RE.match(s)
start = match.end() if match else 0
ends = [i for i in (s.find(d, start) for d in delimiters) if i >= 0]
end = min(ends) if ends else len(s)
return s[start:end], s[end:]
def _raw_authority_has_userinfo(url: str, delimiters: str) -> bool:
"""Whether the authority of *url*, as written, holds an ``@``.
``urllib3.parse_url`` sets ``auth = auth or None``, so an authority
that starts with ``@`` (an empty userinfo, as in
``https://@john.doe/s3cret@host/``) parses with ``auth is None``;
this check counts it as a userinfo, as the ``urlparse``-based
siblings already do.
"""
return "@" in _split_raw_authority(url, delimiters)[0]
def _raw_after_authority(url: str, delimiters: str) -> str:
"""Return the text of *url* as written after its authority.
The authority runs from the ``//`` after the scheme to the first of
*delimiters* (``/?#`` for ``urlsplit``; ``urllib3.parse_url`` also
ends it at ``\\``). This is taken from the raw string, never from the
parsed path: for http(s) ``parse_url`` removes dot segments, so the
path of ``https://tok/en@host/../a`` parses to ``/a`` and the ``@``
that marks ``tok`` as a userinfo fragment would be gone. Without a
``//`` the search starts at the beginning of the string, which can
only include more text (fail closed).
"""
return _split_raw_authority(url, delimiters)[1]
def authority_may_be_userinfo(
host: str,
has_port: bool,
url: str,
*,
has_userinfo: bool = False,
delimiters: str = "/?#",
) -> bool:
"""Whether a parsed ``host[:port]`` may really be a userinfo prefix.
Shared by ``redact_url_for_log``, ``url_authority_without_userinfo``
and the downloaders' ``rate_limit_authority``. *url* is the raw URL
the authority was parsed from and *delimiters* the characters that
end the authority for that parser; whether an ``@`` follows the
authority is decided on the URL as written (``_raw_after_authority``),
so a dot segment (``https://tok/en@host/../a``, which ``parse_url``
normalises to the path ``/a``) cannot hide it.
A userinfo holding an unencoded ``/``, ``?``, ``#`` (or, for urllib3,
``\\``) ends the authority early, so ``https://tok/en@host/`` parses
to the host ``tok`` and ``https://john.doe:1234#x@host/`` to
``john.doe:1234``. That shape always leaves an ``@`` after the parsed
authority. With one there, the authority is treated as ambiguous
when:
* the parsed authority itself carried a userinfo (*has_userinfo*):
a password holding an unencoded ``@`` *and* a later delimiter
(``https://admin:[email protected]#1@host/``) is split at its own ``@``,
so the "host" (``ss.example``) is the password's tail -- this applies
to every host, ``localhost`` and IP literals included;
* the host is a single label (no ``.``), or
* a port was parsed -- it may be the leading digits of a password,
the last two unless the host is ``localhost`` or an IP literal, which
are not plausible usernames (``http://localhost:8080/?email=a@b``
stays). A real credentialed URL with an ``@`` in its path or query
(``https://u:[email protected]/@group``) therefore fails closed.
Residual: a userinfo whose text before its first ``/``, ``?`` or
``#`` holds no ``@`` and is either a dotted name with no
``:digits`` port (``https://john.doe/x@host/``,
``https://john.doe:#pw@host/``) or ``localhost`` / an IP literal
with or without one (``https://127.0.0.1:80/x@host/``) is
indistinguishable from a real host followed by an ``@`` in the path
(``https://medium.com/@user``) and is not flagged. Only that text
(a username, plus a digits-only password prefix for the IP/localhost
case) can surface.
"""
if "@" not in _raw_after_authority(url, delimiters):
return False
if has_userinfo:
return True
bare = host.strip("[]")
if host.startswith("[") or bare.lower() == "localhost":
return False
try:
ipaddress.IPv4Address(bare)
return False
except ValueError:
pass
return has_port or "." not in host
def redact_url_for_log(url: str) -> str:
"""Return ``scheme://host:port`` (no userinfo, path, query, fragment).
For log output only. Drops everything except scheme + authority host
+ port to minimise the chance of leaking credentials, tokens, or
sensitive paths into logs while still giving operators enough to
distinguish ``http://10.0.0.1:80`` from ``https://10.0.0.1:443``.
The scheme and the host each pass through ``redact_and_bound_for_log``
capped at ``_LOG_COMPONENT_MAX_CHARS``: the log patcher's credential
redaction runs first, then control characters are stripped and an
over-long component is cut with a ``...`` marker, so the result stays
bounded for any input. A component whose first whitespace-free run
reaches past the 1,024 characters ``redact_and_bound_for_log`` scans
is replaced by the patcher's omitted-length marker, as a run past the
patcher's own 32 KiB bound would be in any log line.
RFC 3986 §3.2.1 allows credentials in URL userinfo
(``http://user:pass@host/``). A rejected URL is by definition
adversarial-shaped, but it may still carry the operator's real
credentials if a misconfiguration produced it.
Total by design: it runs inside failure handlers, so it never raises.
A non-``str`` input yields ``"<unparseable:TYPE>"`` (the egress audit
log's placeholder) and any parse error yields ``"<unparseable>"``.
When no host parses, the "scheme" is only kept if it is a well-known
one: in a scheme-less ``user:pass@host`` the parser reads the
username as the scheme, so an unknown one becomes ``?``.
When the parsed authority may be a userinfo prefix (see
``authority_may_be_userinfo``: ``https://tok/en@host/`` parses to the
host ``tok``), the authority is replaced by ``<redacted>``.
"""
if not isinstance(url, str):
return f"<unparseable:{type(url).__name__}>"
try:
# requests strips leading whitespace before it fetches; parse_url does
# not, and would put the scheme in the host slot.
u = parse_url(url.lstrip())
if not u.host:
scheme = (u.scheme or "").lower()
if scheme not in _HOSTLESS_LOGGABLE_SCHEMES:
scheme = "?"
return f"{scheme}://<no-host>"
scheme = u.scheme or "?"
# The raw URL, not u.path: parse_url removes dot segments.
if authority_may_be_userinfo(
u.host,
u.port is not None,
url,
# Also the raw text: parse_url drops an empty userinfo
# (``https://@john.doe/x@host/`` has ``auth is None``).
has_userinfo=u.auth is not None
or _raw_authority_has_userinfo(url, "/?#\\"),
delimiters="/?#\\",
):
if scheme.lower() not in _HOSTLESS_LOGGABLE_SCHEMES:
scheme = "?"
return f"{scheme}://<redacted>"
host = u.host or "<no-host>"
# Scheme and host are unbounded in a request-supplied URL; preserve
# the preview cap after excluding ambiguous userinfo authorities.
scheme = redact_and_bound_for_log(scheme, _LOG_COMPONENT_MAX_CHARS)
host = redact_and_bound_for_log(host, _LOG_COMPONENT_MAX_CHARS)
host_port = f"{host}:{u.port}" if u.port else host
return f"{scheme}://{host_port}"
except Exception: # never raise from a log-formatting helper
return "<unparseable>"
def url_authority_without_userinfo(url: object) -> str:
"""Return a URL's ``host[:port]`` with any userinfo removed.
For display and keying (library domain filters, domain-classifier
keys, rate-limit keys, failure messages). For an ordinary URL the
result is exactly ``urllib.parse.urlparse(url).netloc``, case
preserved, so keys derived from it stay stable; a ``user:pass@host``
netloc yields ``host``.
Returns ``""`` (never raises) when no credential-free authority can
be determined:
* a non-``str`` or empty input (``urlparse(None)`` yields bytes) or a
parse error;
* a port that is not a valid port number. A password holding an
unencoded ``/``, ``?`` or ``#`` ends the netloc early, so
``https://alice:pa#ss@host/`` parses to the netloc ``alice:pa``,
whose "port" is the start of the password;
* an authority followed by an ``@`` later in the URL that itself
carried a userinfo, or whose host is a single label or which
carries a port (for the last two, ``localhost`` and IP literals
excepted): the shape a userinfo holding one of those delimiters
leaves behind (``https://tok/en@host/`` parses to the netloc
``tok``, ``https://user:12#34@host/`` to ``user:12``,
``https://john.doe:1234/x@host/`` to ``john.doe:1234`` and
``https://admin:[email protected]#1@host/`` to ``admin:[email protected]``).
See ``authority_may_be_userinfo``, which ``redact_url_for_log``
and ``rate_limit_authority`` share.
Residual: see ``authority_may_be_userinfo`` -- a dotted username
directly followed by the delimiter (``https://john.doe/x@host/``) is
returned as parsed.
"""
if not isinstance(url, str) or not url:
return ""
try:
parsed = urlparse(url)
userinfo, at, authority = parsed.netloc.rpartition("@")
if not authority:
return ""
# ``.port`` raises ValueError for a non-numeric or out-of-range
# port; that "port" may be the start of a password.
port = parsed.port
if authority.startswith("["):
host = authority.partition("]")[0] + "]"
else:
host = authority.partition(":")[0]
if authority_may_be_userinfo(
host, port is not None, url, has_userinfo=bool(at)
):
return ""
return authority
except Exception: # never raise from a display/keying helper
return ""