Skip to content

Commit ea5d63e

Browse files
committed
Compare emailed MFA codes in constant time and clear the retry flag on success.
Use hmac.compare_digest() on the UTF-8 encoded values rather than ==, and also drop the retry session flag alongside the attempt counter once the code has been accepted.
1 parent 77a472f commit ea5d63e

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎web/pgadmin/authenticate/mfa/email.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
##############################################################################
99
"""Multi-factor Authentication implementation by sending OTP through email"""
1010

11+
import hmac
12+
1113
from flask import url_for, session, Response, render_template, current_app, \
1214
flash
1315
from flask_babel import gettext as _
@@ -213,9 +215,14 @@ def label(self):
213215
def validate(self, **kwargs):
214216
code = kwargs.get('code', None)
215217
email_otp = session.get("mfa_email_code", None)
216-
if code is not None and email_otp is not None and code == email_otp:
218+
# Compare in constant time; compare_digest() rejects non-ASCII str,
219+
# so compare the UTF-8 encoded bytes instead.
220+
if isinstance(code, str) and isinstance(email_otp, str) and \
221+
hmac.compare_digest(code.encode('utf-8'),
222+
email_otp.encode('utf-8')):
217223
session.pop("mfa_email_code")
218224
session.pop(_EMAIL_CODE_ATTEMPTS, None)
225+
session.pop(_EMAIL_CODE_RETRY, None)
219226
return
220227

221228
if email_otp is None:

0 commit comments

Comments
 (0)