Skip to content

Commit d88f787

Browse files
authored
Fix verify_password() with bcrypt 5.0 (#1179)
verify_password() fails with "password cannot be longer than 72 bytes" when bcrypt 5.0 is installed. Fix this similar to how commit 2994677 ("Move to libpass and support bcrypt 5.0 (#1143)") did it in other places. Add a test. Extracting salt for comparing the result with bcrypt.hashpw() is too brittle; just skip this step.
1 parent 3c50751 commit d88f787

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

flask_security/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ def verify_password(password: str | bytes, password_hash: str | bytes) -> bool:
395395
"""
396396
if use_double_hash(password_hash):
397397
password = get_hmac(password)
398+
if _pwd_context.identify(password_hash) == "bcrypt":
399+
password = password[:72]
398400

399401
return _pwd_context.verify(password, password_hash)
400402

tests/test_hashing.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
)
2525

2626

27-
def test_verify_password_double_hash(app, sqlalchemy_datastore):
27+
def test_verify_password_double_hash_argon2(app, sqlalchemy_datastore):
2828
init_app_with_options(
2929
app,
3030
sqlalchemy_datastore,
@@ -44,6 +44,22 @@ def test_verify_password_double_hash(app, sqlalchemy_datastore):
4444
assert verify_password("pass", argon2.hash(get_hmac("pass")))
4545

4646

47+
def test_verify_password_double_hash_bcrypt(app, sqlalchemy_datastore):
48+
init_app_with_options(
49+
app,
50+
sqlalchemy_datastore,
51+
**{
52+
"SECURITY_PASSWORD_HASH": "bcrypt",
53+
"SECURITY_PASSWORD_SALT": "salty",
54+
"SECURITY_PASSWORD_SINGLE_HASH": False,
55+
},
56+
)
57+
with app.app_context():
58+
hashed_pwd = hash_password("pass")
59+
assert verify_password("pass", hashed_pwd)
60+
assert hashed_pwd.startswith("$2")
61+
62+
4763
def test_verify_password_single_hash(app, sqlalchemy_datastore):
4864
init_app_with_options(
4965
app,

0 commit comments

Comments
 (0)