Skip to content

Commit 1d7e599

Browse files
committed
docs
1 parent 12bef9c commit 1d7e599

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Doc/library/pyexpat.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,53 @@ XMLParser Objects
231231
.. versionadded:: 3.13
232232

233233

234+
:class:`xmlparser` objects have the following methods to mitigate some
235+
well-known XML vulnerabilities.
236+
237+
.. method:: xmlparser.SetAllocTrackerMaximumAmplification(max_factor, /)
238+
239+
Sets the maximum amplification factor between direct input and bytes
240+
of dynamic memory allocated.
241+
242+
By default, parsers objects have a maximum amplification factor of 100.
243+
244+
The amplification factor is calculated as ``allocated / direct``
245+
while parsing, where ``direct`` is the number of bytes read from
246+
the primary document in parsing and ``allocated`` is the number
247+
of bytes of dynamic memory allocated in the parser hierarchy.
248+
249+
The *max_factor* value must be a non-NaN :class:`float` value greater than
250+
or equal to 1.0. Amplifications factors greater than 100 can been observed
251+
near the start of parsing even with benign files in practice. As such, the
252+
upper bound must be carefully chosen so to avoid false positives.
253+
254+
An :exc:`ExpatError` is raised if this method is called by a non-root
255+
parser or if *max_factor* is outside the valid range. The corresponding
256+
:attr:`~ExpatError.lineno` and :attr:`~ExpatError.column` should not be
257+
used as they will have no special meaning.
258+
259+
.. note::
260+
261+
The maximum amplification factor is only considered if the threshold
262+
specified by :meth:`.SetAllocTrackerActivationThreshold` is reached.
263+
264+
.. versionadded:: next
265+
266+
.. method:: xmlparser.SetAllocTrackerActivationThreshold(threshold, /)
267+
268+
Sets the number of allocated bytes of dynamic memory needed to activate
269+
protection against disproportionate use of RAM.
270+
271+
By default, parsers objects have an allocation activation threshold of 64 MiB,
272+
or equivalently 67,108,864 bytes.
273+
274+
An :exc:`ExpatError` is raised if this method is called by a non-root parser.
275+
The corresponding :attr:`~ExpatError.lineno` and :attr:`~ExpatError.column`
276+
should not be used as they will have no special meaning.
277+
278+
.. versionadded:: next
279+
280+
234281
:class:`xmlparser` objects have the following attributes:
235282

236283

0 commit comments

Comments
 (0)