diff --git a/backend/AGENTS.md b/backend/AGENTS.md index 1ec6e43a93..c3fbeeb4e7 100644 --- a/backend/AGENTS.md +++ b/backend/AGENTS.md @@ -61,3 +61,5 @@ default suite needs no network and no API key. [run interaction policy](docs/RUN_INTERACTION_POLICY.md#hitl-tool-approval). - **Add a schema change with `make migrate-rev MSG="..."`** and follow the migrations guide. + +- **Notification preferences are a send-time trust boundary.** Malformed provider or per-event resolver output must fail closed and remain retryable without consuming an attempt; never treat malformed data as opt-in. diff --git a/backend/app/scheduler/notification_delivery.py b/backend/app/scheduler/notification_delivery.py index 58e3a61de6..b9f2e11a81 100644 --- a/backend/app/scheduler/notification_delivery.py +++ b/backend/app/scheduler/notification_delivery.py @@ -289,11 +289,16 @@ class NotificationDeliveryWorker: event_prefs = self._resolve_notification_event_preferences(delivery.get("owner_user_id") or "") if inspect.isawaitable(event_prefs): event_prefs = await event_prefs + if not isinstance(event_prefs, dict): + raise ValueError("Invalid runtime event preference response") + event_enabled = event_prefs.get(event, False) + if type(event_enabled) is not bool: + raise ValueError("Invalid runtime event preference value") except Exception: logger.warning("Could not verify runtime event preference for delivery %s; parking it", delivery["id"], exc_info=True) await self._delivery_repo.mark_failed(delivery["id"], claim_token=delivery.get("claim_token"), error="could not verify notification preference", count_attempt=False) return False - if isinstance(event_prefs, dict) and not event_prefs.get(event, False): + if not event_enabled: await self._delivery_repo.mark_failed(delivery["id"], claim_token=delivery.get("claim_token"), error=DISABLED_BY_OWNER, count_attempt=False, terminal=True) return False elif preferences.get(event) is False: diff --git a/backend/tests/test_notification_delivery_worker.py b/backend/tests/test_notification_delivery_worker.py index d900bccdbe..93b6f91cc0 100644 --- a/backend/tests/test_notification_delivery_worker.py +++ b/backend/tests/test_notification_delivery_worker.py @@ -1187,3 +1187,36 @@ async def test_wecom_group_notification_for_owner_and_multi_group_binding_isolat assert len(ws_client.calls) == 2 finally: await close_engine() + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "event_preferences", + [ + pytest.param(None, id="none"), + pytest.param([], id="list"), + pytest.param("malformed", id="string"), + pytest.param({"run_completed": "false"}, id="string-value"), + pytest.param({"run_completed": 1}, id="integer-value"), + ], +) +async def test_runtime_event_preference_lookup_fails_closed_on_malformed_shape(event_preferences): + row = _delivery_row(event="run_completed") + row["source_kind"] = "run" + repo = FakeDeliveryRepo([row]) + channel = FakeChannel() + worker = NotificationDeliveryWorker( + delivery_repo=repo, + resolve_channel=lambda provider: channel, + resolve_notification_preferences=lambda owner: {"wecom": True}, + resolve_notification_event_preferences=lambda owner: event_preferences, + ) + + await worker.run_once(now=datetime.now(UTC)) + + # An invalid preference lookup must be retryable, never interpreted as opt-in. + assert repo.sent == [] + assert repo.failed == [("delivery-1", "could not verify notification preference", False)] + assert repo.terminal_failures == [] + assert channel.sent == [] + assert channel.owners == []