AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): accept Web Push subscriptions from Edge, Safari and pre-release Chrome (#15356)
Reinier van der Leer583fc0bb4e43
diff --git a/autogpt_platform/backend/backend/api/features/push/routes_test.py b/autogpt_platform/backend/backend/api/features/push/routes_test.pyindex 5e44003bf7..d6a5deeb7d 100644--- a/autogpt_platform/backend/backend/api/features/push/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/push/routes_test.py@@ -52,7 +52,15 @@ def test_get_vapid_public_key_empty(mocker):assert data["public_key"] == ""-def test_subscribe_push(mocker, test_user_id):+ "endpoint",+ [+ "https://fcm.googleapis.com/fcm/send/abc123",+ "https://jmt17.google.com/fcm/send/abc123",+ "https://wns2-par02p.notify.windows.com/w/?token=abc123",+ ],+)+def test_subscribe_push(mocker, test_user_id, endpoint):mock_upsert = mocker.patch("backend.api.features.push.routes.upsert_push_subscription",new_callable=AsyncMock,@@ -61,7 +69,7 @@ def test_subscribe_push(mocker, test_user_id):response = client.post("/subscribe",json={- "endpoint": "https://fcm.googleapis.com/fcm/send/abc123",+ "endpoint": endpoint,"keys": {"p256dh": "test-p256dh-key","auth": "test-auth-key",@@ -73,7 +81,7 @@ def test_subscribe_push(mocker, test_user_id):assert response.status_code == 204mock_upsert.assert_awaited_once_with(user_id=test_user_id,- endpoint="https://fcm.googleapis.com/fcm/send/abc123",+ endpoint=endpoint,p256dh="test-p256dh-key",auth="test-auth-key",user_agent="Mozilla/5.0 Test",diff --git a/autogpt_platform/backend/backend/data/push_sender_test.py b/autogpt_platform/backend/backend/data/push_sender_test.pyindex 2d095141ac..075ee1e8ed 100644--- a/autogpt_platform/backend/backend/data/push_sender_test.py+++ b/autogpt_platform/backend/backend/data/push_sender_test.py@@ -196,11 +196,21 @@ class TestSendPushForUser:assert "https://fcm.googleapis.com/fcm/send/sub/2" in endpoints_called@pytest.mark.asyncio- async def test_webpush_called_with_correct_args(self, mocker, mock_db_client):+ @pytest.mark.parametrize(+ "endpoint",+ [+ "https://fcm.googleapis.com/fcm/send/sub/1",+ "https://jmt17.google.com/fcm/send/sub/1",+ "https://wns2-par02p.notify.windows.com/w/?token=sub1",+ ],+ )+ async def test_webpush_called_with_correct_args(+ self, mocker, mock_db_client, endpoint+ ):mocker.patch.object(push_sender, "_settings", _make_settings())sub = _make_subscription(user_id="user-1",- endpoint="https://fcm.googleapis.com/fcm/send/sub/1",+ endpoint=endpoint,p256dh="key-p256dh",auth="key-auth",)@@ -212,12 +222,13 @@ class TestSendPushForUser:mock_webpush.assert_called_once()call_kwargs = mock_webpush.call_args.kwargsassert call_kwargs["subscription_info"] == {- "endpoint": "https://fcm.googleapis.com/fcm/send/sub/1",+ "endpoint": endpoint,"keys": {"p256dh": "key-p256dh", "auth": "key-auth"},}assert call_kwargs["vapid_private_key"] == "vapid-private"assert call_kwargs["vapid_claims"] == {"sub": "mailto:[email protected]"}assert isinstance(call_kwargs["data"], str)+ mock_db_client.delete_push_subscription.assert_not_awaited()@pytest.mark.asyncioasync def test_removes_subscription_on_410_gone(self, mocker, mock_db_client):diff --git a/autogpt_platform/backend/backend/data/push_subscription.py b/autogpt_platform/backend/backend/data/push_subscription.pyindex 2cfc6d5c7d..10c509d18d 100644--- a/autogpt_platform/backend/backend/data/push_subscription.py+++ b/autogpt_platform/backend/backend/data/push_subscription.py@@ -6,7 +6,7 @@ from datetime import datetime, timezonefrom prisma.models import PushSubscriptionfrom pydantic import BaseModel-from backend.util.request import validate_url_host+from backend.util.request import parse_url, validate_url_hostlogger = logging.getLogger(__name__)@@ -14,14 +14,18 @@ logger = logging.getLogger(__name__)# Hostnames of legitimate Web Push services. Endpoints submitted by# clients must match one of these; everything else is rejected to prevent# the backend (which POSTs to the stored URL via pywebpush) from being-# used as an SSRF primitive against internal infrastructure. Covers Chrome/-# Edge/Brave (FCM), Firefox (Autopush), and Safari/macOS (Apple Web Push).+# used as an SSRF primitive against internal infrastructure. Chrome uses FCM+# (jmt17.google.com on pre-release channels), Firefox uses Autopush._PUSH_SERVICE_HOSTNAMES: list[str] = ["fcm.googleapis.com",+ "jmt17.google.com","updates.push.services.mozilla.com",- "web.push.apple.com",]+# Edge on Windows (WNS) and Safari (Apple Web Push) issue endpoints on varying+# subdomains of a domain the vendor owns, so any subdomain of these is accepted.+_PUSH_SERVICE_DOMAIN_SUFFIXES = (".notify.windows.com", ".push.apple.com")+# Cap on concurrent push subscriptions per user — one entry per device/browser# is typical, so this comfortably covers real usage while preventing an# authenticated user from registering unbounded endpoints to amplify outbound@@ -41,8 +45,12 @@ async def validate_push_endpoint(endpoint: str) -> None:Called at subscribe time and again before dispatch (defense-in-depth againstrows written before this check existed or via future codepaths)."""+ hostname = parse_url(endpoint).hostname or ""+ vendor_subdomain = (+ [hostname] if hostname.endswith(_PUSH_SERVICE_DOMAIN_SUFFIXES) else []+ )parsed, is_trusted, _ = await validate_url_host(- endpoint, trusted_hostnames=_PUSH_SERVICE_HOSTNAMES+ endpoint, trusted_hostnames=_PUSH_SERVICE_HOSTNAMES + vendor_subdomain)if parsed.scheme != "https":raise ValueError("Push endpoint must use https://")diff --git a/autogpt_platform/backend/backend/data/push_subscription_test.py b/autogpt_platform/backend/backend/data/push_subscription_test.pyindex 282e8f6306..f41085465a 100644--- a/autogpt_platform/backend/backend/data/push_subscription_test.py+++ b/autogpt_platform/backend/backend/data/push_subscription_test.py@@ -278,8 +278,10 @@ class TestValidatePushEndpoint:"endpoint",["https://fcm.googleapis.com/fcm/send/abc",+ "https://jmt17.google.com/fcm/send/abc","https://updates.push.services.mozilla.com/wpush/v2/xyz","https://web.push.apple.com/some-token",+ "https://wns2-par02p.notify.windows.com/w/?token=abc",],)async def test_allows_known_push_services(self, endpoint):@@ -307,6 +309,31 @@ class TestValidatePushEndpoint:with pytest.raises(ValueError):await push_subscription.validate_push_endpoint(endpoint)+ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "endpoint",+ [+ "https://jmt17.google.com.evil.example/fcm/send/abc",+ "https://evil-jmt17.google.com/fcm/send/abc",+ "https://www.google.com/fcm/send/abc",+ "https://notify.windows.com.evil.example/w/?token=abc",+ "https://evilnotify.windows.com/w/?token=abc",+ "https://windows.com/w/?token=abc",+ "https://push.apple.com.evil.example/some-token",+ "https://evilpush.apple.com/some-token",+ "https://apple.com/some-token",+ ],+ )+ async def test_rejects_lookalike_hosts(self, mocker, endpoint):+ # Resolve publicly so the allowlist, not a DNS failure, does the refusing.+ mocker.patch(+ "backend.util.request._resolve_host",+ new_callable=AsyncMock,+ return_value=["8.8.8.8"],+ )+ with pytest.raises(ValueError, match="not a recognised Web Push service"):+ await push_subscription.validate_push_endpoint(endpoint)+@pytest.mark.asyncioasync def test_rejects_non_http_scheme(self):with pytest.raises(ValueError):