Skip to content

Swap user_can_authenticate and check_password in if statement#16969

Closed
ebram96 wants to merge 1 commit intodjango:mainfrom
ebram96:patch-4
Closed

Swap user_can_authenticate and check_password in if statement#16969
ebram96 wants to merge 1 commit intodjango:mainfrom
ebram96:patch-4

Conversation

@ebram96
Copy link
Copy Markdown
Contributor

@ebram96 ebram96 commented Jun 12, 2023

Make user_can_authenticate the left operand and check_password the right operand in if statement because user_can_authenticate does no complex logic but check_password does (like hashing the raw password). This can save computation in case of user_can_authenticate just returns False.

Make user_can_authenticate the left operand and check_password the right operand in if statement because user_can_authenticate does no complex logic but check_password does. This can save computation.
UserModel().set_password(password)
else:
if user.check_password(password) and self.user_can_authenticate(user):
if self.user_can_authenticate(user) and user.check_password(password):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the line above I think the original order was intended to blur the lines in terms of timing difference and prevent attacks.

Basically no matter what input is provided hashing takes place, which is a slow operation, to make it hard to differentiate between

  1. A user that doesn't exist
  2. A user that can't authenticate
  3. A user with the wrong password
  4. A user with the right password

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.. thank you for explaining this!

krisward1 pushed a commit to krisward1/django that referenced this pull request Jun 13, 2023
@felixxm felixxm closed this Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants