~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
"""Tests for notification_validator module - notification service URL validation."""
from unittest.mock import patch
import socket
import pytest
from local_deep_research.security.notification_validator import (
NotificationURLValidationError,
NotificationURLValidator,
parse_notification_url_list,
)
class TestNotificationURLValidationError:
"""Tests for NotificationURLValidationError exception."""
def test_inherits_from_value_error(self):
"""Should inherit from ValueError."""
assert issubclass(NotificationURLValidationError, ValueError)
def test_can_be_raised_with_message(self):
"""Should be raisable with a message."""
with pytest.raises(
NotificationURLValidationError, match="test message"
):
raise NotificationURLValidationError("test message")
class TestIsPrivateIP:
"""Tests for _is_private_ip static method."""
def test_localhost_string(self):
"""Should detect 'localhost' as private."""
assert NotificationURLValidator._is_private_ip("localhost") is True
def test_localhost_uppercase(self):
"""Should detect 'LOCALHOST' as private (case-insensitive)."""
assert NotificationURLValidator._is_private_ip("LOCALHOST") is True
def test_loopback_ipv4(self):
"""Should detect 127.0.0.1 as private."""
assert NotificationURLValidator._is_private_ip("127.0.0.1") is True
def test_loopback_ipv6(self):
"""Should detect ::1 as private."""
assert NotificationURLValidator._is_private_ip("::1") is True
def test_all_zeros_ipv4(self):
"""Should detect 0.0.0.0 as private."""
assert NotificationURLValidator._is_private_ip("0.0.0.0") is True
def test_all_zeros_ipv6(self):
"""Should detect :: as private."""
assert NotificationURLValidator._is_private_ip("::") is True
def test_private_10_range(self):
"""Should detect 10.x.x.x as private."""
assert NotificationURLValidator._is_private_ip("10.0.0.1") is True
assert NotificationURLValidator._is_private_ip("10.255.255.255") is True
def test_private_172_range(self):
"""Should detect 172.16-31.x.x as private."""
assert NotificationURLValidator._is_private_ip("172.16.0.1") is True
assert NotificationURLValidator._is_private_ip("172.31.255.255") is True
def test_private_192_range(self):
"""Should detect 192.168.x.x as private."""
assert NotificationURLValidator._is_private_ip("192.168.0.1") is True
assert (
NotificationURLValidator._is_private_ip("192.168.255.255") is True
)
def test_link_local_ipv4(self):
"""Should detect link-local 169.254.x.x as private."""
assert NotificationURLValidator._is_private_ip("169.254.1.1") is True
def test_public_ipv4(self):
"""Should not detect public IPs as private."""
assert NotificationURLValidator._is_private_ip("8.8.8.8") is False
assert NotificationURLValidator._is_private_ip("1.1.1.1") is False
assert NotificationURLValidator._is_private_ip("93.184.216.34") is False
def test_hostname_resolving_to_public_ip(self):
"""Should return False for hostnames that resolve to public IPs."""
fake_result = [
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
]
with patch("socket.getaddrinfo", return_value=fake_result):
assert (
NotificationURLValidator._is_private_ip("example.com") is False
)
def test_hostname_resolving_to_private_ip(self):
"""Should return True for hostnames that resolve to private IPs."""
fake_result = [
(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 0)),
]
with patch("socket.getaddrinfo", return_value=fake_result):
assert (
NotificationURLValidator._is_private_ip("evil.example.com")
is True
)
def test_hostname_dns_failure_returns_false(self):
"""Should return False when DNS resolution fails."""
with patch(
"socket.getaddrinfo",
side_effect=socket.gaierror("Name not resolved"),
):
assert (
NotificationURLValidator._is_private_ip("nonexistent.invalid")
is False
)
class TestValidateServiceUrl:
"""Tests for validate_service_url static method."""
def test_empty_url_rejected(self):
"""Should reject empty URLs."""
is_valid, error = NotificationURLValidator.validate_service_url("")
assert is_valid is False
assert "non-empty string" in error
def test_none_url_rejected(self):
"""Should reject None URLs."""
is_valid, error = NotificationURLValidator.validate_service_url(None)
assert is_valid is False
assert "non-empty string" in error
def test_non_string_url_rejected(self):
"""Should reject non-string URLs."""
is_valid, error = NotificationURLValidator.validate_service_url(123)
assert is_valid is False
assert "non-empty string" in error
def test_url_without_scheme_rejected(self):
"""Should reject URLs without protocol scheme."""
is_valid, error = NotificationURLValidator.validate_service_url(
"example.com/webhook"
)
assert is_valid is False
assert "must have a protocol" in error
def test_parse_error_does_not_leak_exception_text(self):
"""A urlparse failure must return a generic message, not the
exception text. The validator error is surfaced to the user by the
/api/notifications/test-url endpoint, so leaking the exception would
expose stack-trace fragments (CWE-209, CodeQL py/stack-trace-exposure,
alert #4775)."""
secret_marker = "INTERNAL-PARSER-DETAIL-do-not-leak"
with patch(
"local_deep_research.security.notification_validator.urlparse",
side_effect=ValueError(secret_marker),
):
is_valid, error = NotificationURLValidator.validate_service_url(
"https://example.com/webhook"
)
assert is_valid is False
assert secret_marker not in error
assert error == "Invalid URL format"
def test_parse_error_real_input_does_not_leak(self):
"""Real-input companion to the mocked test: an unbalanced IPv6
bracket makes the stdlib urlparse raise ``ValueError: Invalid IPv6
URL``, which must surface as the generic message. Guards the reachable
path against a refactor that stops calling urlparse (or a future
CPython that stops raising) — something the mocked test cannot catch."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://[::1"
)
assert is_valid is False
assert error == "Invalid URL format"
def test_file_scheme_blocked(self):
"""Should block file:// scheme."""
is_valid, error = NotificationURLValidator.validate_service_url(
"file:///etc/passwd"
)
assert is_valid is False
assert "Blocked unsafe protocol" in error
assert "file" in error
def test_ftp_scheme_blocked(self):
"""Should block ftp:// scheme."""
is_valid, error = NotificationURLValidator.validate_service_url(
"ftp://ftp.example.com"
)
assert is_valid is False
assert "Blocked unsafe protocol" in error
def test_javascript_scheme_blocked(self):
"""Should block javascript: scheme."""
is_valid, error = NotificationURLValidator.validate_service_url(
"javascript:alert(1)"
)
assert is_valid is False
assert "Blocked unsafe protocol" in error
def test_data_scheme_blocked(self):
"""Should block data: scheme."""
is_valid, error = NotificationURLValidator.validate_service_url(
"data:text/html,<script>alert(1)</script>"
)
assert is_valid is False
assert "Blocked unsafe protocol" in error
def test_unknown_scheme_rejected(self):
"""Should reject unknown/unsupported schemes."""
is_valid, error = NotificationURLValidator.validate_service_url(
"custom://example.com"
)
assert is_valid is False
assert "Unsupported protocol" in error
def test_https_valid(self):
"""Should accept https:// URLs to public hosts."""
is_valid, error = NotificationURLValidator.validate_service_url(
"https://webhook.example.com/notify"
)
assert is_valid is True
assert error is None
def test_http_valid(self):
"""Should accept http:// URLs to public hosts."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://webhook.example.com/notify"
)
assert is_valid is True
assert error is None
def test_discord_scheme_valid(self):
"""Should accept discord:// URLs."""
is_valid, error = NotificationURLValidator.validate_service_url(
"discord://webhook_id/webhook_token"
)
assert is_valid is True
assert error is None
def test_slack_scheme_valid(self):
"""Should accept slack:// URLs."""
is_valid, error = NotificationURLValidator.validate_service_url(
"slack://token_a/token_b/token_c"
)
assert is_valid is True
assert error is None
def test_telegram_scheme_rejected_as_unsupported(self):
is_valid, error = NotificationURLValidator.validate_service_url(
"telegram://bot_token/chat_id"
)
assert is_valid is False
assert "no longer supported" in error
assert "tgram://" in error
def test_tgram_scheme_valid(self):
"""Should accept tgram:// URLs (regression for #5399)."""
is_valid, error = NotificationURLValidator.validate_service_url(
"tgram://123456789:Token_abc-123/987654321"
)
assert is_valid is True
assert error is None
def test_mailto_scheme_valid(self):
"""Should accept mailto: URLs."""
is_valid, error = NotificationURLValidator.validate_service_url(
"mailto://[email protected]"
)
assert is_valid is True
assert error is None
def test_ntfy_scheme_valid(self):
"""Should accept ntfy:// URLs."""
is_valid, error = NotificationURLValidator.validate_service_url(
"ntfy://topic"
)
assert is_valid is True
assert error is None
def test_ntfys_scheme_valid(self):
"""Should accept ntfys:// URLs (HTTPS variant of ntfy)."""
is_valid, error = NotificationURLValidator.validate_service_url(
"ntfys://topic"
)
assert is_valid is True
assert error is None
def test_http_localhost_blocked(self):
"""Should block http://localhost by default."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://localhost:5000/webhook"
)
assert is_valid is False
assert "Blocked private/internal IP" in error
def test_http_127_blocked(self):
"""Should block http://127.0.0.1 by default."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://127.0.0.1/webhook"
)
assert is_valid is False
assert "Blocked private/internal IP" in error
def test_http_private_ip_blocked(self):
"""Should block http to private IPs by default."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://192.168.1.100/webhook"
)
assert is_valid is False
assert "Blocked private/internal IP" in error
def test_http_localhost_allowed_with_flag(self):
"""Should allow localhost when allow_private_ips=True."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://localhost:5000/webhook", allow_private_ips=True
)
assert is_valid is True
assert error is None
def test_http_private_ip_allowed_with_flag(self):
"""Should allow private IPs when allow_private_ips=True."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://192.168.1.100/webhook", allow_private_ips=True
)
assert is_valid is True
assert error is None
def test_whitespace_stripped(self):
"""Should strip whitespace from URL."""
is_valid, error = NotificationURLValidator.validate_service_url(
" https://example.com/webhook "
)
assert is_valid is True
assert error is None
class TestParserDifferentialBypass:
"""
Tests for the parser-differential SSRF bypass (GHSA-g23j-2vwm-5c25)
in the notification flow. The same bypass that affected
``ssrf_validator.validate_url`` also affected
``NotificationURLValidator.validate_service_url`` because both used
``urlparse(url).hostname`` for the SSRF check.
"""
def test_advisory_canonical_payload_blocked(self):
is_valid, error = NotificationURLValidator.validate_service_url(
"http://127.0.0.1:6666\\@1.1.1.1"
)
assert is_valid is False
assert error is not None
def test_post_prepare_canonicalised_form_blocked(self):
"""Layer-2 verification on the notification flow."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://127.0.0.1:6666/%[email protected]"
)
assert is_valid is False
assert error is not None
assert "127.0.0.1" in error # Layer 2 reports the actual host
def test_backslash_no_port(self):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://127.0.0.1\\@1.1.1.1"
)
assert is_valid is False
def test_tab_in_url_blocked(self):
is_valid, _ = NotificationURLValidator.validate_service_url(
"https://example.com/path\there"
)
assert is_valid is False
def test_null_byte_blocked(self):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://127.0.0.1\x00@1.1.1.1"
)
assert is_valid is False
def test_apprise_discord_still_works(self):
is_valid, error = NotificationURLValidator.validate_service_url(
"discord://webhook_id/token"
)
assert is_valid is True
assert error is None
def test_apprise_slack_still_works(self):
is_valid, error = NotificationURLValidator.validate_service_url(
"slack://TestApp@TokenA/TokenB/TokenC"
)
assert is_valid is True
assert error is None
def test_apprise_mailto_with_credentials(self):
is_valid, error = NotificationURLValidator.validate_service_url(
"mailto://user:[email protected]"
)
assert is_valid is True
assert error is None
def test_multi_at_authority_rejected(self):
"""A second '@' in the authority is a parser-differential SSRF:
urllib3 (the validator/pin parser) reads the host after the LAST
'@' (``decoy-public.example.com``) while Apprise's own parser reads
``169.254.169.254`` and connects there. Reject any authority with
more than one '@' before the host is ever extracted (fail closed)."""
is_valid, error = NotificationURLValidator.validate_service_url(
"json://[email protected]@decoy-public.example.com/path",
allow_private_ips=True,
)
assert is_valid is False
assert error is not None
assert "@" in error
def test_multi_at_authority_rejected_http_scheme(self):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://a@[email protected]/x"
)
assert is_valid is False
def test_single_at_userinfo_still_allowed(self):
"""A single '@' (RFC 3986 userinfo) is legitimate and must not be
collateral damage of the multi-'@' rejection."""
for url in (
"json://[email protected]/path",
"json://user:[email protected]/path",
"mailto://user:[email protected]",
):
is_valid, error = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is True, (url, error)
def test_at_in_query_not_counted(self):
"""The multi-'@' check counts only the authority (urlparse netloc),
so an '@' in the query string is not a rejection cause."""
is_valid, error = NotificationURLValidator.validate_service_url(
"json://hook.example.com/path?to=a@b@c", allow_private_ips=True
)
assert is_valid is True, error
def test_apprise_signal_url_accepted(self):
"""signal:// (Apprise's Signal-API-REST transport) is allowed.
Regression test for #4006: the validator previously rejected the
Signal scheme with "Unsupported protocol". Private signal-api-rest
hosts require the operator's private-IP opt-in.
"""
is_valid, error = NotificationURLValidator.validate_service_url(
"signal://192.168.50.20:8739/+15551234567/+15557654321",
allow_private_ips=True,
)
assert is_valid is True
assert error is None
def test_ipv6_unspecified_blocked(self):
"""``::`` (and equivalent forms) routes to local host on Linux."""
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://[::]/"
)
assert is_valid is False
def test_ipv6_unspecified_zero_form_blocked(self):
"""``0::`` bypasses the literal-string ``::`` allow-list at
``_is_private_ip`` — must be caught via the ip_address normalisation
path against ``::/128`` in BLOCKED_IP_RANGES."""
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://[0::]/"
)
assert is_valid is False
def test_ipv6_unspecified_full_form_blocked(self):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://[0:0:0:0:0:0:0:0]/"
)
assert is_valid is False
class TestCloudMetadataBlockedForPluginSchemes:
"""Plugin-scheme IMDS guard.
Apprise translates schemes like signal://host/... into HTTP requests
against the URL host (e.g. POST http://host/v2/send), so cloud-
metadata IPs reached through a plugin scheme would otherwise bypass
the IMDS protection enforced for http/https. ``validate_service_url``
must reject them under every flag combination.
"""
METADATA_IPS = (
"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
)
# Allowed plugin-shaped authorities used to exercise the validator's
# uniform IMDS policy. This is a validator allowlist fixture, not a claim
# that locked Apprise registers every listed name as a direct prefix.
PLUGIN_AUTHORITY_FIXTURES = (
"signal",
"gotify",
"ntfy",
"ntfys",
"mattermost",
"rocketchat",
"matrix",
"json",
"xml",
"form",
)
@pytest.mark.parametrize("ip", METADATA_IPS)
@pytest.mark.parametrize("scheme", PLUGIN_AUTHORITY_FIXTURES)
def test_metadata_ip_blocked_by_default(self, scheme, ip):
url = (
f"matrix://user:pass@{ip}/%23room"
if scheme == "matrix"
else f"{scheme}://{ip}/path"
)
is_valid, error = NotificationURLValidator.validate_service_url(url)
assert is_valid is False
assert "cloud-metadata" in error.lower()
@pytest.mark.parametrize("ip", METADATA_IPS)
@pytest.mark.parametrize("scheme", PLUGIN_AUTHORITY_FIXTURES)
def test_metadata_ip_blocked_even_with_allow_private_ips(self, scheme, ip):
"""allow_private_ips=True unlocks LAN reach, NOT IMDS."""
url = (
f"matrix://user:pass@{ip}/%23room"
if scheme == "matrix"
else f"{scheme}://{ip}/path"
)
is_valid, error = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is False
assert "cloud-metadata" in error.lower()
def test_mailto_metadata_host_blocked(self):
"""mailto://[email protected]/... must not reach IMDS."""
is_valid, error = NotificationURLValidator.validate_service_url(
"mailto://user:[email protected]/recipient"
)
assert is_valid is False
assert "cloud-metadata" in error.lower()
@pytest.mark.parametrize(
"url",
[
"signal://192.168.50.20:8739/+15551234567/+15557654321",
"gotify://10.0.0.5:8080/AbCdEf123",
"signal://127.0.0.1:8739/+15551234567/+15557654321",
"json://127.0.0.1:8000/internal",
],
)
def test_private_plugin_requires_operator_opt_in(self, url):
denied, reason = NotificationURLValidator.validate_service_url(url)
allowed, _ = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert denied is False
assert "private/internal" in reason.lower()
assert allowed is True
def test_allowlisted_token_shaped_urls_pass_validator(self):
"""Non-IP authority values pass the validator's IMDS check.
This tests validation policy only. Direct-prefix compatibility with
locked Apprise is asserted separately; several legacy LDR names do
not overlap Apprise's registered prefixes.
"""
for url in (
"discord://webhook_id/token",
"slack://TestApp@TokenA/TokenB/TokenC",
"tgram://123456789:Token_abc-123/987654321",
"pushover://user@token",
"teams://group@token1/token2/token3",
):
is_valid, error = NotificationURLValidator.validate_service_url(url)
assert is_valid is True, f"{url} should be valid, got: {error}"
def test_signal_metadata_hostname_via_dns_blocked(self):
"""DNS-resolved hostname pointing at IMDS is rejected — closes
the easy ``signal://imds.attacker.example/...`` variant of the
bypass. (The full DNS-rebinding TOCTOU window is a separately
documented residual risk; this test only covers single-resolve
attackers.)"""
with patch("socket.getaddrinfo") as mock_dns:
mock_dns.return_value = [
(
socket.AF_INET,
socket.SOCK_STREAM,
0,
"",
("169.254.169.254", 0),
)
]
is_valid, error = NotificationURLValidator.validate_service_url(
"signal://imds.attacker.example/+15551234567/+15557654321"
)
assert is_valid is False
assert "cloud-metadata" in error.lower()
class TestAppriseSecondaryDestinationPolicy:
"""Apprise options must not bypass validation of the URL authority."""
@pytest.mark.parametrize(
("url", "blocked_key"),
[
pytest.param(
"discord://123456789012345678/token"
"?template=file%3A%2F%2F%2Fetc%2Fpasswd",
"template",
id="discord-local-template",
),
pytest.param(
"slack://T00000000/B00000000/token"
"?template=http%3A%2F%2F169.254.169.254%2Fmetadata",
"template",
id="slack-remote-template",
),
pytest.param(
"https://discord.com/api/webhooks/123456789012345678/token"
"?template=file%3A%2F%2F%2Fetc%2Fpasswd",
"template",
id="native-https-template",
),
pytest.param(
"https://example.com/hook?redirect=yes",
"redirect",
id="https-redirect-override",
),
pytest.param(
"signal://signal.example.com/source/destination?redirect=yes",
"redirect",
id="plugin-redirect-override",
),
pytest.param(
"mailto://sender:[email protected]/[email protected]"
"?smtp=smtp-alt.example.com",
"smtp",
id="mailto-smtp-override",
),
pytest.param(
"mailto://sender:[email protected]/[email protected]"
"?pgppub=http%3A%2F%2Fkeys.example.com%2Fpublic.asc",
"pgppub",
id="mailto-public-key-resource",
),
pytest.param(
"mailto://sender:[email protected]/[email protected]"
"?pgpkey=file%3A%2F%2F%2Ftmp%2Fpublic.asc",
"pgpkey",
id="mailto-legacy-key-resource",
),
pytest.param(
"mailto://sender:[email protected]/[email protected]"
"?pgpprv=file%3A%2F%2F%2Ftmp%2Fprivate.asc",
"pgpprv",
id="mailto-private-key-resource",
),
pytest.param(
"mailto://sender:[email protected]/[email protected]"
"?wkd=yes",
"wkd",
id="mailto-recipient-wkd-fetch",
),
],
)
def test_secondary_destination_or_resource_is_rejected_before_dns(
self, url, blocked_key
):
with patch.object(
NotificationURLValidator, "_resolve_hostname_ips"
) as resolver:
result = NotificationURLValidator.validate_service_url_with_hint(
url
)
assert result == (
False,
f"Blocked unsafe notification parameter: {blocked_key}",
False,
)
resolver.assert_not_called()
@pytest.mark.parametrize(
("url", "blocked_key"),
[
pytest.param(
"discord://id/token?TemPlate=file%3A%2F%2F%2Ftmp%2Fx",
"template",
id="mixed-case",
),
pytest.param(
"discord://id/token?%74emplate=file%3A%2F%2F%2Ftmp%2Fx",
"template",
id="encoded-key",
),
pytest.param(
"discord://id/token?footer=yes;%74emplate=file%3A%2F%2F%2Fx",
"template",
id="semicolon-separator",
),
pytest.param(
"discord://id/token?footer=yes#fragment&template=file%3A%2F%2Fx",
"template",
id="query-text-after-fragment-marker",
),
pytest.param(
"discord://id/token?template=&template=file%3A%2F%2F%2Fx",
"template",
id="duplicate-blocked-key",
),
pytest.param(
"discord://id/token?%20template%20=file%3A%2F%2F%2Fx",
"template",
id="encoded-surrounding-space",
),
pytest.param(
"mailto://user:[email protected]?mode=starttls;%73mtp=host",
"smtp",
id="encoded-mailto-key-after-semicolon",
),
],
)
def test_query_key_parser_matches_apprise(self, url, blocked_key):
from apprise.utils.parse import parse_qsd
raw_query = url.split("?", 1)[1]
ldr_keys, malformed = NotificationURLValidator._apprise_query_keys(url)
apprise_keys = set(parse_qsd(raw_query)["qsd"])
assert malformed is False
assert set(ldr_keys) == apprise_keys
assert blocked_key in apprise_keys
is_valid, error = NotificationURLValidator.validate_service_url(url)
assert is_valid is False
assert error == f"Blocked unsafe notification parameter: {blocked_key}"
def test_malformed_percent_escape_in_query_key_is_rejected_before_dns(self):
with patch.object(
NotificationURLValidator, "_resolve_hostname_ips"
) as resolver:
result = NotificationURLValidator.validate_service_url_with_hint(
"discord://id/token?bad%=value"
)
assert result == (
False,
"Malformed percent-encoding in notification parameter name",
False,
)
resolver.assert_not_called()
@pytest.mark.parametrize(
"url",
[
"https://93.184.216.34/hook?event=complete&redirect_uri=/done",
"discord://93.184.216.34/token?footer=yes&tts=no",
"discord://93.184.216.34/token?template_id=standard",
"discord://93.184.216.34/token?+template=custom-token",
"discord://93.184.216.34/token?-template=forwarded-value",
"discord://93.184.216.34/token?:template=header-value",
"mailto://user:[email protected]?mode=starttls",
"ntfy://93.184.216.34/topic?mode=private&priority=high",
"matrix://user:[email protected]/%23room?mode=matrix",
],
)
def test_benign_query_parameters_remain_allowed(self, url):
is_valid, error = NotificationURLValidator.validate_service_url(url)
assert is_valid is True
assert error is None
class TestLinkLocalBlockedForPluginSchemes:
"""Plugin-scheme link-local guard (metadata beyond the always-blocked
literals).
The lenient plugin/raw-webhook partition runs with
``allow_private_ips=True`` so self-hosted LAN notifiers work. But
cloud-provider metadata lives across the WHOLE link-local range
(169.254.0.0/16, fe80::/10), not just the always-blocked literals. ``validate_service_url`` must reject
any link-local plugin-scheme target while still allowing RFC1918 /
loopback / non-link-local ULA.
"""
# Link-local metadata / auto-config addresses that are NOT in
# ALWAYS_BLOCKED_METADATA_IPS — these were reachable before the fix.
LINK_LOCAL_IPS = (
"169.254.10.5",
"169.254.1.1",
"169.254.255.254",
)
PLUGIN_SCHEMES = ("json", "xml", "form", "signal", "gotify", "ntfy")
@pytest.mark.parametrize("ip", LINK_LOCAL_IPS)
@pytest.mark.parametrize("scheme", PLUGIN_SCHEMES)
def test_ipv4_link_local_blocked_even_with_allow_private_ips(
self, scheme, ip
):
url = f"{scheme}://{ip}/path"
is_valid, error = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is False, f"{url} should be blocked"
assert "link-local" in error.lower()
def test_ipv6_link_local_blocked(self):
is_valid, error = NotificationURLValidator.validate_service_url(
# Uncompressed: Apprise rewrites a compressed literal, which
# the classifier refuses before the link-local check.
"json://[fe80:0:0:0:0:0:0:1]/path",
allow_private_ips=True,
)
assert is_valid is False
assert "link-local" in error.lower()
def test_rfc1918_still_allowed(self):
for url in (
"json://10.11.12.13/hook",
"json://172.16.5.5/hook",
"json://192.168.50.20/hook",
):
is_valid, error = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is True, f"{url} should be allowed, got: {error}"
def test_loopback_and_ula_still_allowed(self):
# Uncompressed IPv6: Apprise rewrites ``[fd00::1]`` to ``[fd00::]``,
# so the compressed form is refused as ambiguous.
for url in (
"json://127.0.0.1/hook",
"json://[fd00:0:0:0:0:0:0:1]/hook",
):
is_valid, error = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is True, f"{url} should be allowed, got: {error}"
def test_http_scheme_link_local_blocked_even_with_opt_in(self):
"""The link-local block covers http/https too. Apprise converts
some http(s) URLs into plugins that post to the URL host (Apprise
API, Workflows), so an http link-local host is as reachable as a
plugin one and the opt-in must not reopen it."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://169.254.1.1/x", allow_private_ips=True
)
assert is_valid is False
assert "link-local" in error.lower()
class TestEmptyAuthorityRejected:
"""Empty-authority parser-differential SSRF (host smuggled into path).
``json:///169.254.169.254/path`` has an empty ``//`` authority:
urllib3/requests see no host (the IP check is skipped) while Apprise
dials the first path segment as the host. ``validate_service_url``
rejects any empty-authority ``scheme://`` URL before host extraction,
without breaking legitimate URLs.
"""
@pytest.mark.parametrize(
"url",
[
"json:///169.254.169.254/path",
"xml:///10.0.0.1/x",
"form:///metadata",
"https:///example.com/webhook",
"json:///path-only",
],
)
def test_empty_authority_rejected(self, url):
is_valid, error = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is False, f"{url} should be rejected"
assert "empty" in error.lower()
@pytest.mark.parametrize(
"url",
[
"json://host.example/path",
"json://169.254.169.254.example/path", # host, not empty authority
"mailto://user:[email protected]",
"ntfy://host.example/topic",
"discord://webhook_id/token",
"slack://TestApp@TokenA/TokenB/TokenC",
"https://example.com/webhook",
"signal://192.168.50.20:8739/+15551234567/+15557654321",
],
)
def test_legit_urls_with_authority_still_pass(self, url):
is_valid, error = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is True, f"{url} should pass, got: {error}"
class TestEmptyHostAuthorityRejected:
"""Non-empty ``//`` authority that urllib3 parses to an EMPTY host.
``json://169.254.169.254:80@/`` smuggles an IP into the userinfo with
nothing after the trailing ``@``: urllib3 returns no host, so the
per-scheme private-IP check (guarded on ``if hostname``) is skipped and the
URL would be accepted, yet the authority still carries a target another
parser could dial. Sibling of the multi-``@`` / empty-``//``-authority
guards — rejected fail-closed before the scheme checks.
"""
def test_ip_in_userinfo_empty_host_rejected(self):
is_valid, error = NotificationURLValidator.validate_service_url(
"json://169.254.169.254:80@/", allow_private_ips=True
)
assert is_valid is False
assert "host" in error.lower()
@pytest.mark.parametrize(
"url",
[
"json://169.254.169.254:80@/",
"json://169.254.169.254@",
"xml://10.0.0.5:443@/x",
],
)
def test_empty_host_variants_rejected(self, url):
is_valid, _ = NotificationURLValidator.validate_service_url(
url, allow_private_ips=True
)
assert is_valid is False, f"{url} should be rejected"
class TestAllowedSchemesRawSocketRegression:
"""``ALLOWED_SCHEMES`` gates which Apprise schemes reach the guarded send
path. A scheme whose plugin does network I/O via a primitive the DNS-pin
shim cannot see — a raw ``socket.sendto`` datagram (``rsyslog://``) or
similar — would bypass BOTH the address pin and the block-private window.
Lock such schemes out of the allowlist so one can't be added later without
matching pin/block coverage.
"""
@pytest.mark.parametrize("scheme", ["rsyslog", "aprs", "syslog", "xmpp"])
def test_raw_socket_scheme_not_allowed(self, scheme):
assert scheme not in NotificationURLValidator.ALLOWED_SCHEMES
class TestValidateServiceUrlStrict:
"""Tests for validate_service_url_strict static method."""
def test_valid_url_returns_true(self):
"""Should return True for valid URLs."""
result = NotificationURLValidator.validate_service_url_strict(
"https://example.com/webhook"
)
assert result is True
def test_invalid_url_raises_exception(self):
"""Should raise NotificationURLValidationError for invalid URLs."""
with pytest.raises(NotificationURLValidationError) as exc_info:
NotificationURLValidator.validate_service_url_strict(
"file:///etc/passwd"
)
assert "validation failed" in str(exc_info.value)
def test_private_ip_raises_exception(self):
"""Should raise exception for private IPs by default."""
with pytest.raises(NotificationURLValidationError) as exc_info:
NotificationURLValidator.validate_service_url_strict(
"http://localhost/webhook"
)
assert "Blocked private/internal IP" in str(exc_info.value)
def test_private_ip_allowed_with_flag(self):
"""Should not raise when allow_private_ips=True."""
result = NotificationURLValidator.validate_service_url_strict(
"http://localhost/webhook", allow_private_ips=True
)
assert result is True
class TestValidateMultipleUrls:
"""Tests for validate_multiple_urls static method."""
def test_empty_urls_rejected(self):
"""Should reject empty URL string."""
is_valid, error = NotificationURLValidator.validate_multiple_urls("")
assert is_valid is False
assert "non-empty string" in error
def test_none_urls_rejected(self):
"""Should reject None."""
is_valid, error = NotificationURLValidator.validate_multiple_urls(None)
assert is_valid is False
assert "non-empty string" in error
def test_only_separators_rejected(self):
"""Should reject string with only separators."""
is_valid, error = NotificationURLValidator.validate_multiple_urls(",,,")
assert is_valid is False
assert "No valid URLs found" in error
def test_single_valid_url(self):
"""Should accept single valid URL."""
is_valid, error = NotificationURLValidator.validate_multiple_urls(
"https://example.com/webhook"
)
assert is_valid is True
assert error is None
def test_multiple_valid_urls(self):
"""Should accept multiple valid URLs."""
urls = "https://example.com/webhook,discord://id/token,slack://token"
is_valid, error = NotificationURLValidator.validate_multiple_urls(urls)
assert is_valid is True
assert error is None
@pytest.mark.parametrize(
"url",
[
"json://example.com/webhook?field=a,b",
(
"mailto://user:[email protected]"
"[email protected],[email protected]"
),
],
)
def test_embedded_commas_remain_in_single_url(self, url):
"""Commas inside one Apprise URL must not create invalid entries."""
is_valid, error = NotificationURLValidator.validate_multiple_urls(url)
assert is_valid is True
assert error is None
@pytest.mark.parametrize(
"urls",
[
"typo.example.com/x, slack://t/x/y",
"typo.example.com/x slack://t/x/y",
"discord://id/token, example.com/webhook",
"discord://id/token,example.com/webhook",
"discord://id/token example.com/webhook",
],
)
def test_scheme_less_fragment_fails_closed(self, urls):
is_valid, error = NotificationURLValidator.validate_multiple_urls(urls)
assert is_valid is False
assert "must have a protocol" in error
assert "example.com" not in error
assert "typo.example.com" not in error
@pytest.mark.parametrize(
("urls", "expected_fragment"),
[
("discord://x garbage", "garbage"),
("discord://x ???", "???"),
("discord://x\tgarbage", "garbage"),
("discord://x\x00garbage", "garbage"),
],
)
def test_unencoded_whitespace_fragment_fails_closed(
self, urls, expected_fragment
):
"""An entry containing characters that are illegal unencoded in a
URI (whitespace, control bytes) is not one URL: the token after
the first illegal character must surface as ``invalid_fragment``
however it is shaped — a bare word like ``garbage`` is exactly as
undeliverable as the dotted ``example.com/hook`` shape pinned
above. Weakening the parser back to dotted-name-only fragment
detection returns ``None`` for these inputs and the manager
mislabels the resulting drop as an egress refusal instead of
``invalid_url``."""
parsed_urls, invalid_fragment = parse_notification_url_list(urls)
assert invalid_fragment == expected_fragment
assert parsed_urls == [urls]
@pytest.mark.parametrize(
("label", "whitespace"),
[
("NBSP U+00A0", "\u00a0"),
("IDEOGRAPHIC SPACE U+3000", "\u3000"),
("LINE SEPARATOR U+2028", "\u2028"),
("THIN SPACE U+2009", "\u2009"),
("VERTICAL TAB", "\x0b"),
("FORM FEED", "\x0c"),
],
)
def test_surrounding_unicode_whitespace_is_trimmed_not_refused(
self, label, whitespace
):
"""A URL pasted with invisible whitespace around it is still a
valid single URL, not a malformed entry.
``RFC_FORBIDDEN_URL_CHARS_RE``'s ``\\s`` is Unicode-aware, but
``str.strip(" ,\\t\\r\\n")`` is ASCII-only. Trimming with the
narrow set and then applying the wide check made an entry ending
in U+00A0 / U+3000 / \\x0b / \\x0c (a webhook URL copied out of a
rendered docs page, say) come back as an ``invalid_fragment`` —
so the whole ``notifications.service_url`` failed closed as
``invalid_url``, with a message telling the operator to "remove
any spaces, backslashes or control characters" that describes
nothing they can see. Every other consumer trims with bare
``str.strip()`` before its own RFC check
(``validate_service_url``, ``_partition_urls``), so these values
were, and must stay, deliverable.
Teeth: revert the trim to ``entry.strip(" ,\\t\\r\\n")`` and every
TRAILING case here returns a fragment instead of ``None``. The
LEADING-only candidate is a CONTROL, not a tooth: ``\\s`` in
``_URL_BOUNDARY_RE`` is Unicode-aware too, so leading whitespace is
consumed as an entry boundary and never reaches the RFC check on
the entry. It is kept to pin that the boundary split and the trim
agree on the same whitespace class.
"""
base = "discord://123456789012345678/abcdefghijklmnop"
for candidate in (
base + whitespace,
# Control: handled by the boundary split — see the docstring.
whitespace + base,
whitespace + base + whitespace,
base + " " + whitespace,
base + whitespace + ",",
):
parsed_urls, invalid_fragment = parse_notification_url_list(
candidate
)
assert invalid_fragment is None, (label, repr(candidate))
assert parsed_urls == [base], (label, repr(candidate))
def test_unicode_whitespace_trim_applies_to_every_entry(self):
"""The trim runs per entry, so a trailing NBSP on the LAST URL of
a multi-URL setting does not condemn the whole list either."""
parsed_urls, invalid_fragment = parse_notification_url_list(
"discord://111111111111111111/aaaaaaaaaaaaaaaa\u00a0"
", json://hook.example/x\u3000"
)
assert invalid_fragment is None
assert parsed_urls == [
"discord://111111111111111111/aaaaaaaaaaaaaaaa",
"json://hook.example/x",
]
@pytest.mark.parametrize(
("urls", "expected_fragment"),
[
# Positive controls for the trim above: it is anchored to the
# ENDS of an entry, so nothing with a smuggled token after an
# illegal character may be loosened by it.
("discord://x garbage", "garbage"),
("discord://x\u00a0garbage", "garbage"),
("discord://x\u3000garbage", "garbage"),
("discord://x example.com/hook", "example.com/hook"),
(
"json://ok.example/x, slack://tokenA/tokenB/tokenC\\",
"slack://tokenA/tokenB/tokenC\\",
),
(
"json://metadata.example/x\\https://vendor.example/ok",
"https://vendor.example/ok",
),
],
)
def test_edge_trim_does_not_loosen_smuggled_tokens(
self, urls, expected_fragment
):
"""The Unicode-whitespace trim must not weaken the fail-closed
parse. An illegal character with a token after it, and an illegal
character that is not whitespace at all (backslash), still yield a
fragment.
Teeth: widen the trim from the entry's ends to a global
substitution and the first four cases stop fragmenting, silently
dispatching the smuggled destination."""
parsed_urls, invalid_fragment = parse_notification_url_list(urls)
assert invalid_fragment == expected_fragment
def test_one_invalid_url_fails_all(self):
"""Should fail if any URL is invalid."""
urls = "https://example.com/webhook,file:///etc/passwd"
is_valid, error = NotificationURLValidator.validate_multiple_urls(urls)
assert is_valid is False
assert "file" in error.lower()
def test_whitespace_in_urls_stripped(self):
"""Should handle whitespace around URLs."""
urls = " https://example.com/webhook , discord://id/token "
is_valid, error = NotificationURLValidator.validate_multiple_urls(urls)
assert is_valid is True
assert error is None
def test_unicode_whitespace_around_urls_stripped(self):
"""The user-visible half of the trim: a setting whose entries carry
invisible surrounding whitespace still validates, so the send path
does not report ``invalid_url`` for a URL the operator cannot see
anything wrong with. ``discord://`` is a token scheme, so this
exercises no DNS."""
urls = "\u00a0discord://id/token\u00a0,\u3000discord://id2/token2\u3000"
is_valid, error = NotificationURLValidator.validate_multiple_urls(urls)
assert is_valid is True
assert error is None
def test_custom_separator(self):
"""Should support custom separator."""
urls = "https://example.com/webhook|discord://id/token"
is_valid, error = NotificationURLValidator.validate_multiple_urls(
urls, separator="|"
)
assert is_valid is True
assert error is None
def test_private_ip_in_multiple_blocked(self):
"""Should block private IPs in multiple URLs."""
urls = "https://example.com/webhook,http://localhost/webhook"
is_valid, error = NotificationURLValidator.validate_multiple_urls(urls)
assert is_valid is False
assert "Blocked private/internal IP" in error
def test_private_ip_allowed_with_flag(self):
"""Should allow private IPs when flag is set."""
urls = "https://example.com/webhook,http://localhost/webhook"
is_valid, error = NotificationURLValidator.validate_multiple_urls(
urls, allow_private_ips=True
)
assert is_valid is True
assert error is None
class TestValidateMultipleUrlsFragmentMessage:
r"""``validate_multiple_urls`` refuses ANY non-``None``
``invalid_fragment``, and says something true while doing it.
The refusal is about the INPUT not partitioning unambiguously, not
about the fragment being a bad URL — and a fragment can be a
perfectly good URL on its own: when the illegal character only trails
the entry the fragment IS the entry, and when it separates two
entries the trailing token can be a well-formed vendor URL. So
interpolating ``validate_service_url``'s error unconditionally can
interpolate ``None``.
"""
@patch(
"local_deep_research.security.notification_validator"
".NotificationURLValidator.validate_service_url",
return_value=(True, None),
)
def test_valid_fragment_still_refused_with_a_real_message(
self, mock_validate
):
r"""Teeth: drop the ``if not error_message:`` fallback in
``validate_multiple_urls`` and the user-facing message becomes
the nonsense ``"Invalid notification service URL: None"`` — the
``"None"`` assertion below fails.
``validate_service_url`` is stubbed to accept everything (no DNS,
and it pins the branch that only exists when the fragment passes
on its own).
"""
is_valid, error = NotificationURLValidator.validate_multiple_urls(
"https://example.com/a\\https://example.org/b"
)
assert is_valid is False
assert "None" not in error
assert "could not be parsed unambiguously" in error
# The fragment's own text is never echoed back — it is
# attacker-shaped and may carry real credentials.
assert "example.org" not in error
@patch(
"local_deep_research.security.notification_validator"
".NotificationURLValidator.validate_service_url",
return_value=(True, None),
)
def test_trailing_forbidden_byte_refused_without_echoing_the_entry(
self, mock_validate
):
r"""Companion to the above on the ``else entry`` branch, where the
illegal character only TRAILS the entry so the fragment IS the
whole credential-bearing service URL.
``validate_service_url`` is stubbed to accept everything for the
same reason as the test above: with the real validator this input
takes the OTHER branch (the RFC check returns a non-empty message,
so the ``if not error_message:`` fallback never runs) and the
assertions below would hold no matter what the fallback said. The
stub forces the fallback, which is the branch that has to invent
its own text and is therefore the one that could echo the entry.
Teeth: interpolate ``invalid_fragment`` into that fallback message
and ``"tokenA"``/``"slack://"`` appear in the user-facing error —
an Apprise bot token in a validation message. Drop the fallback
entirely and the message becomes ``"... : None"``.
"""
is_valid, error = NotificationURLValidator.validate_multiple_urls(
"slack://tokenA/tokenB/tokenC/\\"
)
assert is_valid is False
assert error
assert "None" not in error
assert "could not be parsed unambiguously" in error
assert "tokenA" not in error
assert "slack://" not in error
class TestClassConstants:
"""Tests for class constants."""
def test_blocked_schemes_contains_dangerous_protocols(self):
"""BLOCKED_SCHEMES should contain dangerous protocols."""
blocked = NotificationURLValidator.BLOCKED_SCHEMES
assert "file" in blocked
assert "ftp" in blocked
assert "javascript" in blocked
assert "data" in blocked
def test_allowed_schemes_contains_common_services(self):
"""ALLOWED_SCHEMES should contain common notification services."""
allowed = NotificationURLValidator.ALLOWED_SCHEMES
assert "http" in allowed
assert "https" in allowed
assert "discord" in allowed
assert "slack" in allowed
assert "tgram" in allowed
assert "telegram" not in allowed
assert "mailto" in allowed
assert "ntfys" in allowed
assert "signal" in allowed
def test_private_ip_ranges_exist(self):
"""PRIVATE_IP_RANGES should contain RFC1918 and other private ranges."""
ranges = NotificationURLValidator.PRIVATE_IP_RANGES
assert len(ranges) > 0
# Check some expected ranges are present
range_strings = [str(r) for r in ranges]
assert "127.0.0.0/8" in range_strings
assert "10.0.0.0/8" in range_strings
assert "192.168.0.0/16" in range_strings
class TestIPv4TranslatedPrefixInNotificationValidator:
"""Mirror of ssrf_validator's IPv4-Translated checks for the
notification path. Both surfaces share ``PRIVATE_IP_RANGES`` through
``is_ip_blocked``, so the second ingress is pinned the way the
NAT64 family above is."""
def test_ipv4_translated_imds_blocked(self):
"""``::ffff:0:169.254.169.254`` — RFC 2765 / SIIT, IMDS in the low
32 bits. Not the IPv4-MAPPED prefix, so ``ipv4_mapped`` does not
unwrap it (#6408)."""
assert (
NotificationURLValidator._is_private_ip("::ffff:0:169.254.169.254")
is True
)
def test_ipv4_translated_loopback_blocked(self):
"""``::ffff:0:127.0.0.1`` — the address from the report."""
assert (
NotificationURLValidator._is_private_ip("::ffff:0:127.0.0.1")
is True
)
def test_ipv4_translated_hex_form_blocked(self):
"""The same two addresses with the low 32 bits in hex."""
assert (
NotificationURLValidator._is_private_ip("::ffff:0:a9fe:a9fe")
is True
)
assert (
NotificationURLValidator._is_private_ip("::ffff:0:7f00:1") is True
)
def test_the_mapped_family_is_still_judged_on_its_payload(self):
"""Control: the new entry blocks the TRANSLATED prefix only.
``::ffff:8.8.8.8`` is IPv4-MAPPED, one group lower, and is still
unwrapped and judged on the address it carries — so this class
cannot pass by refusing every embedded-IPv4 literal.
"""
assert (
NotificationURLValidator._is_private_ip("::ffff:8.8.8.8") is False
)
assert (
NotificationURLValidator._is_private_ip("::ffff:127.0.0.1") is True
)
def test_ipv4_translated_rejected_under_allow_private_ips(self):
"""``validate_service_url`` must refuse the translated IMDS embed
even with ``allow_private_ips=True`` — the flag widens the
RFC1918/loopback literals, not the transition-prefix block
(mirror of the SSRF surface's allow-flag matrix).
"""
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://[::ffff:0:169.254.169.254]/", allow_private_ips=True
)
assert is_valid is False
def test_ipv4_translated_public_embed_blocked(self):
"""``::ffff:0:8.8.8.8`` — translated wrap of a PUBLIC IPv4 is
private here too.
Intentional divergence from the mapped family (whose public
wraps unwrap and pass, as pinned by the control above): the
translated prefix has no legitimate endpoint use, so the whole
/96 is refused. Pins the widening so a later "refinement"
cannot narrow the block to private embeds only.
"""
assert (
NotificationURLValidator._is_private_ip("::ffff:0:8.8.8.8") is True
)
class TestNat64EnvOptOutInNotificationValidator:
"""Mirror of ssrf_validator's TestNat64EnvOptOut for the notification
path. The notification validator must honor the same operator
opt-in semantics AND keep the cloud-metadata block absolute."""
def test_nat64_wkp_blocked_when_env_unset(self, monkeypatch):
monkeypatch.delenv("LDR_SECURITY_ALLOW_NAT64", raising=False)
# 64:ff9b::a00:1 is the NAT64 wrap of 10.0.0.1.
assert NotificationURLValidator._is_private_ip("64:ff9b::a00:1") is True
def test_nat64_wkp_allowed_when_env_true(self, monkeypatch):
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
# NAT64 wrap of 8.8.8.8 — canonical IPv6-only-deployment use case.
assert (
NotificationURLValidator._is_private_ip("64:ff9b::808:808") is False
)
def test_nat64_local_use_allowed_when_env_true(self, monkeypatch):
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
assert (
NotificationURLValidator._is_private_ip("64:ff9b:1:808:8:800::")
is False
)
def test_imds_via_nat64_wkp_wrap_blocked_under_env_true(self, monkeypatch):
"""[64:ff9b::a9fe:a9fe] — NAT64 WKP wrap of 169.254.169.254.
Must remain blocked even with the operator opt-in. Mirrors the
ssrf_validator embedded-IPv4 IMDS check."""
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
assert (
NotificationURLValidator._is_private_ip("64:ff9b::a9fe:a9fe")
is True
)
def test_imds_via_nat64_local_use_wrap_blocked_under_env_true(
self, monkeypatch
):
"""Same lock-in for the RFC 8215 local-use prefix wrap."""
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
assert (
NotificationURLValidator._is_private_ip("64:ff9b:1:a9fe:a9:fe00::")
is True
)
def test_ecs_metadata_via_nat64_wrap_blocked_under_env_true(
self, monkeypatch
):
"""169.254.170.2 = 0xa9feaa02 — AWS ECS task metadata v3."""
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
assert (
NotificationURLValidator._is_private_ip("64:ff9b::a9fe:aa02")
is True
)
def test_alibaba_metadata_via_nat64_wrap_blocked_under_env_true(
self, monkeypatch
):
"""100.100.100.200 = 0x646464c8 — AlibabaCloud metadata."""
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
assert (
NotificationURLValidator._is_private_ip("64:ff9b::6464:64c8")
is True
)
def test_env_does_not_unblock_6to4_in_notification_path(self, monkeypatch):
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
assert (
NotificationURLValidator._is_private_ip("2002:c0a8:101::") is True
)
def test_env_does_not_unblock_teredo_in_notification_path(
self, monkeypatch
):
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
assert NotificationURLValidator._is_private_ip("2001::1") is True
def test_imds_via_nat64_wrap_blocked_when_env_unset(self, monkeypatch):
"""Sanity: the IMDS embedded-IPv4 check fires regardless of env
state — when env is unset, the NAT64 prefix entry already blocks
directly, but the embedded-IPv4 path is still well-formed."""
monkeypatch.delenv("LDR_SECURITY_ALLOW_NAT64", raising=False)
assert (
NotificationURLValidator._is_private_ip("64:ff9b::a9fe:a9fe")
is True
)
def test_ipv4_mapped_imds_blocked(self, monkeypatch):
"""Cross-validator parity: ssrf_validator unwraps IPv4-mapped
IPv6 (``::ffff:169.254.169.254``) before the IMDS literal check.
notification_validator must do the same — otherwise an attacker
who can configure a webhook URL can reach IMDS via the IPv4-
mapped form. Pre-PR this was a real gap; locked in here so it
cannot regress."""
monkeypatch.delenv("LDR_SECURITY_ALLOW_NAT64", raising=False)
assert (
NotificationURLValidator._is_private_ip("::ffff:169.254.169.254")
is True
)
def test_ipv4_mapped_loopback_blocked(self, monkeypatch):
"""Same parity check for the loopback IPv4-mapped form."""
monkeypatch.delenv("LDR_SECURITY_ALLOW_NAT64", raising=False)
assert (
NotificationURLValidator._is_private_ip("::ffff:127.0.0.1") is True
)
def test_ipv4_mapped_public_ip_passes(self, monkeypatch):
"""Anti-collision: the unwrap must not over-block public IPv4."""
monkeypatch.delenv("LDR_SECURITY_ALLOW_NAT64", raising=False)
assert (
NotificationURLValidator._is_private_ip("::ffff:8.8.8.8") is False
)
def test_validate_service_url_imds_blocked_under_allow_private_ips(self):
"""Round-3 audit regression: validate_service_url with
allow_private_ips=True previously short-circuited the entire
host check, allowing http://169.254.169.254/ through. The opt-in
is for self-hosted webhooks on internal networks, not for IMDS
exfiltration. ALWAYS_BLOCKED_METADATA_IPS must remain absolute."""
is_valid, error = NotificationURLValidator.validate_service_url(
"http://169.254.169.254/latest/meta-data/",
allow_private_ips=True,
)
assert is_valid is False
assert error is not None
def test_validate_service_url_imds_v6_mapped_blocked_under_allow_private_ips(
self,
):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://[::ffff:169.254.169.254]/", allow_private_ips=True
)
assert is_valid is False
def test_validate_service_url_imds_via_nat64_wkp_blocked_under_allow_private_ips(
self,
):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://[64:ff9b::a9fe:a9fe]/", allow_private_ips=True
)
assert is_valid is False
def test_validate_service_url_imds_via_nat64_local_use_blocked_under_allow_private_ips(
self,
):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://[64:ff9b:1:a9fe:a9:fe00::]/", allow_private_ips=True
)
assert is_valid is False
def test_validate_service_url_alibaba_metadata_blocked_under_allow_private_ips(
self,
):
"""100.100.100.200 is in ALWAYS_BLOCKED_METADATA_IPS and ALSO in
the CGNAT range (100.64.0.0/10) — pre-fix the carve-out for
CGNAT under allow_private_ips=True would have leaked it."""
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://100.100.100.200/", allow_private_ips=True
)
assert is_valid is False
def test_validate_service_url_rfc1918_allowed_under_allow_private_ips(self):
"""Anti-collision: the fix must not over-block legitimate
self-hosted webhook destinations. allow_private_ips=True is
designed for exactly this case."""
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://192.168.1.100/webhook", allow_private_ips=True
)
assert is_valid is True
def test_validate_service_url_localhost_allowed_under_allow_private_ips(
self,
):
is_valid, _ = NotificationURLValidator.validate_service_url(
"http://localhost:5000/webhook", allow_private_ips=True
)
assert is_valid is True
def test_dns_resolved_imds_via_nat64_blocked_under_env_true(
self, monkeypatch
):
"""Hostname-resolution branch: a hostname that resolves to a
NAT64-wrapped IMDS IPv4 must still be blocked under env opt-in.
This exercises the second call site of _ip_matches_blocked_range."""
monkeypatch.setenv("LDR_SECURITY_ALLOW_NAT64", "true")
# AF_INET6 result tuple: (family, type, proto, canonname, sockaddr)
# sockaddr for IPv6 is (host, port, flowinfo, scopeid)
with patch(
"socket.getaddrinfo",
return_value=[
(
socket.AF_INET6,
socket.SOCK_STREAM,
6,
"",
("64:ff9b::a9fe:a9fe", 0, 0, 0),
)
],
):
assert (
NotificationURLValidator._is_private_ip("imds.attacker.example")
is True
)