| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bbcb03e commit 8f0fa4b
27 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4851,7 +4851,7 @@ | |||
| 4851 | 4851 | <var-decl name='root_cframe' type-id='type-id-23' visibility='default' filepath='./Include/cpython/pystate.h' line='148' column='1'/> | |
| 4852 | 4852 | </data-member> | |
| 4853 | 4853 | </class-decl> | |
| 4854 | - <class-decl name='_is' size-in-bits='908160' is-struct='yes' visibility='default' filepath='./Include/internal/pycore_interp.h' line='220' column='1' id='type-id-224'> | ||
| 4854 | + <class-decl name='_is' size-in-bits='908224' is-struct='yes' visibility='default' filepath='./Include/internal/pycore_interp.h' line='220' column='1' id='type-id-224'> | ||
| 4855 | 4855 | <data-member access='public' layout-offset-in-bits='0'> | |
| 4856 | 4856 | <var-decl name='next' type-id='type-id-225' visibility='default' filepath='./Include/internal/pycore_interp.h' line='222' column='1'/> | |
| 4857 | 4857 | </data-member> | |
@@ -5002,6 +5002,9 @@ | |||
| 5002 | 5002 | <data-member access='public' layout-offset-in-bits='121728'> | |
| 5003 | 5003 | <var-decl name='type_cache' type-id='type-id-249' visibility='default' filepath='./Include/internal/pycore_interp.h' line='313' column='1'/> | |
| 5004 | 5004 | </data-member> | |
| 5005 | + <data-member access='public' layout-offset-in-bits='908160'> | ||
| 5006 | + <var-decl name='int_max_str_digits' type-id='type-id-9' visibility='default' filepath='./Include/internal/pycore_interp.h' line='309' column='1'/> | ||
| 5007 | + </data-member> | ||
| 5005 | 5008 | </class-decl> | |
| 5006 | 5009 | <pointer-type-def type-id='type-id-224' size-in-bits='64' id='type-id-225'/> | |
| 5007 | 5010 | <class-decl name='pyruntimestate' size-in-bits='5376' is-struct='yes' visibility='default' filepath='./Include/internal/pycore_runtime.h' line='61' column='1' id='type-id-250'> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -893,6 +893,14 @@ are always available. They are listed here in alphabetical order. | |||
| 893 | 893 | .. versionchanged:: 3.8 | |
| 894 | 894 | Falls back to :meth:`__index__` if :meth:`__int__` is not defined. | |
| 895 | 895 | ||
| 896 | + .. versionchanged:: 3.10.7 | ||
| 897 | + :class:`int` string inputs and string representations can be limited to | ||
| 898 | + help avoid denial of service attacks. A :exc:`ValueError` is raised when | ||
| 899 | + the limit is exceeded while converting a string *x* to an :class:`int` or | ||
| 900 | + when converting an :class:`int` into a string would exceed the limit. | ||
| 901 | + See the :ref:`integer string conversion length limitation | ||
| 902 | + <int_max_str_digits>` documentation. | ||
| 903 | + | ||
| 896 | 904 | ||
| 897 | 905 | .. function:: isinstance(object, classinfo) | |
| 898 | 906 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,11 @@ is a lightweight data interchange format inspired by | |||
| 18 | 18 | `JavaScript <https://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax | |
| 19 | 19 | (although it is not a strict subset of JavaScript [#rfc-errata]_ ). | |
| 20 | 20 | ||
| 21 | + .. warning:: | ||
| 22 | + Be cautious when parsing JSON data from untrusted sources. A malicious | ||
| 23 | + JSON string may cause the decoder to consume considerable CPU and memory | ||
| 24 | + resources. Limiting the size of data to be parsed is recommended. | ||
| 25 | + | ||
| 21 | 26 | :mod:`json` exposes an API familiar to users of the standard library | |
| 22 | 27 | :mod:`marshal` and :mod:`pickle` modules. | |
| 23 | 28 | ||
@@ -248,6 +253,12 @@ Basic Usage | |||
| 248 | 253 | be used to use another datatype or parser for JSON integers | |
| 249 | 254 | (e.g. :class:`float`). | |
| 250 | 255 | ||
| 256 | + .. versionchanged:: 3.10.7 | ||
| 257 | + The default *parse_int* of :func:`int` now limits the maximum length of | ||
| 258 | + the integer string via the interpreter's :ref:`integer string | ||
| 259 | + conversion length limitation <int_max_str_digits>` to help avoid denial | ||
| 260 | + of service attacks. | ||
| 261 | + | ||
| 251 | 262 | *parse_constant*, if specified, will be called with one of the following | |
| 252 | 263 | strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. | |
| 253 | 264 | This can be used to raise an exception if invalid JSON numbers | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -584,6 +584,13 @@ class`. float also has the following additional methods. | |||
| 584 | 584 | :exc:`OverflowError` on infinities and a :exc:`ValueError` on | |
| 585 | 585 | NaNs. | |
| 586 | 586 | ||
| 587 | + .. note:: | ||
| 588 | + | ||
| 589 | + The values returned by ``as_integer_ratio()`` can be huge. Attempts | ||
| 590 | + to render such integers into decimal strings may bump into the | ||
| 591 | + :ref:`integer string conversion length limitation | ||
| 592 | + <int_max_str_digits>`. | ||
| 593 | + | ||
| 587 | 594 | .. method:: float.is_integer() | |
| 588 | 595 | ||
| 589 | 596 | Return ``True`` if the float instance is finite with integral | |
@@ -5407,6 +5414,165 @@ types, where they are relevant. Some of these are not reported by the | |||
| 5407 | 5414 | [<class 'bool'>] | |
| 5408 | 5415 | ||
| 5409 | 5416 | ||
| 5417 | + .. _int_max_str_digits: | ||
| 5418 | + | ||
| 5419 | + Integer string conversion length limitation | ||
| 5420 | + =========================================== | ||
| 5421 | + | ||
| 5422 | + CPython has a global limit for converting between :class:`int` and :class:`str` | ||
| 5423 | + to mitigate denial of service attacks. This limit *only* applies to decimal or | ||
| 5424 | + other non-power-of-two number bases. Hexadecimal, octal, and binary conversions | ||
| 5425 | + are unlimited. The limit can be configured. | ||
| 5426 | + | ||
| 5427 | + The :class:`int` type in CPython is an abitrary length number stored in binary | ||
| 5428 | + form (commonly known as a "bignum"). There exists no algorithm that can convert | ||
| 5429 | + a string to a binary integer or a binary integer to a string in linear time, | ||
| 5430 | + *unless* the base is a power of 2. Even the best known algorithms for base 10 | ||
| 5431 | + have sub-quadratic complexity. Converting a large value such as ``int('1' * | ||
| 5432 | + 500_000)`` can take over a second on a fast CPU. | ||
| 5433 | + | ||
| 5434 | + Limiting conversion size offers a practical way to avoid `CVE-2020-10735 | ||
| 5435 | + <https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-10735>`_. | ||
| 5436 | + | ||
| 5437 | + The limit is applied to the number of digit characters in the input or output | ||
| 5438 | + string when a non-linear conversion algorithm would be involved. Underscores | ||
| 5439 | + and the sign are not counted towards the limit. | ||
| 5440 | + | ||
| 5441 | + When an operation would exceed the limit, a :exc:`ValueError` is raised: | ||
| 5442 | + | ||
| 5443 | + .. doctest:: | ||
| 5444 | + | ||
| 5445 | + >>> import sys | ||
| 5446 | + >>> sys.set_int_max_str_digits(4300) # Illustrative, this is the default. | ||
| 5447 | + >>> _ = int('2' * 5432) | ||
| 5448 | + Traceback (most recent call last): | ||
| 5449 | + ... | ||
| 5450 | + ValueError: Exceeds the limit (4300) for integer string conversion: value has 5432 digits. | ||
| 5451 | + >>> i = int('2' * 4300) | ||
| 5452 | + >>> len(str(i)) | ||
| 5453 | + 4300 | ||
| 5454 | + >>> i_squared = i*i | ||
| 5455 | + >>> len(str(i_squared)) | ||
| 5456 | + Traceback (most recent call last): | ||
| 5457 | + ... | ||
| 5458 | + ValueError: Exceeds the limit (4300) for integer string conversion: value has 8599 digits. | ||
| 5459 | + >>> len(hex(i_squared)) | ||
| 5460 | + 7144 | ||
| 5461 | + >>> assert int(hex(i_squared), base=16) == i*i # Hexadecimal is unlimited. | ||
| 5462 | + | ||
| 5463 | + The default limit is 4300 digits as provided in | ||
| 5464 | + :data:`sys.int_info.default_max_str_digits <sys.int_info>`. | ||
| 5465 | + The lowest limit that can be configured is 640 digits as provided in | ||
| 5466 | + :data:`sys.int_info.str_digits_check_threshold <sys.int_info>`. | ||
| 5467 | + | ||
| 5468 | + Verification: | ||
| 5469 | + | ||
| 5470 | + .. doctest:: | ||
| 5471 | + | ||
| 5472 | + >>> import sys | ||
| 5473 | + >>> assert sys.int_info.default_max_str_digits == 4300, sys.int_info | ||
| 5474 | + >>> assert sys.int_info.str_digits_check_threshold == 640, sys.int_info | ||
| 5475 | + >>> msg = int('578966293710682886880994035146873798396722250538762761564' | ||
| 5476 | + ... '9252925514383915483333812743580549779436104706260696366600' | ||
| 5477 | + ... '571186405732').to_bytes(53, 'big') | ||
| 5478 | + ... | ||
| 5479 | + | ||
| 5480 | + .. versionadded:: 3.10.7 | ||
| 5481 | + | ||
| 5482 | + Affected APIs | ||
| 5483 | + ------------- | ||
| 5484 | + | ||
| 5485 | + The limition only applies to potentially slow conversions between :class:`int` | ||
| 5486 | + and :class:`str` or :class:`bytes`: | ||
| 5487 | + | ||
| 5488 | + * ``int(string)`` with default base 10. | ||
| 5489 | + * ``int(string, base)`` for all bases that are not a power of 2. | ||
| 5490 | + * ``str(integer)``. | ||
| 5491 | + * ``repr(integer)`` | ||
| 5492 | + * any other string conversion to base 10, for example ``f"{integer}"``, | ||
| 5493 | + ``"{}".format(integer)``, or ``b"%d" % integer``. | ||
| 5494 | + | ||
| 5495 | + The limitations do not apply to functions with a linear algorithm: | ||
| 5496 | + | ||
| 5497 | + * ``int(string, base)`` with base 2, 4, 8, 16, or 32. | ||
| 5498 | + * :func:`int.from_bytes` and :func:`int.to_bytes`. | ||
| 5499 | + * :func:`hex`, :func:`oct`, :func:`bin`. | ||
| 5500 | + * :ref:`formatspec` for hex, octal, and binary numbers. | ||
| 5501 | + * :class:`str` to :class:`float`. | ||
| 5502 | + * :class:`str` to :class:`decimal.Decimal`. | ||
| 5503 | + | ||
| 5504 | + Configuring the limit | ||
| 5505 | + --------------------- | ||
| 5506 | + | ||
| 5507 | + Before Python starts up you can use an environment variable or an interpreter | ||
| 5508 | + command line flag to configure the limit: | ||
| 5509 | + | ||
| 5510 | + * :envvar:`PYTHONINTMAXSTRDIGITS`, e.g. | ||
| 5511 | + ``PYTHONINTMAXSTRDIGITS=640 python3`` to set the limit to 640 or | ||
| 5512 | + ``PYTHONINTMAXSTRDIGITS=0 python3`` to disable the limitation. | ||
| 5513 | + * :option:`-X int_max_str_digits <-X>`, e.g. | ||
| 5514 | + ``python3 -X int_max_str_digits=640`` | ||
| 5515 | + * :data:`sys.flags.int_max_str_digits` contains the value of | ||
| 5516 | + :envvar:`PYTHONINTMAXSTRDIGITS` or :option:`-X int_max_str_digits <-X>`. | ||
| 5517 | + If both the env var and the ``-X`` option are set, the ``-X`` option takes | ||
| 5518 | + precedence. A value of *-1* indicates that both were unset, thus a value of | ||
| 5519 | + :data:`sys.int_info.default_max_str_digits` was used during initilization. | ||
| 5520 | + | ||
| 5521 | + From code, you can inspect the current limit and set a new one using these | ||
| 5522 | + :mod:`sys` APIs: | ||
| 5523 | + | ||
| 5524 | + * :func:`sys.get_int_max_str_digits` and :func:`sys.set_int_max_str_digits` are | ||
| 5525 | + a getter and setter for the interpreter-wide limit. Subinterpreters have | ||
| 5526 | + their own limit. | ||
| 5527 | + | ||
| 5528 | + Information about the default and minimum can be found in :attr:`sys.int_info`: | ||
| 5529 | + | ||
| 5530 | + * :data:`sys.int_info.default_max_str_digits <sys.int_info>` is the compiled-in | ||
| 5531 | + default limit. | ||
| 5532 | + * :data:`sys.int_info.str_digits_check_threshold <sys.int_info>` is the lowest | ||
| 5533 | + accepted value for the limit (other than 0 which disables it). | ||
| 5534 | + | ||
| 5535 | + .. versionadded:: 3.10.7 | ||
| 5536 | + | ||
| 5537 | + .. caution:: | ||
| 5538 | + | ||
| 5539 | + Setting a low limit *can* lead to problems. While rare, code exists that | ||
| 5540 | + contains integer constants in decimal in their source that exceed the | ||
| 5541 | + minimum threshold. A consequence of setting the limit is that Python source | ||
| 5542 | + code containing decimal integer literals longer than the limit will | ||
| 5543 | + encounter an error during parsing, usually at startup time or import time or | ||
| 5544 | + even at installation time - anytime an up to date ``.pyc`` does not already | ||
| 5545 | + exist for the code. A workaround for source that contains such large | ||
| 5546 | + constants is to convert them to ``0x`` hexadecimal form as it has no limit. | ||
| 5547 | + | ||
| 5548 | + Test your application thoroughly if you use a low limit. Ensure your tests | ||
| 5549 | + run with the limit set early via the environment or flag so that it applies | ||
| 5550 | + during startup and even during any installation step that may invoke Python | ||
| 5551 | + to precompile ``.py`` sources to ``.pyc`` files. | ||
| 5552 | + | ||
| 5553 | + Recommended configuration | ||
| 5554 | + ------------------------- | ||
| 5555 | + | ||
| 5556 | + The default :data:`sys.int_info.default_max_str_digits` is expected to be | ||
| 5557 | + reasonable for most applications. If your application requires a different | ||
| 5558 | + limit, set it from your main entry point using Python version agnostic code as | ||
| 5559 | + these APIs were added in security patch releases in versions before 3.11. | ||
| 5560 | + | ||
| 5561 | + Example:: | ||
| 5562 | + | ||
| 5563 | + >>> import sys | ||
| 5564 | + >>> if hasattr(sys, "set_int_max_str_digits"): | ||
| 5565 | + ... upper_bound = 68000 | ||
| 5566 | + ... lower_bound = 4004 | ||
| 5567 | + ... current_limit = sys.get_int_max_str_digits() | ||
| 5568 | + ... if current_limit == 0 or current_limit > upper_bound: | ||
| 5569 | + ... sys.set_int_max_str_digits(upper_bound) | ||
| 5570 | + ... elif current_limit < lower_bound: | ||
| 5571 | + ... sys.set_int_max_str_digits(lower_bound) | ||
| 5572 | + | ||
| 5573 | + If you need to disable it entirely, set it to ``0``. | ||
| 5574 | + | ||
| 5575 | + | ||
| 5410 | 5576 | .. rubric:: Footnotes | |
| 5411 | 5577 | ||
| 5412 | 5578 | .. [1] Additional information on these special methods may be found in the Python | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -460,9 +460,9 @@ always available. | |||
| 460 | 460 | The :term:`named tuple` *flags* exposes the status of command line | |
| 461 | 461 | flags. The attributes are read only. | |
| 462 | 462 | ||
| 463 | - ============================= ================================================================ | ||
| 463 | + ============================= ============================================================================================================== | ||
| 464 | 464 | attribute flag | |
| 465 | - ============================= ================================================================ | ||
| 465 | + ============================= ============================================================================================================== | ||
| 466 | 466 | :const:`debug` :option:`-d` | |
| 467 | 467 | :const:`inspect` :option:`-i` | |
| 468 | 468 | :const:`interactive` :option:`-i` | |
@@ -478,7 +478,8 @@ always available. | |||
| 478 | 478 | :const:`hash_randomization` :option:`-R` | |
| 479 | 479 | :const:`dev_mode` :option:`-X dev <-X>` (:ref:`Python Development Mode <devmode>`) | |
| 480 | 480 | :const:`utf8_mode` :option:`-X utf8 <-X>` | |
| 481 | - ============================= ================================================================ | ||
| 481 | + :const:`int_max_str_digits` :option:`-X int_max_str_digits <-X>` (:ref:`integer string conversion length limitation <int_max_str_digits>`) | ||
| 482 | + ============================= ============================================================================================================== | ||
| 482 | 483 | ||
| 483 | 484 | .. versionchanged:: 3.2 | |
| 484 | 485 | Added ``quiet`` attribute for the new :option:`-q` flag. | |
@@ -497,6 +498,9 @@ always available. | |||
| 497 | 498 | Mode <devmode>` and the ``utf8_mode`` attribute for the new :option:`-X` | |
| 498 | 499 | ``utf8`` flag. | |
| 499 | 500 | ||
| 501 | + .. versionchanged:: 3.10.7 | ||
| 502 | + Added the ``int_max_str_digits`` attribute. | ||
| 503 | + | ||
| 500 | 504 | ||
| 501 | 505 | .. data:: float_info | |
| 502 | 506 | ||
@@ -677,6 +681,13 @@ always available. | |||
| 677 | 681 | ||
| 678 | 682 | .. versionadded:: 3.6 | |
| 679 | 683 | ||
| 684 | + .. function:: get_int_max_str_digits() | ||
| 685 | + | ||
| 686 | + Returns the current value for the :ref:`integer string conversion length | ||
| 687 | + limitation <int_max_str_digits>`. See also :func:`set_int_max_str_digits`. | ||
| 688 | + | ||
| 689 | + .. versionadded:: 3.10.7 | ||
| 690 | + | ||
| 680 | 691 | .. function:: getrefcount(object) | |
| 681 | 692 | ||
| 682 | 693 | Return the reference count of the *object*. The count returned is generally one | |
@@ -950,19 +961,31 @@ always available. | |||
| 950 | 961 | ||
| 951 | 962 | .. tabularcolumns:: |l|L| | |
| 952 | 963 | ||
| 953 | - +-------------------------+----------------------------------------------+ | ||
| 954 | - | Attribute | Explanation | | ||
| 955 | - +=========================+==============================================+ | ||
| 956 | - | :const:`bits_per_digit` | number of bits held in each digit. Python | | ||
| 957 | - | | integers are stored internally in base | | ||
| 958 | - | | ``2**int_info.bits_per_digit`` | | ||
| 959 | - +-------------------------+----------------------------------------------+ | ||
| 960 | - | :const:`sizeof_digit` | size in bytes of the C type used to | | ||
| 961 | - | | represent a digit | | ||
| 962 | - +-------------------------+----------------------------------------------+ | ||
| 964 | + +----------------------------------------+-----------------------------------------------+ | ||
| 965 | + | Attribute | Explanation | | ||
| 966 | + +========================================+===============================================+ | ||
| 967 | + | :const:`bits_per_digit` | number of bits held in each digit. Python | | ||
| 968 | + | | integers are stored internally in base | | ||
| 969 | + | | ``2**int_info.bits_per_digit`` | | ||
| 970 | + +----------------------------------------+-----------------------------------------------+ | ||
| 971 | + | :const:`sizeof_digit` | size in bytes of the C type used to | | ||
| 972 | + | | represent a digit | | ||
| 973 | + +----------------------------------------+-----------------------------------------------+ | ||
| 974 | + | :const:`default_max_str_digits` | default value for | | ||
| 975 | + | | :func:`sys.get_int_max_str_digits` when it | | ||
| 976 | + | | is not otherwise explicitly configured. | | ||
| 977 | + +----------------------------------------+-----------------------------------------------+ | ||
| 978 | + | :const:`str_digits_check_threshold` | minimum non-zero value for | | ||
| 979 | + | | :func:`sys.set_int_max_str_digits`, | | ||
| 980 | + | | :envvar:`PYTHONINTMAXSTRDIGITS`, or | | ||
| 981 | + | | :option:`-X int_max_str_digits <-X>`. | | ||
| 982 | + +----------------------------------------+-----------------------------------------------+ | ||
| 963 | 983 | ||
| 964 | 984 | .. versionadded:: 3.1 | |
| 965 | 985 | ||
| 986 | + .. versionchanged:: 3.10.7 | ||
| 987 | + Added ``default_max_str_digits`` and ``str_digits_check_threshold``. | ||
| 988 | + | ||
| 966 | 989 | ||
| 967 | 990 | .. data:: __interactivehook__ | |
| 968 | 991 | ||
@@ -1255,6 +1278,14 @@ always available. | |||
| 1255 | 1278 | ||
| 1256 | 1279 | .. availability:: Unix. | |
| 1257 | 1280 | ||
| 1281 | + .. function:: set_int_max_str_digits(n) | ||
| 1282 | + | ||
| 1283 | + Set the :ref:`integer string conversion length limitation | ||
| 1284 | + <int_max_str_digits>` used by this interpreter. See also | ||
| 1285 | + :func:`get_int_max_str_digits`. | ||
| 1286 | + | ||
| 1287 | + .. versionadded:: 3.10.7 | ||
| 1288 | + | ||
| 1258 | 1289 | .. function:: setprofile(profilefunc) | |
| 1259 | 1290 | ||
| 1260 | 1291 | .. index:: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -942,6 +942,16 @@ The :mod:`test.support` module defines the following functions: | |||
| 942 | 942 | .. versionadded:: 3.10 | |
| 943 | 943 | ||
| 944 | 944 | ||
| 945 | + .. function:: adjust_int_max_str_digits(max_digits) | ||
| 946 | + | ||
| 947 | + This function returns a context manager that will change the global | ||
| 948 | + :func:`sys.set_int_max_str_digits` setting for the duration of the | ||
| 949 | + context to allow execution of test code that needs a different limit | ||
| 950 | + on the number of digits when converting between an integer and string. | ||
| 951 | + | ||
| 952 | + .. versionadded:: 3.10.7 | ||
| 953 | + | ||
| 954 | + | ||
| 945 | 955 | The :mod:`test.support` module defines the following classes: | |
| 946 | 956 | ||
| 947 | 957 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments