summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorIdan Moral <idan22moral@gmail.com>2021-07-19 00:45:19 (GMT)
committerGitHub <noreply@github.com>2021-07-19 00:45:19 (GMT)
commit35b98e38b6edd63153fc8e092f94cb20725dacc1 (patch)
tree0075d1bd3b9f195c5bd60553f9adcb5ef72a6fa1 /Modules
parentb494685b2548489efcc66993cc6c13b027ce3b26 (diff)
downloadcpython-35b98e38b6edd63153fc8e092f94cb20725dacc1.zip
cpython-35b98e38b6edd63153fc8e092f94cb20725dacc1.tar.gz
cpython-35b98e38b6edd63153fc8e092f94cb20725dacc1.tar.bz2
bpo-43086: Add handling for out-of-spec data in a2b_base64 (GH-24402)
binascii.a2b_base64 gains a strict_mode= parameter. When enabled it will raise an error on input that deviates from the base64 spec in any way. The default remains False for backward compatibility. Code reviews and minor tweaks by: Gregory P. Smith <greg@krypto.org> [Google]
Diffstat (limited to 'Modules')
-rw-r--r--Modules/binascii.c52
-rw-r--r--Modules/clinic/binascii.c.h37
2 files changed, 75 insertions, 14 deletions
diff --git a/Modules/binascii.c b/Modules/binascii.c
index 59e4b0a..50f25b4 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -433,18 +433,26 @@ binascii.a2b_base64
data: ascii_buffer
/
+ *
+ strict_mode: bool(accept={int}) = False
Decode a line of base64 data.
+
+ strict_mode
+ When set to True, bytes that are not part of the base64 standard are not allowed.
+ The same applies to excess data after padding (= / ==).
[clinic start generated code]*/
static PyObject *
-binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
-/*[clinic end generated code: output=0628223f19fd3f9b input=5872acf6e1cac243]*/
+binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
+/*[clinic end generated code: output=5409557788d4f975 input=3a30c4e3528317c6]*/
{
assert(data->len >= 0);
const unsigned char *ascii_data = data->buf;
size_t ascii_len = data->len;
+ binascii_state *state = NULL;
+ char padding_started = 0;
/* Allocate the buffer */
Py_ssize_t bin_len = ((ascii_len+3)/4)*3; /* Upper bound, corrected later */
@@ -455,6 +463,15 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
return NULL;
unsigned char *bin_data_start = bin_data;
+ if (strict_mode && ascii_len > 0 && ascii_data[0] == '=') {
+ malformed_padding:
+ state = get_binascii_state(module);
+ if (state) {
+ PyErr_SetString(state->Error, "Leading padding not allowed");
+ }
+ goto error_end;
+ }
+
int quad_pos = 0;
unsigned char leftchar = 0;
int pads = 0;
@@ -465,11 +482,21 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
** the invalid ones.
*/
if (this_ch == BASE64_PAD) {
+ padding_started = 1;
+
if (quad_pos >= 2 && quad_pos + ++pads >= 4) {
- /* A pad sequence means no more input.
- ** We've already interpreted the data
- ** from the quad at this point.
+ /* A pad sequence means we should not parse more input.
+ ** We've already interpreted the data from the quad at this point.
+ ** in strict mode, an error should raise if there's excess data after the padding.
*/
+ if (strict_mode && i + 1 < ascii_len) {
+ state = get_binascii_state(module);
+ if (state) {
+ PyErr_SetString(state->Error, "Excess data after padding");
+ }
+ goto error_end;
+ }
+
goto done;
}
continue;
@@ -477,8 +504,20 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
this_ch = table_a2b_base64[this_ch];
if (this_ch >= 64) {
+ if (strict_mode) {
+ state = get_binascii_state(module);
+ if (state) {
+ PyErr_SetString(state->Error, "Only base64 data is allowed");
+ }
+ goto error_end;
+ }
continue;
}
+
+ // Characters that are not '=', in the middle of the padding, are not allowed
+ if (strict_mode && padding_started) {
+ goto malformed_padding;
+ }
pads = 0;
switch (quad_pos) {
@@ -505,7 +544,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
}
if (quad_pos != 0) {
- binascii_state *state = get_binascii_state(module);
+ state = get_binascii_state(module);
if (state == NULL) {
/* error already set, from get_binascii_state */
} else if (quad_pos == 1) {
@@ -522,6 +561,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
} else {
PyErr_SetString(state->Error, "Incorrect padding");
}
+ error_end:
_PyBytesWriter_Dealloc(&writer);
return NULL;
}
diff --git a/Modules/clinic/binascii.c.h b/Modules/clinic/binascii.c.h
index ae1c457..a924004 100644
--- a/Modules/clinic/binascii.c.h
+++ b/Modules/clinic/binascii.c.h
@@ -87,27 +87,48 @@ exit:
}
PyDoc_STRVAR(binascii_a2b_base64__doc__,
-"a2b_base64($module, data, /)\n"
+"a2b_base64($module, data, /, *, strict_mode=False)\n"
"--\n"
"\n"
-"Decode a line of base64 data.");
+"Decode a line of base64 data.\n"
+"\n"
+" strict_mode\n"
+" When set to True, bytes that are not part of the base64 standard are not allowed.\n"
+" The same applies to excess data after padding (= / ==).");
#define BINASCII_A2B_BASE64_METHODDEF \
- {"a2b_base64", (PyCFunction)binascii_a2b_base64, METH_O, binascii_a2b_base64__doc__},
+ {"a2b_base64", (PyCFunction)(void(*)(void))binascii_a2b_base64, METH_FASTCALL|METH_KEYWORDS, binascii_a2b_base64__doc__},
static PyObject *
-binascii_a2b_base64_impl(PyObject *module, Py_buffer *data);
+binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode);
static PyObject *
-binascii_a2b_base64(PyObject *module, PyObject *arg)
+binascii_a2b_base64(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
PyObject *return_value = NULL;
+ static const char * const _keywords[] = {"", "strict_mode", NULL};
+ static _PyArg_Parser _parser = {NULL, _keywords, "a2b_base64", 0};
+ PyObject *argsbuf[2];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Py_buffer data = {NULL, NULL};
+ int strict_mode = 0;
- if (!ascii_buffer_converter(arg, &data)) {
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
+ if (!args) {
goto exit;
}
- return_value = binascii_a2b_base64_impl(module, &data);
+ if (!ascii_buffer_converter(args[0], &data)) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_kwonly;
+ }
+ strict_mode = _PyLong_AsInt(args[1]);
+ if (strict_mode == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+skip_optional_kwonly:
+ return_value = binascii_a2b_base64_impl(module, &data, strict_mode);
exit:
/* Cleanup for data */
@@ -746,4 +767,4 @@ exit:
return return_value;
}
-/*[clinic end generated code: output=95a0178f30801b89 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=0f261ee49971f5ca input=a9049054013a1b77]*/