Skip to content

Encryption backend will fail for environments migrated from python2 #441

@Ashex

Description

@Ashex

When migrating from python 2.7 runtime to python 3 (3.8 for me) will will fail to load anything from redis with the following exception (once revealed):

Traceback (most recent call last):
File "/opt/willbot/will/backends/encryption/aes.py", line 57, in decrypt_from_b64
return pickle.loads(binascii.a2b_base64(decrypted_data.decode('utf8')))
File "/opt/pyenv/lib/python3.8/site-packages/dill/_dill.py", line 283, in loads
return load(file, ignore, **kwds)
File "/opt/pyenv/lib/python3.8/site-packages/dill/_dill.py", line 278, in load
return Unpickler(file, ignore=ignore, **kwds).load()
File "/opt/pyenv/lib/python3.8/site-packages/dill/_dill.py", line 481, in load
obj = StockUnpickler.load(self)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)

The fix is to explicitly tell pickle what encoding to use:

    @staticmethod
    def decrypt_from_b64(enc):
        'decrypt b64-encoded data'
        try:
            if b'/' in enc and enc.index(b'/') == BS:
                iv = enc[:BS]
                encrypted_data = enc[BS+1:]
                cipher = AES.new(key, AES.MODE_CBC, iv)
                decrypted_data = unpad(cipher.decrypt(binascii.a2b_base64(encrypted_data)))
            return pickle.loads(binascii.a2b_base64(decrypted_data), encoding='bytes')
        except (KeyboardInterrupt, SystemExit):
            pass
        except Exception:
            logging.debug("Error decrypting.  Attempting unencrypted load to ease migration.")
            return pickle.loads(binascii.a2b_base64(enc))

Reference: https://stackoverflow.com/a/28218598

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions