-
Notifications
You must be signed in to change notification settings - Fork 1k
chore: disallow password resets to unverified email #18088
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
Merged
miketheman
merged 7 commits into
pypi:main
from
miketheman:miketheman/disallow-password-resets-for-unverified-emails
May 9, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e5d518
chore: disallow password reset to unverified email
miketheman 8a71a5a
feat: reject unverified emails in password reset
miketheman 95ce3c2
refactor: send notification email instead of flash
miketheman 6df56fa
make translations
miketheman d79255c
remove unused param
miketheman 7308421
refactor: address PR feedback
miketheman 402a9f7
make translations
miketheman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1845,26 +1845,30 @@ def test_get(self, pyramid_request, user_service): | |
] | ||
|
||
def test_request_password_reset( | ||
self, monkeypatch, pyramid_request, pyramid_config, user_service, token_service | ||
self, | ||
monkeypatch, | ||
pyramid_request, | ||
user_service, | ||
token_service, | ||
mocker, | ||
): | ||
stub_user = pretend.stub( | ||
id=pretend.stub(), | ||
username=pretend.stub(), | ||
emails=[pretend.stub(email="[email protected]")], | ||
can_reset_password=True, | ||
record_event=pretend.call_recorder(lambda *a, **kw: None), | ||
user = UserFactory.create(with_verified_primary_email=True) | ||
mock_record_event = mocker.patch( | ||
"warehouse.accounts.models.HasEvents.record_event", | ||
autospec=True, | ||
return_value=True, | ||
) | ||
pyramid_request.method = "POST" | ||
token_service.dumps = pretend.call_recorder(lambda a: "TOK") | ||
user_service.get_user_by_username = pretend.call_recorder(lambda a: stub_user) | ||
user_service.get_user_by_username = pretend.call_recorder(lambda a: user) | ||
pyramid_request.find_service = pretend.call_recorder( | ||
lambda interface, **kw: { | ||
IUserService: user_service, | ||
ITokenService: token_service, | ||
}[interface] | ||
) | ||
form_obj = pretend.stub( | ||
username_or_email=pretend.stub(data=stub_user.username), | ||
username_or_email=pretend.stub(data=user.username), | ||
validate=pretend.call_recorder(lambda: True), | ||
) | ||
form_class = pretend.call_recorder(lambda d, user_service: form_obj) | ||
|
@@ -1879,9 +1883,7 @@ def test_request_password_reset( | |
result = views.request_password_reset(pyramid_request, _form_class=form_class) | ||
|
||
assert result == {"n_hours": n_hours} | ||
assert user_service.get_user_by_username.calls == [ | ||
pretend.call(stub_user.username) | ||
] | ||
assert user_service.get_user_by_username.calls == [pretend.call(user.username)] | ||
assert pyramid_request.find_service.calls == [ | ||
pretend.call(IUserService, context=None), | ||
pretend.call(ITokenService, name="password"), | ||
|
@@ -1891,22 +1893,21 @@ def test_request_password_reset( | |
pretend.call(pyramid_request.POST, user_service=user_service) | ||
] | ||
assert send_password_reset_email.calls == [ | ||
pretend.call(pyramid_request, (stub_user, None)) | ||
] | ||
assert stub_user.record_event.calls == [ | ||
pretend.call( | ||
tag=EventTag.Account.PasswordResetRequest, | ||
request=pyramid_request, | ||
) | ||
pretend.call(pyramid_request, (user, user.primary_email)) | ||
] | ||
mock_record_event.assert_called_once_with( | ||
user, | ||
tag=EventTag.Account.PasswordResetRequest, | ||
request=pyramid_request, | ||
) | ||
|
||
def test_request_password_reset_with_email( | ||
self, monkeypatch, pyramid_request, pyramid_config, user_service, token_service | ||
): | ||
stub_user = pretend.stub( | ||
id=uuid.uuid4(), | ||
email="[email protected]", | ||
emails=[pretend.stub(email="[email protected]")], | ||
emails=[pretend.stub(email="[email protected]", verified=True)], | ||
can_reset_password=True, | ||
record_event=pretend.call_recorder(lambda *a, **kw: None), | ||
) | ||
|
@@ -1977,8 +1978,8 @@ def test_request_password_reset_with_non_primary_email( | |
id=uuid.uuid4(), | ||
email="[email protected]", | ||
emails=[ | ||
pretend.stub(email="[email protected]"), | ||
pretend.stub(email="[email protected]"), | ||
pretend.stub(email="[email protected]", verified=True), | ||
pretend.stub(email="[email protected]", verified=True), | ||
], | ||
can_reset_password=True, | ||
record_event=pretend.call_recorder(lambda *a, **kw: None), | ||
|
@@ -2055,7 +2056,7 @@ def test_too_many_password_reset_requests( | |
stub_user = pretend.stub( | ||
id=uuid.uuid4(), | ||
email="[email protected]", | ||
emails=[pretend.stub(email="[email protected]")], | ||
emails=[pretend.stub(email="[email protected]", verified=True)], | ||
can_reset_password=True, | ||
record_event=pretend.call_recorder(lambda *a, **kw: None), | ||
) | ||
|
@@ -2100,26 +2101,26 @@ def test_too_many_password_reset_requests( | |
pretend.call(stub_user.id) | ||
] | ||
|
||
def test_password_reset_prohibited( | ||
self, monkeypatch, pyramid_request, pyramid_config, user_service | ||
): | ||
stub_user = pretend.stub( | ||
id=pretend.stub(), | ||
username=pretend.stub(), | ||
emails=[pretend.stub(email="[email protected]")], | ||
can_reset_password=False, | ||
record_event=pretend.call_recorder(lambda *a, **kw: None), | ||
def test_password_reset_prohibited(self, pyramid_request, user_service, mocker): | ||
user = UserFactory.create( | ||
with_verified_primary_email=True, | ||
prohibit_password_reset=True, | ||
) | ||
mock_record_event = mocker.patch( | ||
"warehouse.accounts.models.HasEvents.record_event", | ||
autospec=True, | ||
return_value=True, | ||
) | ||
pyramid_request.method = "POST" | ||
pyramid_request.route_path = pretend.call_recorder(lambda a: "/the-redirect") | ||
user_service.get_user_by_username = pretend.call_recorder(lambda a: stub_user) | ||
user_service.get_user_by_username = pretend.call_recorder(lambda a: user) | ||
pyramid_request.find_service = pretend.call_recorder( | ||
lambda interface, **kw: { | ||
IUserService: user_service, | ||
}[interface] | ||
) | ||
form_obj = pretend.stub( | ||
username_or_email=pretend.stub(data=stub_user.username), | ||
username_or_email=pretend.stub(data=user.username), | ||
validate=pretend.call_recorder(lambda: True), | ||
) | ||
form_class = pretend.call_recorder(lambda d, user_service: form_obj) | ||
|
@@ -2132,12 +2133,11 @@ def test_password_reset_prohibited( | |
] | ||
assert result.headers["Location"] == "/the-redirect" | ||
|
||
assert stub_user.record_event.calls == [ | ||
pretend.call( | ||
tag=EventTag.Account.PasswordResetAttempt, | ||
request=pyramid_request, | ||
) | ||
] | ||
mock_record_event.assert_called_once_with( | ||
user, | ||
tag=EventTag.Account.PasswordResetAttempt, | ||
request=pyramid_request, | ||
) | ||
|
||
def test_password_reset_with_nonexistent_email( | ||
self, monkeypatch, pyramid_request, pyramid_config, user_service, token_service | ||
|
@@ -2169,6 +2169,52 @@ def test_redirect_authenticated_user(self): | |
assert isinstance(result, HTTPSeeOther) | ||
assert result.headers["Location"] == "/the-redirect" | ||
|
||
@pytest.mark.parametrize( | ||
"user_input", | ||
[ | ||
"email", | ||
"username", | ||
], | ||
) | ||
def test_unverified_email_sends_alt_notice(self, db_request, mocker, user_input): | ||
unverified_email = EmailFactory(verified=False) | ||
|
||
form_input = { | ||
"email": unverified_email.email, | ||
"username": unverified_email.user.username, | ||
}.get(user_input) | ||
|
||
mock_send_email = mocker.patch( | ||
"warehouse.accounts.views.send_password_reset_unverified_email", | ||
autospec=True, | ||
return_value=None, | ||
) | ||
# Prevent form's validation from checking deliverability | ||
mock_form_validation = mocker.patch( | ||
"warehouse.accounts.forms." | ||
"RequestPasswordResetForm.validate_username_or_email", | ||
autospec=True, | ||
return_value=True, | ||
) | ||
|
||
db_request.method = "POST" | ||
db_request.POST = MultiDict({"username_or_email": form_input}) | ||
|
||
result = views.request_password_reset(db_request) | ||
|
||
assert result == {"n_hours": 6} | ||
mock_form_validation.assert_called_once() | ||
mock_send_email.assert_called_once_with( | ||
db_request, (unverified_email.user, unverified_email) | ||
) | ||
assert db_request.log.warning.calls == [ | ||
pretend.call( | ||
"User requested password reset for unverified email", | ||
username=unverified_email.user.username, | ||
email_address=unverified_email.email, | ||
) | ||
] | ||
|
||
|
||
class TestResetPassword: | ||
@pytest.mark.parametrize("dates_utc", [True, False]) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -535,17 +535,14 @@ def retry(exc): | |
|
||
class TestSendPasswordResetEmail: | ||
@pytest.mark.parametrize( | ||
("verified", "email_addr"), | ||
"email_addr", | ||
[ | ||
(True, None), | ||
(False, None), | ||
(True, "[email protected]"), | ||
(False, "[email protected]"), | ||
None, | ||
"[email protected]", | ||
], | ||
) | ||
def test_send_password_reset_email( | ||
self, | ||
verified, | ||
email_addr, | ||
pyramid_request, | ||
pyramid_config, | ||
|
@@ -556,7 +553,7 @@ def test_send_password_reset_email( | |
stub_user = pretend.stub( | ||
id="id", | ||
email="[email protected]", | ||
primary_email=pretend.stub(email="[email protected]", verified=verified), | ||
primary_email=pretend.stub(email="[email protected]", verified=True), | ||
username="username_value", | ||
name="name_value", | ||
last_login="last_login", | ||
|
@@ -565,7 +562,7 @@ def test_send_password_reset_email( | |
if email_addr is None: | ||
stub_email = None | ||
else: | ||
stub_email = pretend.stub(email=email_addr, verified=verified) | ||
stub_email = pretend.stub(email=email_addr, verified=True) | ||
pyramid_request.method = "POST" | ||
token_service.dumps = pretend.call_recorder(lambda a: "TOKEN") | ||
|
||
|
@@ -654,12 +651,37 @@ def test_send_password_reset_email( | |
"warehouse.emails.scheduled", | ||
tags=[ | ||
"template_name:password-reset", | ||
"allow_unverified:True", | ||
"allow_unverified:False", | ||
"repeat_window:none", | ||
], | ||
) | ||
] | ||
|
||
def test_unverified_email_sends_alt_notice(self, pyramid_config, db_request): | ||
unverified_email = EmailFactory.create(verified=False) | ||
|
||
subject_renderer = pyramid_config.testing_add_renderer( | ||
"email/password-reset-unverified/subject.txt" | ||
) | ||
subject_renderer.string_response = "Email Subject" | ||
body_renderer = pyramid_config.testing_add_renderer( | ||
"email/password-reset-unverified/body.txt" | ||
) | ||
body_renderer.string_response = "Email Body" | ||
html_renderer = pyramid_config.testing_add_renderer( | ||
"email/password-reset-unverified/body.html" | ||
) | ||
html_renderer.string_response = "Email HTML Body" | ||
|
||
result = email.send_password_reset_unverified_email( | ||
db_request, (unverified_email.user, unverified_email) | ||
) | ||
|
||
assert result == {} | ||
subject_renderer.assert_() | ||
body_renderer.assert_() | ||
html_renderer.assert_() | ||
|
||
|
||
class TestEmailVerificationEmail: | ||
def test_email_verification_email( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the material change to the unverified tests from the previous version - now this test takes either of these as an input.