Skip to content

bpo-33770: improve base64 exception message for encoded inputs of invalid length#7416

Merged
taleinat merged 7 commits into
python:masterfrom
taleinat:bpo-33770
Jun 10, 2018
Merged

bpo-33770: improve base64 exception message for encoded inputs of invalid length#7416
taleinat merged 7 commits into
python:masterfrom
taleinat:bpo-33770

Conversation

@taleinat

@taleinat taleinat commented Jun 5, 2018

Copy link
Copy Markdown
Contributor

Give "Invalid length" rather than "Invalid padding" in this case.

Also added some specific test cases for invalid padding.

https://bugs.python.org/issue33770

Give "Invalid length" rather than "Invalid padding" in this case.

Also added some specific test cases for invalid padding.
Comment thread Lib/test/test_binascii.py Outdated
# Test base64 with invalid padding
def assertInvalidPadding(data):
with self.assertRaises(binascii.Error,
expected_regexp=r'(?i)Invalid padding'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't remember other case of passing this argument as keyword. I'm not sure the name of this parameter is the part of the public API.

And seems you meant assertRaisesRegex.

@taleinat

taleinat commented Jun 5, 2018

Copy link
Copy Markdown
Contributor Author

Thanks for the review @serhiy-storchaka! To my horror, both the tests and the implementation were actually broken. Now fixed, ready for re-review.

Comment thread Modules/binascii.c Outdated
** This is an invalid length, as there is no possible input that
** could encoded into such a base64 string.
*/
PyErr_SetString(Error, "Invalid length");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we're going to change this, I'd suggest adding some verbiage to the error message saying that padding ==6 (or whatever) is invalid in base64, and probably mentioning the value of leftbits, if that's useful to the caller. But just having two different error messages for different cases with no additional context would confuse me more than it would help me.

@taleinat taleinat Jun 5, 2018

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Something like this? "Invalid base64-encoded string: length cannot be 1 more than a multiple of 4"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that it is important, IMO, that the messages be different. In the 'incorrect padding' case one can add padding to get a decode (which may be wrong, but most likely will be right). In the invalid length case, one cannot do that. The email package cares about this, as it tries its best to do the decode even if the padding is incorrect. I'm pretty sure there is an open issue against the email package for which this is the answer, though I don't have time to go look for it right now :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@bitdancer, were you referring to bpo-27397?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Also, if this difference is important, perhaps this should be a separate exception class/sub-class?

@taleinat
taleinat requested a review from a team as a code owner June 8, 2018 21:08
@taleinat
taleinat merged commit 1b85c71 into python:master Jun 10, 2018
@bedevere-bot

Copy link
Copy Markdown

@taleinat: Please replace # with GH- in the commit message next time. Thanks!

@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @taleinat for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 10, 2018
…alid length (pythonGH-7416)

(cherry picked from commit 1b85c71)

Co-authored-by: Tal Einat <taleinat+github@gmail.com>
@bedevere-bot

Copy link
Copy Markdown

GH-7602 is a backport of this pull request to the 3.7 branch.

ned-deily pushed a commit that referenced this pull request Jun 10, 2018
…alid length (GH-7416) (GH-7602)

(cherry picked from commit 1b85c71)

Co-authored-by: Tal Einat <taleinat+github@gmail.com>
@taleinat
taleinat deleted the bpo-33770 branch June 11, 2018 05:31
jugmac00 pushed a commit to jugmac00/launchpad that referenced this pull request Dec 5, 2022
python/cpython#7416 changed the exception
message raised for base64 inputs whose length has a remainder of 1 when
divided by 4.  In these tests, we want to make sure that the underlying
exception message is passed through to allow debugging, but we don't
much care about the exact details, so change the input data to something
that raises an "Incorrect padding" exception on Python 3.8 as well as on
earlier versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants