From 6647dd8f08f74cf8c573aa1d736c3505d32b6f38 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Sun, 12 Jan 2020 02:52:27 -0300 Subject: [PATCH] fix decrypting invalid bytes with valid padding --- lbry/crypto/crypt.py | 2 ++ tests/unit/wallet/test_hash.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/lbry/crypto/crypt.py b/lbry/crypto/crypt.py index 382c3b4d4..1b4370bbf 100644 --- a/lbry/crypto/crypt.py +++ b/lbry/crypto/crypt.py @@ -33,6 +33,8 @@ def aes_decrypt(secret: str, value: str) -> typing.Tuple[str, bytes]: unpadder = PKCS7(AES.block_size).unpadder() result = unpadder.update(decryptor.update(data)) + unpadder.finalize() return result.decode(), init_vector + except UnicodeDecodeError: + raise InvalidPasswordError() except ValueError as e: if e.args[0] == 'Invalid padding bytes.': raise InvalidPasswordError() diff --git a/tests/unit/wallet/test_hash.py b/tests/unit/wallet/test_hash.py index 0a09ea1e6..658021f59 100644 --- a/tests/unit/wallet/test_hash.py +++ b/tests/unit/wallet/test_hash.py @@ -39,6 +39,10 @@ class TestAESEncryptDecrypt(TestCase): with self.assertRaises(InvalidPasswordError): aes_decrypt('notbubblegum', aes_encrypt('bubblegum', self.message)) + def test_edge_case_invalid_password_valid_padding_invalid_unicode(self): + with self.assertRaises(InvalidPasswordError): + aes_decrypt('notbubblegum', 'gy3/mNq3FWB/xAXirOQnlAqQLuvhLGXZaeGBUIg1w6yY4PDLDT7BU83XOfBsJoluWU5zEU4+upOFH35HDqyV8EMQhcKSufN9WkT1izEbFtweBUTK8nTSkV7NBppE1Jaz') + def test_better_encrypt_decrypt(self): self.assertEqual( b'valuable value',