Skip to content

Commit 6ae7665

Browse files
picnixzhartwork
authored andcommitted
pythongh-90949: amend pythonGH-139234 in prevision of future mitigation API (python#139366)
Fix some typos left in f04bea4, and simplify some internal functions to ease maintenance of future mitigation APIs. (cherry picked from commit 68a1778)
1 parent de690f1 commit 6ae7665

File tree

3 files changed

+72
-57
lines changed

3 files changed

+72
-57
lines changed

Doc/library/pyexpat.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ common XML vulnerabilities.
282282
.. note::
283283

284284
The maximum amplification factor is only considered if the threshold
285-
that can be adjusted :meth:`.SetAllocTrackerActivationThreshold` is
286-
exceeded.
285+
that can be adjusted by :meth:`.SetAllocTrackerActivationThreshold`
286+
is exceeded.
287287

288288
.. versionadded:: next
289289

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Add :func:`~xml.parsers.expat.xmlparser.SetAllocTrackerActivationThreshold`
2-
and :func:`~xml.parsers.expat.xmlparser.SetAllocTrackerMaximumAmplification`
1+
Add :meth:`~xml.parsers.expat.xmlparser.SetAllocTrackerActivationThreshold`
2+
and :meth:`~xml.parsers.expat.xmlparser.SetAllocTrackerMaximumAmplification`
33
to :ref:`xmlparser <xmlparser-objects>` objects to prevent use of
44
disproportional amounts of dynamic memory from within an Expat parser.
55
Patch by Bénédikt Tran.

Modules/pyexpat.c

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ struct HandlerInfo {
107107
static struct HandlerInfo handler_info[64];
108108

109109
static int
110-
set_error_code(PyObject *err, enum XML_Error code)
110+
set_xml_error_attr_code(PyObject *err, enum XML_Error code)
111111
{
112112
PyObject *v = PyLong_FromLong((long)code);
113113
int ok = v != NULL && PyObject_SetAttr(err, &_Py_ID(code), v) != -1;
@@ -119,7 +119,7 @@ set_error_code(PyObject *err, enum XML_Error code)
119119
* false on an exception.
120120
*/
121121
static int
122-
set_error_location(PyObject *err, const char *name, XML_Size value)
122+
set_xml_error_attr_location(PyObject *err, const char *name, XML_Size value)
123123
{
124124
PyObject *v = PyLong_FromSize_t((size_t)value);
125125
int ok = v != NULL && PyObject_SetAttrString(err, name, v) != -1;
@@ -128,42 +128,32 @@ set_error_location(PyObject *err, const char *name, XML_Size value)
128128
}
129129

130130

131-
static PyObject *
132-
format_xml_error(enum XML_Error code, XML_Size lineno, XML_Size column)
133-
{
134-
const char *errmsg = XML_ErrorString(code);
135-
PyUnicodeWriter *writer = PyUnicodeWriter_Create(strlen(errmsg) + 1);
136-
if (writer == NULL) {
137-
return NULL;
138-
}
139-
if (PyUnicodeWriter_Format(writer,
140-
"%s: line %zu, column %zu",
141-
errmsg, (size_t)lineno, (size_t)column) < 0)
142-
{
143-
PyUnicodeWriter_Discard(writer);
144-
return NULL;
145-
}
146-
return PyUnicodeWriter_Finish(writer);
147-
}
148-
149131
static PyObject *
150132
set_xml_error(pyexpat_state *state,
151133
enum XML_Error code, XML_Size lineno, XML_Size column,
152134
const char *errmsg)
153135
{
154-
PyObject *arg = errmsg == NULL
155-
? format_xml_error(code, lineno, column)
156-
: PyUnicode_FromStringAndSize(errmsg, strlen(errmsg));
136+
PyObject *arg;
137+
if (errmsg == NULL) {
138+
arg = PyUnicode_FromFormat(
139+
"%s: line %zu, column %zu",
140+
XML_ErrorString(code),
141+
(size_t)lineno, (size_t)column
142+
);
143+
}
144+
else {
145+
arg = PyUnicode_FromStringAndSize(errmsg, strlen(errmsg));
146+
}
157147
if (arg == NULL) {
158148
return NULL;
159149
}
160150
PyObject *res = PyObject_CallOneArg(state->error, arg);
161151
Py_DECREF(arg);
162152
if (
163153
res != NULL
164-
&& set_error_code(res, code)
165-
&& set_error_location(res, "lineno", lineno)
166-
&& set_error_location(res, "offset", column)
154+
&& set_xml_error_attr_code(res, code)
155+
&& set_xml_error_attr_location(res, "lineno", lineno)
156+
&& set_xml_error_attr_location(res, "offset", column)
167157
) {
168158
PyErr_SetObject(state->error, res);
169159
}
@@ -1148,6 +1138,50 @@ pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, PyTypeObject *cls,
11481138
}
11491139
#endif
11501140

1141+
#if XML_COMBINED_VERSION >= 20702
1142+
static PyObject *
1143+
set_activation_threshold(xmlparseobject *self,
1144+
PyTypeObject *cls,
1145+
unsigned long long threshold,
1146+
XML_Bool (*setter)(XML_Parser, unsigned long long))
1147+
{
1148+
assert(self->itself != NULL);
1149+
if (setter(self->itself, threshold) == XML_TRUE) {
1150+
Py_RETURN_NONE;
1151+
}
1152+
// The setter fails if self->itself is NULL (which is not possible here)
1153+
// or is a non-root parser, which currently only happens for parsers
1154+
// created by ExternalEntityParserCreate().
1155+
pyexpat_state *state = PyType_GetModuleState(cls);
1156+
return set_invalid_arg(state, self, "parser must be a root parser");
1157+
}
1158+
1159+
static PyObject *
1160+
set_maximum_amplification(xmlparseobject *self,
1161+
PyTypeObject *cls,
1162+
float max_factor,
1163+
XML_Bool (*setter)(XML_Parser, float))
1164+
{
1165+
assert(self->itself != NULL);
1166+
if (setter(self->itself, max_factor) == XML_TRUE) {
1167+
Py_RETURN_NONE;
1168+
}
1169+
// The setter fails if self->itself is NULL (which is not possible here),
1170+
// is a non-root parser, which currently only happens for parsers created
1171+
// by ExternalEntityParserCreate(), or if 'max_factor' is NaN or < 1.0.
1172+
pyexpat_state *state = PyType_GetModuleState(cls);
1173+
// Note: Expat has no API to determine whether a parser is a root parser,
1174+
// and since the Expat functions for defining the various maximum allowed
1175+
// amplifcation factors fail when a bad parser or an out-of-range factor
1176+
// is given without specifying which check failed, we check whether the
1177+
// factor is out-of-range to improve the error message. See also gh-90949.
1178+
const char *message = (isnan(max_factor) || max_factor < 1.0f)
1179+
? "'max_factor' must be at least 1.0"
1180+
: "parser must be a root parser";
1181+
return set_invalid_arg(state, self, message);
1182+
}
1183+
#endif
1184+
11511185
#if XML_COMBINED_VERSION >= 20702
11521186
/*[clinic input]
11531187
pyexpat.xmlparser.SetAllocTrackerActivationThreshold
@@ -1167,15 +1201,10 @@ pyexpat_xmlparser_SetAllocTrackerActivationThreshold_impl(xmlparseobject *self,
11671201
unsigned long long threshold)
11681202
/*[clinic end generated code: output=bed7e93207ba08c5 input=9c706b75c18e4ea1]*/
11691203
{
1170-
assert(self->itself != NULL);
1171-
if (XML_SetAllocTrackerActivationThreshold(self->itself, threshold) == XML_TRUE) {
1172-
Py_RETURN_NONE;
1173-
}
1174-
// XML_SetAllocTrackerActivationThreshold() can only fail if self->itself
1175-
// is not a root parser (currently, this is equivalent to be created
1176-
// by ExternalEntityParserCreate()).
1177-
pyexpat_state *state = PyType_GetModuleState(cls);
1178-
return set_invalid_arg(state, self, "parser must be a root parser");
1204+
return set_activation_threshold(
1205+
self, cls, threshold,
1206+
XML_SetAllocTrackerActivationThreshold
1207+
);
11791208
}
11801209
#endif
11811210

@@ -1208,24 +1237,10 @@ pyexpat_xmlparser_SetAllocTrackerMaximumAmplification_impl(xmlparseobject *self,
12081237
float max_factor)
12091238
/*[clinic end generated code: output=6e44bd48c9b112a0 input=918b9266b490a722]*/
12101239
{
1211-
assert(self->itself != NULL);
1212-
if (XML_SetAllocTrackerMaximumAmplification(self->itself, max_factor) == XML_TRUE) {
1213-
Py_RETURN_NONE;
1214-
}
1215-
// XML_SetAllocTrackerMaximumAmplification() can fail if self->itself
1216-
// is not a root parser (currently, this is equivalent to be created
1217-
// by ExternalEntityParserCreate()) or if 'max_factor' is NaN or < 1.0.
1218-
//
1219-
// Expat does not provide a way to determine whether a parser is a root
1220-
// or not, nor does it provide a way to distinguish between failures in
1221-
// XML_SetAllocTrackerMaximumAmplification() (see gh-90949), we manually
1222-
// detect the factor out-of-range issue here so that users have a better
1223-
// error message.
1224-
pyexpat_state *state = PyType_GetModuleState(cls);
1225-
const char *message = (isnan(max_factor) || max_factor < 1.0f)
1226-
? "'max_factor' must be at least 1.0"
1227-
: "parser must be a root parser";
1228-
return set_invalid_arg(state, self, message);
1240+
return set_maximum_amplification(
1241+
self, cls, max_factor,
1242+
XML_SetAllocTrackerMaximumAmplification
1243+
);
12291244
}
12301245
#endif
12311246

0 commit comments

Comments
 (0)