diff --git a/shelfmark/config/notifications_settings.py b/shelfmark/config/notifications_settings.py index 681a0c2e..4fff97d2 100644 --- a/shelfmark/config/notifications_settings.py +++ b/shelfmark/config/notifications_settings.py @@ -7,7 +7,11 @@ from typing import Any from urllib.parse import urlsplit from shelfmark.core.config import config as app_config -from shelfmark.core.notifications import NotificationEvent, send_test_notification +from shelfmark.core.notifications import ( + NotificationEvent, + send_test_notification, + user_route_url_problem, +) from shelfmark.core.settings_registry import ( ActionButton, HeadingField, @@ -142,8 +146,14 @@ def _extract_unique_route_urls(routes: list[dict[str, Any]]) -> list[str]: return urls -def build_notification_test_result(routes_input: Any, *, scope_label: str) -> dict[str, Any]: - """Validate routes and return a test-notification result payload.""" +def build_notification_test_result( + routes_input: Any, *, scope_label: str, user_scope: bool = False +) -> dict[str, Any]: + """Validate routes and return a test-notification result payload. + + ``user_scope`` is for a user's own routes, which may only use the services and public hosts + that ``user_route_url_problem`` allows. + """ invalid_event_count = _count_invalid_route_events(routes_input) if invalid_event_count: return { @@ -172,6 +182,15 @@ def build_notification_test_result(routes_input: Any, *, scope_label: str) -> di "message": f"Add at least one {scope_label} notification URL route first.", } + if user_scope: + for url in urls: + problem = user_route_url_problem(url) + if problem is not None: + return { + "success": False, + "message": f"Personal notification route refused: {problem}.", + } + return send_test_notification(urls) diff --git a/shelfmark/core/admin_settings_routes.py b/shelfmark/core/admin_settings_routes.py index b4d33905..18b91f88 100644 --- a/shelfmark/core/admin_settings_routes.py +++ b/shelfmark/core/admin_settings_routes.py @@ -146,7 +146,7 @@ def build_user_notification_test_response( elif "routes" in payload: routes_input = payload.get("routes") - result = build_notification_test_result(routes_input, scope_label="personal") + result = build_notification_test_result(routes_input, scope_label="personal", user_scope=True) status_code = 200 if result.get("success", False) else 400 return result, status_code diff --git a/shelfmark/core/notifications.py b/shelfmark/core/notifications.py index fc7a5e23..aa038958 100644 --- a/shelfmark/core/notifications.py +++ b/shelfmark/core/notifications.py @@ -2,7 +2,9 @@ from __future__ import annotations +import ipaddress import logging +import socket import threading from concurrent.futures import ThreadPoolExecutor from contextlib import contextmanager, suppress @@ -285,6 +287,72 @@ def _normalize_user_id(value: object) -> int | None: return normalize_positive_int(value) +# A user's own routes are sent from the server, so they must not be able to reach hosts the user +# could not reach themselves. These services always talk to the vendor's own host, whatever the +# rest of the URL says. +_USER_ROUTE_FIXED_HOST_SCHEMES = frozenset({"discord", "slack", "tgram", "pover", "pbul"}) +# These connect to the host written in the URL, so that host has to be a public one. +_USER_ROUTE_HOST_SCHEMES = frozenset({"ntfy", "ntfys", "gotify", "gotifys"}) + + +def _is_public_address(address: str) -> bool: + try: + parsed = ipaddress.ip_address(address.split("%", 1)[0]) + except ValueError: + return False + if isinstance(parsed, ipaddress.IPv6Address) and parsed.ipv4_mapped is not None: + parsed = parsed.ipv4_mapped + return parsed.is_global and not parsed.is_multicast + + +def _host_is_public(host: str) -> bool: + """Whether every address the host resolves to is a public one. A host that does not resolve + has nothing to connect to, so it passes.""" + try: + infos = socket.getaddrinfo(host, None) + except OSError: + return True + return all(_is_public_address(str(info[4][0])) for info in infos) + + +def _apprise_host(url: str) -> str | None: + """The host Apprise itself reads from the URL, which can differ from urlsplit's reading.""" + if apprise is None: + return None + try: + plugin = apprise.Apprise.instantiate(url) + except Exception: # noqa: BLE001 - an unparsable URL is judged on urlsplit alone + return None + host = getattr(plugin, "host", None) + return host if isinstance(host, str) and host else None + + +def user_route_url_problem(url: str) -> str | None: + """Why a user may not use this URL for a personal route, or None when it is allowed.""" + scheme = urlsplit(url).scheme.lower() + if scheme in _USER_ROUTE_FIXED_HOST_SCHEMES: + return None + if scheme not in _USER_ROUTE_HOST_SCHEMES: + return f"'{scheme or url}' is not available for personal notification routes" + hosts = {urlsplit(url).hostname, _apprise_host(url)} - {None} + for host in hosts: + if not _host_is_public(str(host)): + return f"'{host}' is not a public address" + return None + + +def _allowed_user_urls(urls: list[str]) -> list[str]: + """The URLs a user's routes may send to; the rest are logged and dropped.""" + allowed: list[str] = [] + for url in urls: + problem = user_route_url_problem(url) + if problem is None: + allowed.append(url) + else: + logger.warning("Dropping a personal notification route: %s", problem) + return allowed + + def _resolve_user_routes(user_id: int | None) -> list[dict[str, str]]: normalized_user_id = _normalize_user_id(user_id) if normalized_user_id is None: @@ -638,7 +706,7 @@ def notify_user( return routes = _resolve_user_routes(normalized_user_id) - urls = _resolve_route_urls_for_event(routes, event) + urls = _allowed_user_urls(_resolve_route_urls_for_event(routes, event)) if not urls: return @@ -671,6 +739,10 @@ def _dispatch_user_async( context: NotificationContext, urls: list[str], ) -> None: + # Checked again at send time: the host may resolve differently than when it was queued. + urls = _allowed_user_urls(urls) + if not urls: + return result = _send_admin_event(event, context, urls) if not result.get("success", False): logger.warning( diff --git a/tests/core/test_notifications_user_routes.py b/tests/core/test_notifications_user_routes.py new file mode 100644 index 00000000..c41d2204 --- /dev/null +++ b/tests/core/test_notifications_user_routes.py @@ -0,0 +1,203 @@ +"""A user's own notification routes may not reach private or local hosts.""" + +from __future__ import annotations + +import os +import socket +import tempfile +from unittest.mock import patch + +import pytest +from flask import Flask + +from shelfmark.core import notifications +from shelfmark.core.notifications import ( + NotificationContext, + NotificationEvent, + user_route_url_problem, +) +from shelfmark.core.self_user_routes import register_self_user_routes +from shelfmark.core.user_db import UserDB + + +def _resolves(*addresses: str): + def _getaddrinfo(host, port, *args, **kwargs): + del host, port, args, kwargs + return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (addr, 0)) for addr in addresses] + + return patch("shelfmark.core.notifications.socket.getaddrinfo", side_effect=_getaddrinfo) + + +class TestRouteUrlProblem: + @pytest.mark.parametrize( + "url", + [ + "discord://1234567890/abcdefghijk", + "slack://T1JJ3T3L2/A1BRTD4JD/TIiajkdnlazkcOXrIdevi7FQ/", + "tgram://123456:abcdef/12345", + "pover://user@token", + "pbul://accesstoken", + ], + ) + def test_hosted_services_are_allowed(self, url): + assert user_route_url_problem(url) is None + + @pytest.mark.parametrize( + "url", + [ + "json://127.0.0.1:8080/x", + "jsons://example.com/x", + "form://example.com/x", + "xml://example.com/x", + "https://example.com/hook", + "mailto://user:pass@example.com", + "syslog://", + "file:///tmp/notes.txt", + "script://run", + ], + ) + def test_generic_and_local_schemes_are_refused(self, url): + assert user_route_url_problem(url) is not None + + @pytest.mark.parametrize( + "url", + [ + "ntfy://127.0.0.1/topic", + "ntfy://10.10.10.5:8080/topic", + "ntfys://192.168.1.2/topic", + "ntfy://169.254.169.254/topic", + "ntfy://100.64.0.1/topic", + "ntfy://user:pass@172.16.0.9/topic", + "gotify://localhost/token", + "ntfy://[::1]/topic", + "ntfy://[::ffff:127.0.0.1]/topic", + "ntfy://2130706433/topic", + ], + ) + def test_a_private_or_local_host_is_refused(self, url): + assert user_route_url_problem(url) is not None + + def test_a_public_host_is_allowed(self): + with _resolves("93.184.216.34"): + assert user_route_url_problem("ntfys://ntfy.example.com/topic") is None + + def test_a_name_that_resolves_to_a_private_address_is_refused(self): + with _resolves("10.0.0.7"): + assert user_route_url_problem("ntfys://ntfy.example.com/topic") is not None + + def test_one_private_answer_among_public_ones_is_refused(self): + with _resolves("93.184.216.34", "127.0.0.1"): + assert user_route_url_problem("ntfys://ntfy.example.com/topic") is not None + + def test_a_name_that_does_not_resolve_has_nothing_to_connect_to(self): + with patch("shelfmark.core.notifications.socket.getaddrinfo", side_effect=OSError): + assert user_route_url_problem("ntfys://nowhere.invalid/topic") is None + + +class TestSending: + def _context(self): + return NotificationContext( + event=NotificationEvent.REQUEST_CREATED, title="T", author="A", username="u" + ) + + def test_a_refused_route_is_not_queued(self): + routes = [ + {"event": "all", "url": "json://127.0.0.1:9/x"}, + {"event": "all", "url": "discord://1234567890/abcdefghijk"}, + ] + with ( + patch.object(notifications, "_resolve_user_routes", return_value=routes), + patch.object(notifications._executor, "submit") as submit, + ): + notifications.notify_user(5, NotificationEvent.REQUEST_CREATED, self._context()) + + queued_urls = submit.call_args.args[-1] + assert queued_urls == ["discord://1234567890/abcdefghijk"] + + def test_nothing_is_queued_when_every_route_is_refused(self): + routes = [{"event": "all", "url": "json://127.0.0.1:9/x"}] + with ( + patch.object(notifications, "_resolve_user_routes", return_value=routes), + patch.object(notifications._executor, "submit") as submit, + ): + notifications.notify_user(5, NotificationEvent.REQUEST_CREATED, self._context()) + + submit.assert_not_called() + + def test_the_send_checks_again(self): + with patch.object(notifications, "_send_admin_event") as send: + notifications._dispatch_user_async( + 5, + NotificationEvent.REQUEST_CREATED, + self._context(), + ["json://127.0.0.1:9/x", "discord://1234567890/abcdefghijk"], + ) + + assert send.call_args.args[2] == ["discord://1234567890/abcdefghijk"] + + def test_admin_routes_are_not_restricted(self): + with ( + patch.object( + notifications, + "_resolve_admin_routes", + return_value=[{"event": "all", "url": "json://10.0.0.5:8080/hook"}], + ), + patch.object(notifications._executor, "submit") as submit, + ): + notifications.notify_admin(NotificationEvent.REQUEST_CREATED, self._context()) + + assert submit.call_args.args[-1] == ["json://10.0.0.5:8080/hook"] + + +@pytest.fixture +def client(): + with tempfile.TemporaryDirectory() as tmpdir: + db = UserDB(os.path.join(tmpdir, "shelfmark.db")) + db.initialize() + app = Flask(__name__) + app.config["SECRET_KEY"] = "test-secret" + app.config["TESTING"] = True + register_self_user_routes(app, db) + user = db.create_user(username="alice") + test_client = app.test_client() + with test_client.session_transaction() as sess: + sess["user_id"] = "alice" + sess["db_user_id"] = user["id"] + sess["is_admin"] = False + yield test_client + + +class TestTestEndpoint: + URL = "/api/users/me/notification-preferences/test" + + def test_a_local_target_is_refused_and_nothing_is_sent(self, client): + with patch("shelfmark.config.notifications_settings.send_test_notification") as send: + resp = client.post( + self.URL, json={"routes": [{"event": "all", "url": "json://127.0.0.1:9/x"}]} + ) + + assert resp.status_code == 400 + assert "refused" in resp.json["message"] + send.assert_not_called() + + def test_a_private_host_on_an_allowed_scheme_is_refused(self, client): + with patch("shelfmark.config.notifications_settings.send_test_notification") as send: + resp = client.post( + self.URL, json={"routes": [{"event": "all", "url": "ntfy://10.0.0.5/topic"}]} + ) + + assert resp.status_code == 400 + send.assert_not_called() + + def test_a_hosted_service_is_still_tested(self, client): + with patch( + "shelfmark.config.notifications_settings.send_test_notification", + return_value={"success": True, "message": "ok"}, + ) as send: + resp = client.post( + self.URL, + json={"routes": [{"event": "all", "url": "discord://1234567890/abcdefghijk"}]}, + ) + + assert resp.status_code == 200 + send.assert_called_once()