Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(accounts): Allow disabling email sending to unknown addresses #3528

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Note worthy changes
- A configurable timeout (``SOCIALACCOUNT_REQUESTS_TIMEOUT``) is now applied to
all upstream requests.

- Added a setting ``ACCOUNT_EMAIL_UNKNOWN_ACCOUNTS`` to disable sending of
pennersr marked this conversation as resolved.
Show resolved Hide resolved
emails to unknown accounts.


Backwards incompatible changes
------------------------------
Expand Down
4 changes: 4 additions & 0 deletions allauth/account/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ def PASSWORD_RESET_TOKEN_GENERATOR(self):
token_generator = EmailAwarePasswordResetTokenGenerator
return token_generator

@property
def EMAIL_UNKNOWN_ACCOUNTS(self):
return self._setting("EMAIL_UNKNOWN_ACCOUNTS", True)

@property
def REAUTHENTICATION_TIMEOUT(self):
return self._setting("REAUTHENTICATION_TIMEOUT", 300)
Expand Down
3 changes: 2 additions & 1 deletion allauth/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ def clean_email(self):
def save(self, request, **kwargs):
email = self.cleaned_data["email"]
if not self.users:
self._send_unknown_account_mail(request, email)
if app_settings.EMAIL_UNKNOWN_ACCOUNTS:
self._send_unknown_account_mail(request, email)
else:
self._send_password_reset_mail(request, email, self.users, **kwargs)
return email
Expand Down
24 changes: 24 additions & 0 deletions allauth/account/tests/test_reset_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,36 @@
from django.test.utils import override_settings
from django.urls import reverse

import pytest

from allauth.account import app_settings
from allauth.account.forms import ResetPasswordForm
from allauth.account.models import EmailAddress
from allauth.tests import TestCase


@pytest.mark.django_db
def test_reset_password_unknown_account(client, settings):
settings.ACCOUNT_PREVENT_ENUMERATION = True
client.post(
reverse("account_reset_password"),
data={"email": "unknown@example.org"},
)
assert len(mail.outbox) == 1
assert mail.outbox[0].to == ["unknown@example.org"]


@pytest.mark.django_db
def test_reset_password_unknown_account_disabled(client, settings):
settings.ACCOUNT_PREVENT_ENUMERATION = True
settings.ACCOUNT_EMAIL_UNKNOWN_ACCOUNTS = False
client.post(
reverse("account_reset_password"),
data={"email": "unknown@example.org"},
)
assert len(mail.outbox) == 0


@override_settings(
ACCOUNT_PREVENT_ENUMERATION=False,
ACCOUNT_DEFAULT_HTTP_PROTOCOL="https",
Expand Down
4 changes: 4 additions & 0 deletions docs/account/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Available settings:
Subject-line prefix to use for email messages sent. By default, the
name of the current ``Site`` (``django.contrib.sites``) is used.

``ACCOUNT_EMAIL_UNKNOWN_ACCOUNTS`` (default: ``True``)
Configures whether password reset attempts for email addresses which do not
have an account result in sending an email.

``ACCOUNT_DEFAULT_HTTP_PROTOCOL`` (default: ``"http"``)
The default protocol used for when generating URLs, e.g. for the
password forgotten procedure. Note that this is a default only --
Expand Down