Skip to content

Commit

Permalink
feat: ensure new passwords are distinct from the previous scidsg#468
Browse files Browse the repository at this point in the history
Adversarial probing of server responses to reveal user password
information is prevented. This is done by running basic form
validators first, then user password validation, and only then
is the repeat password check done.
  • Loading branch information
rmlibre committed Oct 3, 2024
1 parent 6cbe2eb commit 9641811
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions hushline/settings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import base64
import io
from hmac import compare_digest as bytes_are_equal

import aiohttp
import pyotp
Expand Down Expand Up @@ -354,22 +355,25 @@ def change_password() -> str | Response:

change_password_form = ChangePasswordForm(request.form)
if not change_password_form.validate_on_submit():
flash("New password is invalid.")
flash("⛔️ Invalid form data. Please try again.", "error")
return redirect(url_for("settings.index"))

if not change_password_form.old_password.data or not user.check_password(
change_password_form.old_password.data
if not user.check_password(change_password_form.old_password.data):
flash("⛔️ Incorrect old password.", "error")
return redirect(url_for("settings.index"))

# SECURITY: only check equality after successful old password check
if bytes_are_equal(
change_password_form.old_password.data.encode(),
change_password_form.new_password.data.encode(),
):
flash("Incorrect old password.", "error")
flash("⛔️ Cannot choose a repeat password.", "error")
return redirect(url_for("settings.index"))

user.password_hash = change_password_form.new_password.data
db.session.commit()
session.clear()
flash(
"👍 Password successfully changed. Please log in again.",
"success",
)
flash("👍 Password successfully changed. Please log in again.", "success")
return redirect(url_for("login"))

@bp.route("/enable-2fa", methods=["GET", "POST"])
Expand Down

0 comments on commit 9641811

Please sign in to comment.