Skip to content

Commit

Permalink
Merge pull request #277 from xrmx/wrongkey
Browse files Browse the repository at this point in the history
Add more hints when decrypting AES with an invalid key
  • Loading branch information
kvesteri authored Sep 1, 2017
2 parents 017d0c6 + dd694fe commit fade68c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion sqlalchemy_utils/types/encrypted.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ def decrypt(self, value):
decrypted = decryptor.update(decrypted) + decryptor.finalize()
decrypted = decrypted.rstrip(self.PADDING)
if not isinstance(decrypted, six.string_types):
decrypted = decrypted.decode('utf-8')
try:
decrypted = decrypted.decode('utf-8')
except UnicodeDecodeError:
raise ValueError('Invalid decryption key')
return decrypted


Expand Down
10 changes: 10 additions & 0 deletions tests/types/test_encrypted.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,16 @@ def test_lookup_by_encrypted_string(self, session, User, user, user_name):

assert test.username == user.username

def test_decrypt_raises_value_error_with_invalid_key(self, session, Team):
self._team_key = 'one'
team = Team(key=self._team_key, name=u'One')
session.add(team)
session.commit()

self._team_key = 'notone'
with pytest.raises(ValueError):
assert team.name == u'One'


class TestFernetEncryptedTypeTestCase(EncryptedTypeTestCase):

Expand Down

0 comments on commit fade68c

Please sign in to comment.