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
Do not allow deactivated users to login with JWT #15624
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 @@ | ||
Fix a long-standing bug where deactivated users were still able to login using the custom `org.matrix.login.jwt` login type (if enabled). |
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,118 @@ | ||
# Copyright 2023 Matrix.org Foundation C.I.C. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import TYPE_CHECKING | ||
|
||
from authlib.jose import JsonWebToken, JWTClaims | ||
from authlib.jose.errors import BadSignatureError, InvalidClaimError, JoseError | ||
|
||
from synapse.api.errors import Codes, LoginError, StoreError, UserDeactivatedError | ||
from synapse.types import JsonDict, UserID | ||
|
||
if TYPE_CHECKING: | ||
from synapse.server import HomeServer | ||
|
||
|
||
class JwtHandler: | ||
def __init__(self, hs: "HomeServer"): | ||
self.hs = hs | ||
self._main_store = hs.get_datastores().main | ||
|
||
self.jwt_secret = hs.config.jwt.jwt_secret | ||
self.jwt_subject_claim = hs.config.jwt.jwt_subject_claim | ||
self.jwt_algorithm = hs.config.jwt.jwt_algorithm | ||
self.jwt_issuer = hs.config.jwt.jwt_issuer | ||
self.jwt_audiences = hs.config.jwt.jwt_audiences | ||
|
||
async def validate_login(self, login_submission: JsonDict) -> str: | ||
""" | ||
Authenticates the user for the /login API | ||
|
||
Args: | ||
login_submission: the whole of the login submission | ||
(including 'type' and other relevant fields) | ||
|
||
Returns: | ||
The user ID that is logging in. | ||
|
||
Raises: | ||
LoginError if there was an authentication problem. | ||
""" | ||
token = login_submission.get("token", None) | ||
if token is None: | ||
raise LoginError( | ||
403, "Token field for JWT is missing", errcode=Codes.FORBIDDEN | ||
) | ||
|
||
jwt = JsonWebToken([self.jwt_algorithm]) | ||
claim_options = {} | ||
if self.jwt_issuer is not None: | ||
claim_options["iss"] = {"value": self.jwt_issuer, "essential": True} | ||
if self.jwt_audiences is not None: | ||
claim_options["aud"] = {"values": self.jwt_audiences, "essential": True} | ||
|
||
try: | ||
claims = jwt.decode( | ||
token, | ||
key=self.jwt_secret, | ||
claims_cls=JWTClaims, | ||
claims_options=claim_options, | ||
) | ||
except BadSignatureError: | ||
# We handle this case separately to provide a better error message | ||
raise LoginError( | ||
403, | ||
"JWT validation failed: Signature verification failed", | ||
errcode=Codes.FORBIDDEN, | ||
) | ||
except JoseError as e: | ||
# A JWT error occurred, return some info back to the client. | ||
raise LoginError( | ||
403, | ||
"JWT validation failed: %s" % (str(e),), | ||
errcode=Codes.FORBIDDEN, | ||
) | ||
|
||
try: | ||
claims.validate(leeway=120) # allows 2 min of clock skew | ||
|
||
# Enforce the old behavior which is rolled out in productive | ||
# servers: if the JWT contains an 'aud' claim but none is | ||
# configured, the login attempt will fail | ||
if claims.get("aud") is not None: | ||
if self.jwt_audiences is None or len(self.jwt_audiences) == 0: | ||
raise InvalidClaimError("aud") | ||
except JoseError as e: | ||
raise LoginError( | ||
403, | ||
"JWT validation failed: %s" % (str(e),), | ||
errcode=Codes.FORBIDDEN, | ||
) | ||
|
||
user = claims.get(self.jwt_subject_claim, None) | ||
if user is None: | ||
raise LoginError(403, "Invalid JWT", errcode=Codes.FORBIDDEN) | ||
|
||
user_id = UserID(user, self.hs.hostname).to_string() | ||
|
||
# If the account has been deactivated, do not proceed with the login | ||
# flow. | ||
try: | ||
deactivated = await self._main_store.get_user_deactivated_status(user_id) | ||
except StoreError: | ||
# JWT lazily creates users, so they may not exist in the database yet. | ||
deactivated = False | ||
if deactivated: | ||
raise UserDeactivatedError("This account has been deactivated") | ||
|
||
return 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
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.
#12276 put this in the
_complete_login
flow of the rest servlet, which kind of seems better, but I'm a bit nervous that there's some flow there I don't understand.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 think we don't do it there because of password providers, but I think this is confusing -- if you deactivate the user in Synapse it shouldn't matter if their password is still fine in the upstream password provider?
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.
IMO all login methods should be call after specific validation also a global validation like
validate_login
. There, global checks can be performed, such as whether a user is deactivated.If a user is deactivated in synapse, It should not be able to log in. No matter if the user is active in the password provider or not. Otherwise I as the operator of Synapse have no more influence on the users and abuse or similar.
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.
Just to be clear -- this is exactly what I said. If the user is deactivated in Synapse they'll be blocked from logging in regardless of what the password provider says.