Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
serhiy-storchaka wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromserhiy-storchaka:base64-excess-data
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletionsLib/test/test_binascii.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -240,23 +240,21 @@ def assertNonBase64Data(data, expected, ignorechars):

def test_base64_excess_data(self):
# Test excess data exceptions
def assertExcessData(data, non_strict_expected,
ignore_padchar_expected=None):
def assertExcessData(data, non_strict_expected):
assert_regex = r'(?i)Excess data'
data = self.type2test(data)
with self.assertRaisesRegex(binascii.Error, assert_regex):
binascii.a2b_base64(data, strict_mode=True)
self.assertEqual(binascii.a2b_base64(data, strict_mode=False),
non_strict_expected)
if ignore_padchar_expected is not None:
self.assertEqual(binascii.a2b_base64(data, strict_mode=True,
ignorechars=b'='),
ignore_padchar_expected)
self.assertEqual(binascii.a2b_base64(data, strict_mode=True,
ignorechars=b'='),
non_strict_expected)
self.assertEqual(binascii.a2b_base64(data), non_strict_expected)

assertExcessData(b'ab==c', b'i')
assertExcessData(b'ab==cd', b'i', b'i\xb7\x1d')
assertExcessData(b'abc=d', b'i\xb7', b'i\xb7\x1d')
assertExcessData(b'ab==c=', b'i\xb7')
assertExcessData(b'ab==cd', b'i\xb7\x1d')
assertExcessData(b'abc=d', b'i\xb7\x1d')

def test_base64errors(self):
# Test base64 with invalid padding
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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`, it ignores
the pad character, "=", if it is present before the end of the encoded data.
43 changes: 15 additions & 28 deletionsModules/binascii.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -640,36 +640,24 @@ 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;
}
if (!strict_mode || 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;
}

unsigned char v = table_a2b_base64[this_ch];
Expand DownExpand Up@@ -748,7 +736,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:
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp