An open source, non-profit web search engine
Harden marketing unsubscribe: POST-only, deterministic consent state
Daoud Clarke3df01005ecff
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.pyindex e00d086..e3148a3 100644--- a/mwmbl/platform/api.py+++ b/mwmbl/platform/api.py@@ -5,7 +5,6 @@ from allauth.account.models import EmailConfirmationHMACfrom allauth.account.utils import setup_user_email, send_email_confirmationfrom django.conf import settingsfrom django.core import signing-from django.http import HttpResponsefrom django.utils import timezonefrom ninja import Routerfrom ninja.pagination import paginate@@ -366,8 +365,8 @@ def _record_agreements(user: MwmblUser, agreement_types: list) -> None:)-def _record_marketing_consent(user: MwmblUser, source: MarketingSource, opted_in: bool) -> None:- MarketingConsent.objects.create(user=user, source=source, opted_in=opted_in)+def _record_marketing_consent(user: MwmblUser, source: MarketingSource, opted_in: bool) -> MarketingConsent:+ return MarketingConsent.objects.create(user=user, source=source, opted_in=opted_in)_UNSUBSCRIBE_SALT = "marketing-unsubscribe"@@ -500,7 +499,7 @@ def get_marketing_consent(request) -> list[MarketingConsentResponse]:for source in MarketingSource:latest = (MarketingConsent.objects.filter(user=request.user, source=source)- .order_by("-timestamp")+ .order_by("-timestamp", "-id").first())if latest:@@ -526,11 +525,11 @@ def get_marketing_consent(request) -> list[MarketingConsentResponse]:)def update_marketing_consent(request, body: MarketingConsentRequest):check_email_verified(request)- _record_marketing_consent(request.user, body.source, body.opted_in)+ consent = _record_marketing_consent(request.user, body.source, body.opted_in)return MarketingConsentResponse(- source=body.source,- opted_in=body.opted_in,- timestamp=timezone.now(),+ source=consent.source,+ opted_in=consent.opted_in,+ timestamp=consent.timestamp,)@@ -562,25 +561,6 @@ def unsubscribe_marketing(request, token: str):return {"status": "ok", "message": "You have been unsubscribed from marketing emails."}- "/marketing/unsubscribe",- auth=None,- summary="One-click unsubscribe (browser-friendly)",- description=(- "Browser-friendly variant of the one-click unsubscribe endpoint, for users who open "- "the `List-Unsubscribe` link directly. Records the opt-out and returns a confirmation page."- ),- tags=["Marketing"],-)-def unsubscribe_marketing_page(request, token: str):- _unsubscribe_from_token(token)- return HttpResponse(- "<html><body><h1>You have been unsubscribed</h1>"- "<p>You will no longer receive marketing emails from Mwmbl.</p></body></html>",- content_type="text/html",- )--# ---------------------------------------------------------------------------# API key management# ---------------------------------------------------------------------------diff --git a/test/test_marketing_consent.py b/test/test_marketing_consent.pyindex 9e68c83..322fb24 100644--- a/test/test_marketing_consent.py+++ b/test/test_marketing_consent.py@@ -146,9 +146,11 @@ def test_unsubscribe_rejects_bad_token(client, verified_user):@pytest.mark.django_db-def test_unsubscribe_get_renders_confirmation(client, verified_user):+def test_unsubscribe_get_not_allowed(client, verified_user):+ # The API only exposes a POST endpoint; the browser-facing unsubscribe page+ # lives in the front-end (mwmbl.org), which calls this POST via JS. A GET+ # (e.g. from a link prefetcher or scanner) must never record an opt-out.token = make_unsubscribe_token(verified_user, MarketingSource.API)response = client.get(f"{UNSUBSCRIBE_URL}?token={token}")- assert response.status_code == 200- assert b"unsubscribed" in response.content.lower()- assert MarketingConsent.objects.filter(user=verified_user).count() == 1+ assert response.status_code == 405+ assert MarketingConsent.objects.filter(user=verified_user).count() == 0