diff options
| author | Steve Piercy <web@stevepiercy.com> | 2016-02-27 00:59:17 -0800 |
|---|---|---|
| committer | Steve Piercy <web@stevepiercy.com> | 2016-02-27 00:59:17 -0800 |
| commit | 082d3b2cb9127f8acfd4d081e69c427a37bae91d (patch) | |
| tree | 72d1bcdc64a352ac0acc69a575cac4d4423074b5 /docs | |
| parent | a6db36c984bd69f8a6aba80ad6db435cd4b1b93c (diff) | |
| download | pyramid-082d3b2cb9127f8acfd4d081e69c427a37bae91d.tar.gz pyramid-082d3b2cb9127f8acfd4d081e69c427a37bae91d.tar.bz2 pyramid-082d3b2cb9127f8acfd4d081e69c427a37bae91d.zip | |
wiki2 authentication bug fix and improvement against timing attack
- Bytes type does not have encode method. The expected_hash retrieved from the database is a bytes object.
- Use hmac.compare_digest instead of == to avoid timing attacks as a recommended security best practice. See https://www.python.org/dev/peps/pep-0466/ https://bugs.python.org/issue21306 and https://codahale.com/a-lesson-in-timing-attacks/ for details.
Note, however, this was not backported to py2.6. For a tutorial, I am OK with stating this will not work on Python 2.6 with a clear warning note at the start of the tutorial and on the authentication step.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/tutorials/wiki2/src/authentication/tutorial/models/user.py | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/docs/tutorials/wiki2/src/authentication/tutorial/models/user.py b/docs/tutorials/wiki2/src/authentication/tutorial/models/user.py index 6fb32a1b2..6499491b2 100644 --- a/docs/tutorials/wiki2/src/authentication/tutorial/models/user.py +++ b/docs/tutorials/wiki2/src/authentication/tutorial/models/user.py @@ -1,4 +1,5 @@ import bcrypt +import hmac from sqlalchemy import ( Column, Integer, @@ -23,7 +24,7 @@ class User(Base): def check_password(self, pw): if self.password_hash is not None: - expected_hash = self.password_hash.encode('utf8') + expected_hash = self.password_hash actual_hash = bcrypt.hashpw(pw.encode('utf8'), expected_hash) - return expected_hash == actual_hash + return hmac.compare_digest(expected_hash, actual_hash) return False |
