Skip to content

Commit ae18123

Browse files
mcalingheeMatMaulodelcroi
authored
Send an email if the address is already bound to an user account (#16819)
Co-authored-by: Mathieu Velten <[email protected]> Co-authored-by: Olivier D <[email protected]>
1 parent 074ef4d commit ae18123

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

Diff for: changelog.d/16819.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Send an email if the address is already bound to an user account.

Diff for: synapse/config/emailconfig.py

+12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"invite_from_person_to_space": "[%(app)s] %(person)s has invited you to join the %(space)s space on %(app)s...",
5353
"password_reset": "[%(server_name)s] Password reset",
5454
"email_validation": "[%(server_name)s] Validate your email",
55+
"email_already_in_use": "[%(server_name)s] Email already in use",
5556
}
5657

5758
LEGACY_TEMPLATE_DIR_WARNING = """
@@ -76,6 +77,7 @@ class EmailSubjectConfig:
7677
invite_from_person_to_space: str
7778
password_reset: str
7879
email_validation: str
80+
email_already_in_use: str
7981

8082

8183
class EmailConfig(Config):
@@ -180,6 +182,12 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
180182
registration_template_text = email_config.get(
181183
"registration_template_text", "registration.txt"
182184
)
185+
already_in_use_template_html = email_config.get(
186+
"already_in_use_template_html", "already_in_use.html"
187+
)
188+
already_in_use_template_text = email_config.get(
189+
"already_in_use_template_html", "already_in_use.txt"
190+
)
183191
add_threepid_template_html = email_config.get(
184192
"add_threepid_template_html", "add_threepid.html"
185193
)
@@ -215,6 +223,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
215223
self.email_password_reset_template_text,
216224
self.email_registration_template_html,
217225
self.email_registration_template_text,
226+
self.email_already_in_use_template_html,
227+
self.email_already_in_use_template_text,
218228
self.email_add_threepid_template_html,
219229
self.email_add_threepid_template_text,
220230
self.email_password_reset_template_confirmation_html,
@@ -230,6 +240,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
230240
password_reset_template_text,
231241
registration_template_html,
232242
registration_template_text,
243+
already_in_use_template_html,
244+
already_in_use_template_text,
233245
add_threepid_template_html,
234246
add_threepid_template_text,
235247
"password_reset_confirmation.html",

Diff for: synapse/push/mailer.py

+16
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ async def send_registration_mail(
205205
template_vars,
206206
)
207207

208+
emails_sent_counter.labels("already_in_use")
209+
210+
async def send_already_in_use_mail(self, email_address: str) -> None:
211+
"""Send an email if the address is already bound to an user account
212+
213+
Args:
214+
email_address: Email address we're sending to the "already in use" mail
215+
"""
216+
217+
await self.send_email(
218+
email_address,
219+
self.email_subjects.email_already_in_use
220+
% {"server_name": self.hs.config.server.server_name, "app": self.app_name},
221+
{},
222+
)
223+
208224
emails_sent_counter.labels("add_threepid")
209225

210226
async def send_add_threepid_mail(

Diff for: synapse/res/templates/already_in_use.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "_base.html" %}
2+
{% block title %}Email already in use{% endblock %}
3+
4+
{% block body %}
5+
<p>You have asked us to register this email with a new Matrix account, but this email is already registered with an existing account.</p>
6+
7+
<p>Please reset your password if needed.</p>
8+
9+
<p>If this was not you, you can safely disregard this email.</p>
10+
11+
<p>Thank you.</p>
12+
{% endblock %}

Diff for: synapse/res/templates/already_in_use.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Hello there,
2+
3+
You have asked us to register this email with a new Matrix account,
4+
but this email is already registered with an existing account.
5+
6+
Please reset your password if needed.
7+
8+
If this was not you, you can safely disregard this email.
9+
10+
Thank you.

Diff for: synapse/rest/client/register.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,18 @@ def __init__(self, hs: "HomeServer"):
8686
self.config = hs.config
8787

8888
if self.hs.config.email.can_verify_email:
89-
self.mailer = Mailer(
89+
self.registration_mailer = Mailer(
9090
hs=self.hs,
9191
app_name=self.config.email.email_app_name,
9292
template_html=self.config.email.email_registration_template_html,
9393
template_text=self.config.email.email_registration_template_text,
9494
)
95+
self.already_in_use_mailer = Mailer(
96+
hs=self.hs,
97+
app_name=self.config.email.email_app_name,
98+
template_html=self.config.email.email_already_in_use_template_html,
99+
template_text=self.config.email.email_already_in_use_template_text,
100+
)
95101

96102
async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
97103
if not self.hs.config.email.can_verify_email:
@@ -139,8 +145,10 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
139145
if self.hs.config.server.request_token_inhibit_3pid_errors:
140146
# Make the client think the operation succeeded. See the rationale in the
141147
# comments for request_token_inhibit_3pid_errors.
148+
# Still send an email to warn the user that an account already exists.
142149
# Also wait for some random amount of time between 100ms and 1s to make it
143150
# look like we did something.
151+
await self.already_in_use_mailer.send_already_in_use_mail(email)
144152
await self.hs.get_clock().sleep(random.randint(1, 10) / 10)
145153
return 200, {"sid": random_string(16)}
146154

@@ -151,7 +159,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
151159
email,
152160
client_secret,
153161
send_attempt,
154-
self.mailer.send_registration_mail,
162+
self.registration_mailer.send_registration_mail,
155163
next_link,
156164
)
157165

Diff for: tests/rest/client/test_register.py

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import datetime
2323
import os
2424
from typing import Any, Dict, List, Tuple
25+
from unittest.mock import AsyncMock
2526

2627
import pkg_resources
2728

@@ -42,6 +43,7 @@
4243
from synapse.util import Clock
4344

4445
from tests import unittest
46+
from tests.server import ThreadedMemoryReactorClock
4547
from tests.unittest import override_config
4648

4749

@@ -58,6 +60,13 @@ def default_config(self) -> Dict[str, Any]:
5860
config["allow_guest_access"] = True
5961
return config
6062

63+
def make_homeserver(
64+
self, reactor: ThreadedMemoryReactorClock, clock: Clock
65+
) -> HomeServer:
66+
hs = super().make_homeserver(reactor, clock)
67+
hs.get_send_email_handler()._sendmail = AsyncMock()
68+
return hs
69+
6170
def test_POST_appservice_registration_valid(self) -> None:
6271
user_id = "@as_user_kermit:test"
6372
as_token = "i_am_an_app_service"

0 commit comments

Comments
 (0)