Fix binascii.Error: Incorrect padding #204
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When decode_param receives a value like this:
"=?ISO-8859-1?B?TEdGIC0gUGFzc28gYSBQYXNzbyAtIEZh?= =?ISO-8859-1?B?dHVyYW1lbnRvIEVsZXRy9G5pY28ucGRm?="
It fails to decode the base64 correctly, and it gives the error:
File "/usr/local/lib/python3.6/site-packages/imbox-0.9.8-py3.6.egg/imbox/messages.py", line 55, in _fetch_email_list
yield uid, self._fetch_email(uid)
File "/usr/local/lib/python3.6/site-packages/imbox-0.9.8-py3.6.egg/imbox/messages.py", line 44, in _fetch_email
parser_policy=self.parser_policy)
File "/usr/local/lib/python3.6/site-packages/imbox-0.9.8-py3.6.egg/imbox/parser.py", line 155, in fetch_email_by_uid
email_object = parse_email(raw_email, policy=parser_policy)
File "/usr/local/lib/python3.6/site-packages/imbox-0.9.8-py3.6.egg/imbox/parser.py", line 212, in parse_email
attachment = parse_attachment(part)
File "/usr/local/lib/python3.6/site-packages/imbox-0.9.8-py3.6.egg/imbox/parser.py", line 115, in parse_attachment
name, value = decode_param(param)
File "/usr/local/lib/python3.6/site-packages/imbox-0.9.8-py3.6.egg/imbox/parser.py", line 80, in decode_param
value = base64.decodebytes(code.encode())
File "/usr/lib64/python3.6/base64.py", line 546, in decodebytes
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
So i changed the regular expression and started using findall instead, i also added a "fixer" for invalid base64 blocks, it adds "=" to the end of it if needed.