~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
fix(notifications): parse service URL boundaries safely (#5394)
Ziyang Guocf2c072a0b0e
diff --git a/changelog.d/5394.bugfix.md b/changelog.d/5394.bugfix.mdnew file mode 100644index 0000000000..1223e2eaf5--- /dev/null+++ b/changelog.d/5394.bugfix.md@@ -0,0 +1 @@+Preserve commas inside Apprise service URLs while refusing configurations that contain scheme-less URL fragments.diff --git a/src/local_deep_research/notifications/manager.py b/src/local_deep_research/notifications/manager.pyindex b07a201629..a0a72482c7 100644--- a/src/local_deep_research/notifications/manager.py+++ b/src/local_deep_research/notifications/manager.py@@ -11,6 +11,8 @@ import threadingfrom loguru import logger+from ..security.notification_validator import parse_notification_url_list+from ..security.ssrf_validator import redact_url_for_logfrom ..settings.env_registry import get_env_settingfrom .service import NotificationServicefrom .templates import EventType@@ -396,15 +398,26 @@ class NotificationManager:return self.service.test_service(allowed)def _filter_urls_by_egress_policy(self, service_urls: str) -> str:- """Filter an Apprise URL string (space- or comma-separated) by- the user's egress policy. Returns the joined string of allowed- URLs (may be empty). When no snapshot / context is available,- returns the input unchanged (back-compat — the snapshot-less- path was already used in older callers).+ """Filter an Apprise URL string by the user's egress policy.++ Commas inside one service URL are preserved; commas or whitespace+ followed by another scheme delimit URLs. When no snapshot / context is+ available, return the input unchanged for backwards compatibility."""snapshot = getattr(self, "_settings_snapshot", None)if not snapshot:return service_urls++ url_entries, invalid_fragment = parse_notification_url_list(+ service_urls+ )+ if invalid_fragment is not None:+ logger.bind(policy_audit=True).warning(+ "all notification URLs refused: scheme-less URL fragment",+ fragment=redact_url_for_log(invalid_fragment),+ )+ return ""+try:from ..security.egress.policy import (EgressScope,@@ -456,10 +469,7 @@ class NotificationManager:# pass-through stands (the modeled threat there is internal-http# SSRF via a raw webhook, not vendor APIs).parts = []- for url_entry in service_urls.replace(",", " ").split():- url_entry = url_entry.strip()- if not url_entry:- continue+ for url_entry in url_entries:scheme = (url_entry.split(":", 1)[0].lower() if ":" in url_entry else "")diff --git a/src/local_deep_research/security/notification_validator.py b/src/local_deep_research/security/notification_validator.pyindex 3e551b952f..780b4ed584 100644--- a/src/local_deep_research/security/notification_validator.py+++ b/src/local_deep_research/security/notification_validator.py@@ -6,10 +6,12 @@ to prevent Server-Side Request Forgery (SSRF) attacks and other security issues."""import ipaddress+import reimport socketfrom concurrent.futures import ThreadPoolExecutor, TimeoutErrorfrom typing import List, Optional, Tuple, Unionfrom urllib.parse import urlparse+from loguru import loggerfrom urllib3.exceptions import LocationParseErrorfrom urllib3.util import parse_url@@ -21,6 +23,48 @@ from .ssrf_validator import RFC_FORBIDDEN_URL_CHARS_RE, redact_url_for_log# ipaddress._BaseAddress API).ResolvedIP = Union[ipaddress.IPv4Address, ipaddress.IPv6Address]+_SCHEMELESS_URL_FRAGMENT_RE = re.compile(+ r"^(?:localhost|(?:\d{1,3}\.){3}\d{1,3}|"+ r"(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,})"+ r"(?::\d+)?(?:[/?#]|$)"+)+_URL_SCHEME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]{0,31}://")+_URL_BOUNDARY_RE = re.compile(r"[,\s]+(?=[A-Za-z][A-Za-z0-9+.-]{0,31}://)")+++def parse_notification_url_list(+ urls: str, separator: str = ","+) -> tuple[list[str], Optional[str]]:+ """Partition notification URLs without dropping malformed fragments.++ A boundary is a comma or whitespace sequence followed by a URL scheme.+ This preserves commas inside one Apprise service URL while keeping+ whitespace a hard separator. URL-like fragments without a scheme are+ returned separately so callers can fail closed instead of dispatching only+ the valid subset.++ Returns ``(parsed_urls, invalid_fragment)``. Custom separators retain the+ historical split behavior.+ """+ if separator != ",":+ return (+ [entry.strip() for entry in urls.split(separator) if entry.strip()],+ None,+ )++ parsed_urls = [+ entry.strip(" ,\t\r\n")+ for entry in _URL_BOUNDARY_RE.split(urls)+ if entry.strip(" ,\t\r\n")+ ]+ for entry in parsed_urls:+ if not _URL_SCHEME_RE.match(entry):+ return parsed_urls, entry+ for fragment in re.split(r"[,\s]+", entry)[1:]:+ if _SCHEMELESS_URL_FRAGMENT_RE.match(fragment):+ return parsed_urls, fragment+ return parsed_urls, None+class NotificationURLValidationError(ValueError):"""Raised when a notification service URL fails security validation."""@@ -869,8 +913,21 @@ class NotificationURLValidator:if not urls or not isinstance(urls, str):return False, "Service URLs must be a non-empty string"- # Split by separator and strip whitespace- url_list = [url.strip() for url in urls.split(separator) if url.strip()]+ # Match Apprise's scheme-aware partition so commas inside one service+ # URL are preserved, while rejecting URL-like fragments it repairs+ # away or absorbs into another entry.+ url_list, invalid_fragment = parse_notification_url_list(+ urls, separator+ )+ if invalid_fragment is not None:+ _, error_message = NotificationURLValidator.validate_service_url(+ invalid_fragment, allow_private_ips+ )+ return (+ False,+ f"Invalid URL '{redact_url_for_log(invalid_fragment)}': "+ f"{error_message}",+ )if not url_list:return False, "No valid URLs found after parsing"diff --git a/tests/notifications/test_manager.py b/tests/notifications/test_manager.pyindex f298c1f53a..bd24d06b16 100644--- a/tests/notifications/test_manager.py+++ b/tests/notifications/test_manager.py@@ -1075,6 +1075,47 @@ class TestEgressPolicyURLFilter:assert "slack://t" in outassert "http://127.0.0.1/h" not in out+ def test_embedded_comma_survives_mixed_service_filtering(self, monkeypatch):+ monkeypatch.setenv("LDR_POLICY_ALLOW_UNPROTECTED_EGRESS", "true")+ mgr = self._mgr("unprotected")+ json_url = "json://example.com/webhook?field=a,b"+ out = mgr._filter_urls_by_egress_policy(+ f"{json_url}, discord://webhook/token"+ )++ assert out == f"{json_url} discord://webhook/token"++ @pytest.mark.parametrize(+ "service_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",+ ],+ )+ def test_scheme_less_fragment_refuses_entire_configuration(+ self, service_urls+ ):+ mgr = self._mgr("unprotected")++ assert mgr._filter_urls_by_egress_policy(service_urls) == ""++ def test_scheme_less_refusal_logs_only_redacted_fragment(self):+ mgr = self._mgr("unprotected")+ service_urls = "secret.example.com/private/token slack://t/x/y"++ with patch(+ "local_deep_research.notifications.manager.logger"+ ) as mock_logger:+ assert mgr._filter_urls_by_egress_policy(service_urls) == ""++ mock_logger.bind.assert_called_once_with(policy_audit=True)+ warning = mock_logger.bind.return_value.warning+ warning.assert_called_once()+ assert warning.call_args.kwargs["fragment"] == "?://secret.example.com"+ assert "/private/token" not in warning.call_args.kwargs["fragment"]+def test_public_only_blocks_private_http_keeps_public(self):mgr = self._mgr("public_only", tool="searxng")out = mgr._filter_urls_by_egress_policy(@@ -1086,3 +1127,5 @@ class TestEgressPolicyURLFilter:mgr = NotificationManager.__new__(NotificationManager)mgr._settings_snapshot = Noneassert mgr._filter_urls_by_egress_policy("slack://t") == "slack://t"+ malformed = "typo.example.com/x slack://t/x/y"+ assert mgr._filter_urls_by_egress_policy(malformed) == malformeddiff --git a/tests/security/test_notification_validator.py b/tests/security/test_notification_validator.pyindex d2cdf208e9..52290262a0 100644--- a/tests/security/test_notification_validator.py+++ b/tests/security/test_notification_validator.py@@ -827,6 +827,40 @@ class TestValidateMultipleUrls:assert is_valid is Trueassert error is None+ @pytest.mark.parametrize(+ "url",+ [+ "json://example.com/webhook?field=a,b",+ (+ "mailto://user:[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" in error+def test_one_invalid_url_fails_all(self):"""Should fail if any URL is invalid."""urls = "https://example.com/webhook,file:///etc/passwd"