@@ -57,21 +57,21 @@ def recover_password(email: str, session: SessionDep) -> Message:
5757 """
5858 user = crud .get_user_by_email (session = session , email = email )
5959
60- if not user :
61- raise HTTPException (
62- status_code = 404 ,
63- detail = "The user with this email does not exist in the system." ,
60+ # Always return the same response to prevent email enumeration attacks
61+ # Only send email if user actually exists
62+ if user :
63+ password_reset_token = generate_password_reset_token (email = email )
64+ email_data = generate_reset_password_email (
65+ email_to = user .email , email = email , token = password_reset_token
6466 )
65- password_reset_token = generate_password_reset_token (email = email )
66- email_data = generate_reset_password_email (
67- email_to = user .email , email = email , token = password_reset_token
68- )
69- send_email (
70- email_to = user .email ,
71- subject = email_data .subject ,
72- html_content = email_data .html_content ,
67+ send_email (
68+ email_to = user .email ,
69+ subject = email_data .subject ,
70+ html_content = email_data .html_content ,
71+ )
72+ return Message (
73+ message = "If that email is registered, we sent a password recovery link"
7374 )
74- return Message (message = "Password recovery email sent" )
7575
7676
7777@router .post ("/reset-password/" )
@@ -84,10 +84,8 @@ def reset_password(session: SessionDep, body: NewPassword) -> Message:
8484 raise HTTPException (status_code = 400 , detail = "Invalid token" )
8585 user = crud .get_user_by_email (session = session , email = email )
8686 if not user :
87- raise HTTPException (
88- status_code = 404 ,
89- detail = "The user with this email does not exist in the system." ,
90- )
87+ # Don't reveal that the user doesn't exist - use same error as invalid token
88+ raise HTTPException (status_code = 400 , detail = "Invalid token" )
9189 elif not user .is_active :
9290 raise HTTPException (status_code = 400 , detail = "Inactive user" )
9391 user_in_update = UserUpdate (password = body .new_password )
0 commit comments