Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "cryptography_symmetric_encrypt" function and make sure it handles unicode values correctly #4528

Merged
merged 2 commits into from
Feb 1, 2019

Conversation

Kami
Copy link
Member

@Kami Kami commented Jan 31, 2019

This pull request fixes an issue reported in #4513.

We didn't correctly handle unicode values so encryption didn't work for unicode values which were not of a correct AES block length (aka when padding was needed).

This pull request fixes that by making sure we convert data to bytes before we pass it to the padding function. It also includes a change in pad function so it handles bytes correctly.

Thanks to @dswebbthg and @nickbaum for reporting and working on this issue.

Resolves #4513.

handle unicode (utf-8) data.

Previously we didn't correctly convert string to bytes before padding
data which resulted in incorrectly padded data (incorrect length) so the
actual encryption step failed.

Thanks to @dswebbthg for reporting this issue.

Resolves #4513.
@Kami Kami added this to the 2.10.2 milestone Jan 31, 2019

def test_symmetric_encrypt_decrypt_utf8_character(self):
values = [
u'£',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the u syntax is supported in Python 3. Better to use six.u() so we can run our tests with Python 3.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is - u as in prefix for strings which is also a default (e.g. u'foo).

Those tests also run under Python 2.x and 3.6 on Travis and I also ran them under both versions locally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, good to know.

@blag
Copy link
Contributor

blag commented Jan 31, 2019

    if isinstance(plaintext, (six.text_type, six.string_types)):
        # Convert data to bytes
        data = plaintext.encode('utf-8')

This means that if I encrypt a string, I would get back bytes when I decrypt it, right? That's a little weird, but it's the previous behavior.

@Kami
Copy link
Member Author

Kami commented Jan 31, 2019

@blag When you decrypt a string you always get back unicode -

decrypted = pkcs5_unpad(decrypted)
.

Also see tests - they perform encrypt and decrypt round-trip and decrypted value is the same as the original one (unicode string).

@Kami Kami merged commit cdd8605 into master Feb 1, 2019
@Kami Kami deleted the fix_encrypt_short_utf8_data branch February 1, 2019 09:10
@Kami Kami added the unicode label Feb 4, 2019
Kami added a commit that referenced this pull request Feb 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

st2api encrypt of £ (and other utf-8 chars) fails with python stacktrace
2 participants