Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Confirm email changes with a signed link instead of a pending address

Claude2caf72723def
diff --git a/mwmbl/evaluation/api.py b/mwmbl/evaluation/api.py
index b81ab0e..0e1837a 100644
--- a/mwmbl/evaluation/api.py
+++ b/mwmbl/evaluation/api.py
@@ -15,9 +15,8 @@ router = Router(tags=["Evaluation"])
def check_email_verified(request):
"""Check if user's email is verified."""
- # Not the first address: while an email change awaits confirmation, the user also has
- # the new, unverified one.
- if not request.user.emailaddress_set.filter(verified=True).exists():
+ from_email_address = request.user.emailaddress_set.first()
+ if not from_email_address or not from_email_address.verified:
raise InvalidRequest("Email address is not verified", status=403) # 403 Forbidden
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.py
index c4ab930..0cbc20a 100644
--- a/mwmbl/platform/api.py
+++ b/mwmbl/platform/api.py
@@ -1,11 +1,14 @@
import logging
+from urllib.parse import urlencode
from allauth.account.adapter import get_adapter
from allauth.account.models import EmailAddress, EmailConfirmationHMAC
from allauth.account.utils import send_email_confirmation, setup_user_email
from django.conf import settings
from django.core import signing
+from django.core.cache import cache
from django.core.exceptions import ValidationError
+from django.core.mail import send_mail
from django.core.validators import validate_email
from django.db import transaction
from django.db.models import F
@@ -64,6 +67,7 @@ from mwmbl.platform.schemas import (
CombinedSearchPreference,
CombinedSearchUsageResponse,
ConfirmEmail,
+ ConfirmEmailChangeRequest,
CreateApiKeyRequest,
DeviceResponse,
DomainSubmissionSchema,
@@ -116,9 +120,8 @@ def _refuse_email_like_username(username: str) -> None:
def check_email_verified(request):
- # Not the first address: while an email change awaits confirmation, the user also has
- # the new, unverified one.
- if not request.user.emailaddress_set.filter(verified=True).exists():
+ from_email_address = request.user.emailaddress_set.first()
+ if not from_email_address.verified:
raise InvalidRequest("Email address is not verified", status=403)
@@ -135,12 +138,7 @@ def check_email_verified(request):
),
)
def register(request, registration: Registration):
- # Pending email changes count too: once confirmed, they become their user's email.
- email_taken = (
- MwmblUser.objects.filter(email__iexact=registration.email).exists()
- or EmailAddress.objects.filter(email__iexact=registration.email).exists()
- )
- if email_taken:
+ if _email_taken(registration.email):
raise InvalidRequest("Email already registered")
username = registration.username or generate_username()
@@ -1222,21 +1220,16 @@ def get_current_user(request):
user = request.user
billing = getattr(user, "billing", None)
spend_cents = billing.max_monthly_spend_cents if billing else 0
- pending_email = _pending_email_addresses(user).first()
+ email_address = user.emailaddress_set.first()
return UserProfileResponse(
username=user.username,
email=user.email,
plan="free" if spend_cents == 0 else "pay-as-you-go",
- email_confirmed=user.emailaddress_set.filter(verified=True).exists(),
- pending_email=pending_email.email if pending_email else None,
+ email_confirmed=email_address.verified if email_address else False,
date_joined=user.date_joined,
)
-def _pending_email_addresses(user: MwmblUser):
- return user.emailaddress_set.filter(verified=False).exclude(email__iexact=user.email)
-
-
@router.put(
"/user/username",
auth=JWTAuth(),
@@ -1271,15 +1264,30 @@ def change_username(request, body: ChangeUsernameRequest):
return {"status": "ok", "username": new_username}
+_EMAIL_CHANGE_SALT = "email-change"
+EMAIL_CHANGE_MAX_AGE_SECONDS = 3 * 24 * 60 * 60
+# One confirmation email per user per window, so the endpoint can't be used to flood an inbox.
+EMAIL_CHANGE_COOLDOWN_SECONDS = 60
+EMAIL_CHANGE_COOLDOWN_KEY = "email-change-sent:{user_id}"
+
+
+def _email_taken(email: str, by_other_than: MwmblUser | None = None) -> bool:
+ users = MwmblUser.objects.filter(email__iexact=email)
+ addresses = EmailAddress.objects.filter(email__iexact=email)
+ if by_other_than is not None:
+ users = users.exclude(id=by_other_than.id)
+ addresses = addresses.exclude(user=by_other_than)
+ return users.exists() or addresses.exists()
+
+
@router.post(
"/user/email",
auth=JWTAuth(),
summary="Change email address",
description=(
"Start changing the authenticated user's email address. Requires the current password. "
- "A confirmation email is sent to the new address; the current address stays in use until "
- "the new one is confirmed, and is then replaced by it. Requesting another change replaces "
- "a pending one."
+ "A link is emailed to the new address; the current address stays in use until the link is "
+ "followed, which calls `/platform/user/email/confirm`. One email is sent per user per minute."
),
tags=["Users"],
)
@@ -1295,25 +1303,72 @@ def change_email(request, body: ChangeEmailRequest):
raise InvalidRequest("Enter a valid email address.")
if new_email.lower() == user.email.lower():
raise InvalidRequest("That is already your email address.")
+ if _email_taken(new_email, by_other_than=user):
+ raise InvalidRequest("Email already registered")
+
+ cooldown_key = EMAIL_CHANGE_COOLDOWN_KEY.format(user_id=user.id)
+ if not cache.add(cooldown_key, 1, timeout=EMAIL_CHANGE_COOLDOWN_SECONDS):
+ raise InvalidRequest("A confirmation email was sent a moment ago. Please wait a minute.", status=429)
- used_by_another_user = (
- MwmblUser.objects.filter(email__iexact=new_email).exclude(id=user.id).exists()
- or EmailAddress.objects.filter(email__iexact=new_email).exclude(user=user).exists()
+ # Nothing is stored until the link is followed, so an unconfirmed address neither logs in
+ # nor stops anyone else using it. The current email in the token voids the link once the
+ # email changes.
+ token = signing.dumps(
+ {"user_id": user.id, "old_email": user.email, "new_email": new_email}, salt=_EMAIL_CHANGE_SALT
)
- if used_by_another_user:
- raise InvalidRequest("Email already registered")
+ confirm_url = f"{settings.FRONTEND_URL}/account/confirm-email-change?{urlencode({'token': token})}"
+ send_mail(
+ "Confirm your new Mwmbl email address",
+ f"Hi {user.username},\n\n"
+ f"To use this address for your Mwmbl account, follow this link within three days:\n\n"
+ f"{confirm_url}\n\n"
+ "If you didn't ask for this, you can ignore this email.",
+ settings.DEFAULT_FROM_EMAIL,
+ [new_email],
+ )
+
+ return {"status": "ok", "message": "Check your new email address for a confirmation link."}
+
+
+ "/user/email/confirm",
+ summary="Confirm an email address change",
+ description=(
+ "Complete an email change with the token from the link emailed by `/platform/user/email`. "
+ "The account's email becomes the new, verified address, and the old address is told."
+ ),
+ tags=["Users"],
+)
+def confirm_email_change(request, body: ConfirmEmailChangeRequest):
+ try:
+ change = signing.loads(body.token, salt=_EMAIL_CHANGE_SALT, max_age=EMAIL_CHANGE_MAX_AGE_SECONDS)
+ except signing.BadSignature:
+ raise InvalidRequest("This link is invalid or has expired.")
with transaction.atomic():
- # Serialises concurrent changes by one user, which would otherwise each leave a pending address.
- MwmblUser.objects.select_for_update().get(id=user.id)
- _pending_email_addresses(user).delete()
- EmailAddress.objects.add_email(request, user, new_email, confirm=True)
+ user = MwmblUser.objects.select_for_update().filter(id=change["user_id"]).first()
+ if user is None or user.email != change["old_email"]:
+ raise InvalidRequest("This link is no longer valid.")
+ new_email = change["new_email"]
+ if _email_taken(new_email, by_other_than=user):
+ raise InvalidRequest("Email already registered")
+
+ user.email = new_email
+ user.save(update_fields=["email"])
+ user.emailaddress_set.all().delete()
+ EmailAddress.objects.create(user=user, email=new_email, verified=True, primary=True)
+
+ # Warns the owner if someone else, holding their password, made the change.
+ send_mail(
+ "Your Mwmbl email address has changed",
+ f"Hi {user.username},\n\n"
+ f"The email address for your Mwmbl account has been changed to {new_email}.\n\n"
+ f"If you didn't do this, contact {settings.DEFAULT_FROM_EMAIL} straight away.",
+ settings.DEFAULT_FROM_EMAIL,
+ [change["old_email"]],
+ )
- return {
- "status": "ok",
- "pending_email": new_email,
- "message": "Check your new email address for a confirmation link.",
- }
+ return {"status": "ok", "username": user.username, "email": new_email}
@router.get(
diff --git a/mwmbl/platform/schemas.py b/mwmbl/platform/schemas.py
index 371f422..3f5247f 100644
--- a/mwmbl/platform/schemas.py
+++ b/mwmbl/platform/schemas.py
@@ -13,10 +13,6 @@ class UserProfileResponse(Schema):
email: str
plan: str
email_confirmed: bool
- pending_email: Optional[str] = Field(
- default=None,
- description="A new email address awaiting confirmation, which replaces `email` once confirmed.",
- )
date_joined: datetime
@@ -144,6 +140,10 @@ class ChangeEmailRequest(Schema):
password: str = Field(description="The account's current password.")
+class ConfirmEmailChangeRequest(Schema):
+ token: str = Field(description="The token from the link in the confirmation email.")
+
+
class ResetPasswordRequest(Schema):
email: str
key: str
diff --git a/mwmbl/settings_common.py b/mwmbl/settings_common.py
index 3dbc7ea..66b82db 100644
--- a/mwmbl/settings_common.py
+++ b/mwmbl/settings_common.py
@@ -149,12 +149,11 @@ ACCOUNT_USERNAME_REQUIRED = False
# mwmbl.models.SEED_CRAWLER_USERNAME, which settings cannot import.
ACCOUNT_USERNAME_BLACKLIST = ["mwmbl-seed-crawler"]
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
-# A new address waits beside the current one until it is confirmed, then replaces it.
-ACCOUNT_CHANGE_EMAIL = True
-ACCOUNT_MAX_EMAIL_ADDRESSES = 2
-
DEFAULT_FROM_EMAIL = "[email protected]"
+# Where links in emails, such as the one confirming an email change, send the user.
+FRONTEND_URL = "https://mwmbl.org"
+
LOGIN_REDIRECT_URL = "/"
FOOTER_LINKS = [
diff --git a/test/test_platform_user_api.py b/test/test_platform_user_api.py
index 76d3017..abb0de6 100644
--- a/test/test_platform_user_api.py
+++ b/test/test_platform_user_api.py
@@ -1,13 +1,17 @@
"""
-Per-user platform endpoints: agreements, devices, the profile, and the moderator's
+Per-user platform endpoints: agreements, devices, the profile and changes to it, and the moderator's
re-crawl of a submission.
"""
+import re
+from urllib.parse import parse_qs, urlparse
+
import pytest
-from allauth.account.models import EmailAddress, EmailConfirmationHMAC
+from allauth.account.models import EmailAddress
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.core import mail
+from django.core.cache import cache
from ninja_jwt.tokens import RefreshToken
from mwmbl.models import Device, DomainEvidence, DomainSubmission, UserAgreement
@@ -30,6 +34,8 @@ def _auth(user) -> dict:
@pytest.fixture
def member(db):
+ # The email change cooldown lives in the cache.
+ cache.clear()
return _user("member")
@@ -158,11 +164,29 @@ def test_taken_reserved_and_invalid_usernames_are_refused(client, member, userna
assert member.username == "member"
+def _confirm_url_sent_to(address: str) -> str:
+ links = [re.search(r"https://\S+/confirm-email-change\S+", m.body) for m in mail.outbox if m.to == [address]]
+ return [link for link in links if link][-1].group()
+
+
+def _confirm_email_change(client, address: str):
+ token = parse_qs(urlparse(_confirm_url_sent_to(address)).query)["token"][0]
+ return client.post(f"{BASE}/user/email/confirm", data={"token": token}, content_type="application/json")
+
+
+def _log_in(client, username: str):
+ return client.post(
+ f"{BASE}/token/pair",
+ data={"username": username, "password": "correctpassword"},
+ content_type="application/json",
+ )
+
+
def test_changing_email_needs_the_password(client, member):
response = _change_email(client, member, "[email protected]", password="wrong")
assert response.status_code == 403
- assert not EmailAddress.objects.filter(email="[email protected]").exists()
+ assert mail.outbox == []
@pytest.mark.parametrize(
@@ -180,19 +204,14 @@ def test_a_new_email_replaces_the_old_one_once_confirmed(client, member):
response = _change_email(client, member, "[email protected]")
assert response.status_code == 200
- assert mail.outbox[-1].to == ["[email protected]"]
- # The old address keeps working until the new one is confirmed.
+ assert _confirm_url_sent_to("[email protected]").startswith("https://mwmbl.org/account/confirm-email-change?")
+ # Until the link is followed, nothing changes and the new address can't log in.
member.refresh_from_db()
assert member.email == "[email protected]"
- assert client.get(f"{BASE}/protected", headers=_auth(member)).status_code == 200
- assert client.get(f"{BASE}/user", headers=_auth(member)).json()["pending_email"] == "[email protected]"
+ assert not EmailAddress.objects.filter(email="[email protected]").exists()
+ assert _log_in(client, "[email protected]").status_code == 401
- pending = EmailAddress.objects.get(email="[email protected]")
- confirmed = client.post(
- f"{BASE}/confirm-email",
- data={"email": "[email protected]", "key": EmailConfirmationHMAC(pending).key},
- content_type="application/json",
- )
+ confirmed = _confirm_email_change(client, "[email protected]")
assert confirmed.status_code == 200
member.refresh_from_db()
@@ -200,65 +219,49 @@ def test_a_new_email_replaces_the_old_one_once_confirmed(client, member):
assert list(EmailAddress.objects.filter(user=member).values_list("email", "verified", "primary")) == [
("[email protected]", True, True)
]
+ assert mail.outbox[-1].to == ["[email protected]"]
+ assert _log_in(client, "[email protected]").status_code == 200
+ assert _log_in(client, "[email protected]").status_code == 401
-def test_login_moves_to_the_new_email_once_confirmed(client, member):
+def test_a_confirmation_link_works_only_once(client, member):
_change_email(client, member, "[email protected]")
- pending = EmailAddress.objects.get(email="[email protected]")
- client.post(
- f"{BASE}/confirm-email",
- data={"email": "[email protected]", "key": EmailConfirmationHMAC(pending).key},
- content_type="application/json",
- )
- def log_in(username):
- return client.post(
- f"{BASE}/token/pair",
- data={"username": username, "password": "correctpassword"},
- content_type="application/json",
- )
+ _confirm_email_change(client, "[email protected]")
+ replayed = _confirm_email_change(client, "[email protected]")
- assert log_in("[email protected]").status_code == 200
- assert log_in("[email protected]").status_code == 401
+ assert replayed.status_code == 400
-def test_an_email_pending_on_another_account_cannot_be_registered(client, member):
- _change_email(client, member, "[email protected]")
-
- response = client.post(
- f"{BASE}/register",
- data={"email": "[email protected]", "password": "anotherpassword"},
- content_type="application/json",
- )
+def test_a_tampered_token_is_refused(client, member):
+ response = client.post(f"{BASE}/user/email/confirm", data={"token": "forged"}, content_type="application/json")
assert response.status_code == 400
-def test_confirming_an_email_another_account_has_verified_is_refused(client, member):
+def test_an_unconfirmed_change_does_not_stop_anyone_else_registering_the_address(client, member):
_change_email(client, member, "[email protected]")
- pending = EmailAddress.objects.get(email="[email protected]")
- stranger = _user("stranger")
- EmailAddress.objects.create(user=stranger, email="[email protected]", verified=True)
- response = client.post(
- f"{BASE}/confirm-email",
- data={"email": "[email protected]", "key": EmailConfirmationHMAC(pending).key},
+ registered = client.post(
+ f"{BASE}/register",
+ data={"email": "[email protected]", "password": "anotherpassword"},
content_type="application/json",
)
+ confirmed = _confirm_email_change(client, "[email protected]")
- assert response.status_code == 400
+ assert registered.status_code == 200
+ assert confirmed.status_code == 400
member.refresh_from_db()
assert member.email == "[email protected]"
-def test_a_second_email_change_replaces_the_pending_one(client, member):
- _change_email(client, member, "[email protected]")
- _change_email(client, member, "[email protected]")
+def test_confirmation_emails_are_rate_limited(client, member):
+ first = _change_email(client, member, "[email protected]")
+ second = _change_email(client, member, "[email protected]")
- assert sorted(EmailAddress.objects.filter(user=member).values_list("email", flat=True)) == [
- ]
+ assert first.status_code == 200
+ assert second.status_code == 429
+ assert [m.to for m in mail.outbox] == [["[email protected]"]]
def test_refetching_clears_the_old_evidence_and_queues_a_crawl(client, member, moderator, monkeypatch):