Skip to content

Commit

Permalink
security: uniformize failed login message
Browse files Browse the repository at this point in the history
* Provides same message to the user on login attempt if the password or
username is invalid, to prevent security exploits.

Co-Authored-by: Pascal Repond <[email protected]>
  • Loading branch information
PascalRepond committed Dec 20, 2022
1 parent 7125861 commit 3d29866
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions rero_ils/accounts_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 43,7 @@ def user_exists(email):
"""Validate that a user exists."""
user = User.get_by_username_or_email(email)
if not user:
raise ValidationError(_('USER_DOES_NOT_EXIST'))
raise ValidationError(_('INVALID_USER_OR_PASSWORD'))


class LoginView(CoreLoginView):
Expand All @@ -66,7 66,7 @@ def post(self, **kwargs):
"""Verify and login a user."""
user = self.get_user(**kwargs)
if not user:
_abort(_('USER_DOES_NOT_EXIST'))
_abort(_('INVALID_USER_OR_PASSWORD'))
self.verify_login(user, **kwargs)
self.login_user(user)
return self.success_response(user)
Expand Down
4 changes: 4 additions & 0 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 2882,10 @@ def _(x):

# Login Configuration
# ===================
#: Supercharge flask_security invalid password or user message.
SECURITY_MSG_INVALID_PASSWORD = (_('INVALID_USER_OR_PASSWORD'), 'error')
SECURITY_MSG_USER_DOES_NOT_EXIST = (_('INVALID_USER_OR_PASSWORD'), 'error')

#: Allow password change by users.
SECURITY_CHANGEABLE = True

Expand Down
4 changes: 2 additions & 2 deletions tests/api/test_user_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 40,7 @@ def test_login(client, patron_sion):
assert res.status_code == 400
data = get_json(res)
assert data['errors'][0] == dict(
field='email', message=gettext('USER_DOES_NOT_EXIST'))
field='email', message=gettext('INVALID_USER_OR_PASSWORD'))

# wrong password
res, _ = postdata(
Expand All @@ -54,7 54,7 @@ def test_login(client, patron_sion):
assert res.status_code == 400
data = get_json(res)
assert data['errors'][0] == dict(
field='password', message='Invalid password')
field='password', message=gettext('INVALID_USER_OR_PASSWORD'))

# login by email
res, _ = postdata(
Expand Down

0 comments on commit 3d29866

Please sign in to comment.