Skip to content

Commit

Permalink
Merge pull request matrix-org#572 from matrix-org/daniel/exclusivity
Browse files Browse the repository at this point in the history
Enforce user_id exclusivity for AS registrations
  • Loading branch information
illicitonion committed Feb 11, 2016
2 parents ce14c7a + dc6da63 commit ee4f332
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def check_username(self, localpart, guest_access_token=None):
user = UserID(localpart, self.hs.hostname)
user_id = user.to_string()

yield self.check_user_id_is_valid(user_id)
yield self.check_user_id_not_appservice_exclusive(user_id)

users = yield self.store.get_users_by_id_case_insensitive(user_id)
if users:
Expand Down Expand Up @@ -145,7 +145,7 @@ def register(
localpart = yield self._generate_user_id(attempts > 0)
user = UserID(localpart, self.hs.hostname)
user_id = user.to_string()
yield self.check_user_id_is_valid(user_id)
yield self.check_user_id_not_appservice_exclusive(user_id)
if generate_token:
token = self.auth_handler().generate_access_token(user_id)
try:
Expand Down Expand Up @@ -180,6 +180,11 @@ def appservice_register(self, user_localpart, as_token):
400, "Invalid user localpart for this application service.",
errcode=Codes.EXCLUSIVE
)

yield self.check_user_id_not_appservice_exclusive(
user_id, allowed_appservice=service
)

token = self.auth_handler().generate_access_token(user_id)
yield self.store.register(
user_id=user_id,
Expand Down Expand Up @@ -226,7 +231,7 @@ def register_saml2(self, localpart):
user = UserID(localpart, self.hs.hostname)
user_id = user.to_string()

yield self.check_user_id_is_valid(user_id)
yield self.check_user_id_not_appservice_exclusive(user_id)
token = self.auth_handler().generate_access_token(user_id)
try:
yield self.store.register(
Expand Down Expand Up @@ -278,12 +283,14 @@ def bind_emails(self, user_id, threepidCreds):
yield identity_handler.bind_threepid(c, user_id)

@defer.inlineCallbacks
def check_user_id_is_valid(self, user_id):
def check_user_id_not_appservice_exclusive(self, user_id, allowed_appservice=None):
# valid user IDs must not clash with any user ID namespaces claimed by
# application services.
services = yield self.store.get_app_services()
interested_services = [
s for s in services if s.is_interested_in_user(user_id)
s for s in services
if s.is_interested_in_user(user_id)
and s != allowed_appservice
]
for service in interested_services:
if service.is_exclusive_user(user_id):
Expand Down

0 comments on commit ee4f332

Please sign in to comment.