-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-145264: Do not ignore excess Base64 data after the first padded quad #145267
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
Open
serhiy-storchaka
wants to merge
3
commits into
python:main
Choose a base branch
from
serhiy-storchaka:base64-excess-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+36
−41
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2026-02-26-20-13-16.gh-issue-145264.4pggX_.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Base64 decoder (see :func:`binascii.a2b_base64`, :func:`base64.b64decode`, etc) no | ||
| longer ignores excess data after the first padded quad in non-strict | ||
| (default) mode. Instead, in conformance with :rfc:`4648`, section 3.3, it now ignores | ||
| the pad character, "=", if it is present before the end of the encoded data. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -640,40 +640,33 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode, | |
| */ | ||
| if (this_ch == BASE64_PAD) { | ||
| pads++; | ||
|
|
||
| if (strict_mode) { | ||
| if (quad_pos >= 2 && quad_pos + pads <= 4) { | ||
| continue; | ||
| } | ||
| if (ignorechar(BASE64_PAD, ignorechars, ignorecache)) { | ||
| continue; | ||
| } | ||
| if (quad_pos == 1) { | ||
| /* Set an error below. */ | ||
| break; | ||
| } | ||
| state = get_binascii_state(module); | ||
| if (state) { | ||
| PyErr_SetString(state->Error, | ||
| (quad_pos == 0 && ascii_data == data->buf) | ||
| ? "Leading padding not allowed" | ||
| : "Excess padding not allowed"); | ||
| } | ||
| goto error_end; | ||
| if (quad_pos >= 2 && quad_pos + pads <= 4) { | ||
| continue; | ||
| } | ||
| else { | ||
| if (quad_pos >= 2 && quad_pos + pads >= 4) { | ||
| /* A pad sequence means we should not parse more input. | ||
| ** We've already interpreted the data from the quad at this point. | ||
| */ | ||
| goto done; | ||
| } | ||
| // See RFC 4648, section-3.3: "specifications MAY ignore the | ||
| // pad character, "=", treating it as non-alphabet data, if | ||
| // it is present before the end of the encoded data" and | ||
| // "the excess pad characters MAY also be ignored." | ||
| if (!strict_mode || ignorechar(BASE64_PAD, ignorechars, ignorecache)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment in this block linking to the RFC section.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| continue; | ||
| } | ||
| if (quad_pos == 1) { | ||
| /* Set an error below. */ | ||
| break; | ||
| } | ||
| state = get_binascii_state(module); | ||
| if (state) { | ||
| PyErr_SetString(state->Error, | ||
| (quad_pos == 0 && ascii_data == data->buf) | ||
| ? "Leading padding not allowed" | ||
| : "Excess padding not allowed"); | ||
| } | ||
| goto error_end; | ||
| } | ||
|
|
||
| unsigned char v = table_a2b_base64[this_ch]; | ||
| if (v >= 64) { | ||
| // See RFC 4648, section-3.3. | ||
| if (strict_mode && !ignorechar(this_ch, ignorechars, ignorecache)) { | ||
| state = get_binascii_state(module); | ||
| if (state) { | ||
|
|
@@ -684,7 +677,8 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode, | |
| continue; | ||
| } | ||
|
|
||
| // Characters that are not '=', in the middle of the padding, are not allowed | ||
| // Characters that are not '=', in the middle of the padding, are | ||
| // not allowed (except when they are). See RFC 4648, section-3.3. | ||
| if (pads && strict_mode && | ||
| !ignorechar(BASE64_PAD, ignorechars, ignorecache)) | ||
| { | ||
|
|
@@ -748,7 +742,6 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode, | |
| goto error_end; | ||
| } | ||
|
|
||
| done: | ||
| return PyBytesWriter_FinishWithPointer(writer, bin_data); | ||
|
|
||
| error_end: | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test used to highlight the difference between strict and non-strict mode. we should keep a test highlighting that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In strict mode we get an error. We get a result only when
strict_mode=Falseor new argumentignorecharscontains "=", and they given different results. Now this difference has been fixed.