Skip to content

Commit 5680561

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 a295473 commit 5680561

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
}
@@ -1149,6 +1139,50 @@ pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, PyTypeObject *cls,
11491139
}
11501140
#endif
11511141

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

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

0 commit comments

Comments
 (0)