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

binascii.a2b_base64 ignores padding inside a string #94703

Closed
DemiMarie opened this issue Jul 9, 2022 · 5 comments
Closed

binascii.a2b_base64 ignores padding inside a string #94703

DemiMarie opened this issue Jul 9, 2022 · 5 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@DemiMarie
Copy link

Bug report

The following code should raise binascii.Error but does not:

import binascii
binascii.a2b_base64(b'=aa==')

It appears (from looking at the source code) that this is also the case in 3.11, even in strict mode.

Your environment

  • CPython versions tested on: 3.10
  • Operating system and architecture: Fedora 36, Linux 5.15.52, x86_64
@DemiMarie DemiMarie added the type-bug An unexpected behavior, bug, or error label Jul 9, 2022
@yourlefthandman
Copy link
Contributor

yourlefthandman commented Jul 9, 2022

When running the code from main, the behaviour is as you expect it to be :)

>>> binascii.a2b_base64(b'=aa==', strict_mode=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
binascii.Error: Leading padding not allowed

The following code handles it-

if (strict_mode && ascii_len > 0 && ascii_data[0] == '=') {
    state = get_binascii_state(module);
    if (state) {
        PyErr_SetString(state->Error, "Leading padding not allowed");
    }
    goto error_end;
}

Looking at 3.11, i see the code is there too - link

@DemiMarie
Copy link
Author

Does it reject this input?

import binascii
binascii.a2b_base64(b'aaaa=aa==', strict_mode=True)

?

@yourlefthandman
Copy link
Contributor

Yup :)

>>> import binascii
>>> binascii.a2b_base64(b'aaaa=aa==', strict_mode=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
binascii.Error: Discontinuous padding not allowed

@Mohitduklan
Copy link

Is it because python 3.10.5 uses REF 4648. https://docs.python.org/3/library/base64.html

@idan22moral
Copy link
Contributor

idan22moral commented Nov 4, 2022

Hi @DemiMarie,
This behavior is normal in 3.10, since the strict_mode flag was not introduced yet.
In 3.11.0 it behaves as expected:

Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import binascii
>>> binascii.a2b_base64(b'=aa==')
b'i'
>>> binascii.a2b_base64(b'=aa==', strict_mode=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
binascii.Error: Leading padding not allowed
>>> binascii.a2b_base64(b'aaaa=aa==')
b'i\xa6\x9ai'
>>> binascii.a2b_base64(b'aaaa=aa==', strict_mode=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
binascii.Error: Discontinuous padding not allowed

For further information, please read:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants