Skip to content

Commit 6ced7a5

Browse files
authored
Fix possible timing leak in validate_password (#1535)
1 parent 8a3b55a commit 6ced7a5

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

cmscommon/crypto.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""Utilities dealing with encryption and randomness."""
2424

2525
import binascii
26+
import hmac
2627
import random
2728
from string import ascii_lowercase
2829

@@ -190,15 +191,15 @@ def validate_password(authentication: str, password: str) -> bool:
190191
191192
"""
192193
method, payload = parse_authentication(authentication)
194+
password_bytes = password.encode("utf-8")
195+
payload_bytes = payload.encode("utf-8")
193196
if method == "bcrypt":
194-
password_bytes = password.encode("utf-8")
195-
payload_bytes = payload.encode("utf-8")
196197
try:
197-
return bcrypt.hashpw(password_bytes, payload_bytes) == payload_bytes
198+
return bcrypt.checkpw(password_bytes, payload_bytes)
198199
except ValueError:
199200
return False
200201
elif method == "plaintext":
201-
return payload == password
202+
return hmac.compare_digest(password_bytes, payload_bytes)
202203
else:
203204
raise ValueError("Authentication method not known.")
204205

0 commit comments

Comments
 (0)