This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add validation of format for 3pid and add validation of 3pid in admin api #7022
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea2a8de
Add validation of format for 3pid
dklimpel a801e8d
fixes
dklimpel 4648933
fix2
dklimpel 59e4428
lint
dklimpel 7c9da15
Add validation of 3pid in admin api
dklimpel 8ff3522
sync dev and trigger checks
dklimpel 131255d
sync dev and trigger checks
dklimpel dbaf0a3
Not final commit, update of PR after review.
dklimpel 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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Add validation of 3pid in admin api ``PUT /_synapse/admin/v2/users/<user_id>``. |
This file contains 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 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 |
---|---|---|
|
@@ -36,6 +36,7 @@ | |
historical_admin_path_patterns, | ||
) | ||
from synapse.types import UserID | ||
from synapse.util.threepids import check_3pid_allowed, check_3pid_valid_format | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -195,8 +196,35 @@ async def on_PUT(self, request, user_id): | |
# add new threepids to user | ||
current_time = self.hs.get_clock().time_msec() | ||
for threepid in body["threepids"]: | ||
# For emails, transform the address to lowercase. | ||
# We store all email addreses as lowercase in the DB. | ||
# (See add_threepid in synapse/handlers/auth.py) | ||
address = threepid["address"].lower() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noting that this seems related to #7021. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change needs to wait on the result of that conversation then. |
||
if not check_3pid_valid_format(threepid["medium"], address): | ||
raise SynapseError( | ||
400, | ||
"Third party identifier has an invalid format", | ||
Codes.INVALID_THREEPID, | ||
) | ||
|
||
if not check_3pid_allowed(self.hs, threepid["medium"], address): | ||
raise SynapseError( | ||
403, | ||
"Your email domain or account phone number is not authorized on this server", | ||
Codes.THREEPID_DENIED, | ||
) | ||
|
||
existing_user_id = await self.store.get_user_id_by_threepid( | ||
threepid["medium"], address | ||
) | ||
|
||
if existing_user_id is not None: | ||
raise SynapseError( | ||
400, "Threepid is already in use", Codes.THREEPID_IN_USE | ||
) | ||
|
||
await self.auth_handler.add_threepid( | ||
user_id, threepid["medium"], threepid["address"], current_time | ||
user_id, threepid["medium"], address, current_time | ||
) | ||
|
||
if "avatar_url" in body: | ||
|
@@ -271,8 +299,35 @@ async def on_PUT(self, request, user_id): | |
|
||
current_time = self.hs.get_clock().time_msec() | ||
for threepid in body["threepids"]: | ||
# For emails, transform the address to lowercase. | ||
# We store all email addreses as lowercase in the DB. | ||
# (See add_threepid in synapse/handlers/auth.py) | ||
address = threepid["address"].lower() | ||
if not check_3pid_valid_format(threepid["medium"], address): | ||
raise SynapseError( | ||
400, | ||
"Third party identifier has an invalid format", | ||
Codes.INVALID_THREEPID, | ||
) | ||
|
||
if not check_3pid_allowed(self.hs, threepid["medium"], address): | ||
raise SynapseError( | ||
403, | ||
"Your email domain or account phone number is not authorized on this server", | ||
Codes.THREEPID_DENIED, | ||
) | ||
|
||
existing_user_id = await self.store.get_user_id_by_threepid( | ||
threepid["medium"], address | ||
) | ||
|
||
if existing_user_id is not None: | ||
raise SynapseError( | ||
400, "Threepid is already in use", Codes.THREEPID_IN_USE | ||
) | ||
|
||
await self.auth_handler.add_threepid( | ||
user_id, threepid["medium"], threepid["address"], current_time | ||
user_id, threepid["medium"], address, current_time | ||
) | ||
|
||
if "avatar_url" in body: | ||
|
This file contains 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 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 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 |
---|---|---|
|
@@ -48,3 +48,24 @@ def check_3pid_allowed(hs, medium, address): | |
return True | ||
|
||
return False | ||
|
||
|
||
def check_3pid_valid_format(medium, address) -> bool: | ||
"""Checks whether 3pid has a valid format | ||
|
||
Args: | ||
medium (str): 3pid medium - e.g. email, msisdn | ||
address (str): 3pid address to check (e.g. "wotan@matrix.org") | ||
dklimpel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Returns: | ||
bool: whether the email address has a valid format | ||
""" | ||
|
||
# medium must be "email" or "msisdn" | ||
if medium == "email": | ||
regex = r"^[^@]+@[^@]+\.[^@]+$" | ||
return bool(re.fullmatch(regex, address)) | ||
# no validation/pattern for "msisdn" at the moment | ||
elif medium == "msisdn": | ||
return True | ||
else: | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment above this saying that any other medium is not understood and is thus invalid? Thanks! |
This file contains 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
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.
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.
It is unclear to me if this needs a spec change or not. @matrix-org/synapse-core any opinion?
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.
I did not find an spec for these errors. Only a documenttion https://github.com/matrix-org/matrix-doc/blob/master/specification/client_server_api.rst
I can also remove this error.
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.
You can see the list of error codes at https://matrix.org/docs/spec/client_server/r0.6.0#api-standards, this would need to get added as a separate one following the process at https://matrix.org/docs/spec/proposals