Skip to content

Commit 08830c7

Browse files
[3.9] gh-95588: Drop the safety claim from ast.literal_eval docs. (GH-95919) (GH-126729)
It was never really safe and this claim conflicts directly with the big warning in the docs about it being able to crash the interpreter. (cherry picked from commit 8baef8a) Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 6b8f442 commit 08830c7

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Doc/library/ast.rst

+16-8
Original file line numberDiff line numberDiff line change
@@ -1591,20 +1591,28 @@ and classes for traversing abstract syntax trees:
15911591

15921592
.. function:: literal_eval(node_or_string)
15931593

1594-
Safely evaluate an expression node or a string containing a Python literal or
1594+
Evaluate an expression node or a string containing only a Python literal or
15951595
container display. The string or node provided may only consist of the
15961596
following Python literal structures: strings, bytes, numbers, tuples, lists,
15971597
dicts, sets, booleans, and ``None``.
15981598

1599-
This can be used for safely evaluating strings containing Python values from
1600-
untrusted sources without the need to parse the values oneself. It is not
1601-
capable of evaluating arbitrarily complex expressions, for example involving
1602-
operators or indexing.
1599+
This can be used for evaluating strings containing Python values without the
1600+
need to parse the values oneself. It is not capable of evaluating
1601+
arbitrarily complex expressions, for example involving operators or
1602+
indexing.
1603+
1604+
This function had been documented as "safe" in the past without defining
1605+
what that meant. That was misleading. This is specifically designed not to
1606+
execute Python code, unlike the more general :func:`eval`. There is no
1607+
namespace, no name lookups, or ability to call out. But it is not free from
1608+
attack: A relatively small input can lead to memory exhaustion or to C stack
1609+
exhaustion, crashing the process. There is also the possibility for
1610+
excessive CPU consumption denial of service on some inputs. Calling it on
1611+
untrusted data is thus not recommended.
16031612

16041613
.. warning::
1605-
It is possible to crash the Python interpreter with a
1606-
sufficiently large/complex string due to stack depth limitations
1607-
in Python's AST compiler.
1614+
It is possible to crash the Python interpreter due to stack depth
1615+
limitations in Python's AST compiler.
16081616

16091617
.. versionchanged:: 3.2
16101618
Now allows bytes and set literals.

Lib/ast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ def parse(source, filename='<unknown>', mode='exec', *,
5353

5454
def literal_eval(node_or_string):
5555
"""
56-
Safely evaluate an expression node or a string containing a Python
56+
Evaluate an expression node or a string containing only a Python
5757
expression. The string or node provided may only consist of the following
5858
Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
5959
sets, booleans, and None.
60+
61+
Caution: A complex expression can overflow the C stack and cause a crash.
6062
"""
6163
if isinstance(node_or_string, str):
6264
node_or_string = parse(node_or_string, mode='eval')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Clarified the conflicting advice given in the :mod:`ast` documentation about
2+
:func:`ast.literal_eval` being "safe" for use on untrusted input while at
3+
the same time warning that it can crash the process. The latter statement is
4+
true and is deemed unfixable without a large amount of work unsuitable for a
5+
bugfix. So we keep the warning and no longer claim that ``literal_eval`` is
6+
safe.

0 commit comments

Comments
 (0)