Skip to content

Commit 6080c86

Browse files
miss-islingtonStanFromIrelandnedbat
authored
[3.14] Warn that overriding __builtins__ for eval is not a security mechanism (GH-145773) (GH-145808)
(cherry picked from commit eb9ae65) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> Co-authored-by: Ned Batchelder <ned@nedbatchelder.com>
1 parent 6d28aaf commit 6080c86

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Doc/library/functions.rst

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -597,17 +597,18 @@ are always available. They are listed here in alphabetical order.
597597
.. warning::
598598

599599
This function executes arbitrary code. Calling it with
600-
user-supplied input may lead to security vulnerabilities.
600+
untrusted user-supplied input will lead to security vulnerabilities.
601601

602602
The *source* argument is parsed and evaluated as a Python expression
603603
(technically speaking, a condition list) using the *globals* and *locals*
604604
mappings as global and local namespace. If the *globals* dictionary is
605605
present and does not contain a value for the key ``__builtins__``, a
606606
reference to the dictionary of the built-in module :mod:`builtins` is
607-
inserted under that key before *source* is parsed. That way you can
608-
control what builtins are available to the executed code by inserting your
609-
own ``__builtins__`` dictionary into *globals* before passing it to
610-
:func:`eval`. If the *locals* mapping is omitted it defaults to the
607+
inserted under that key before *source* is parsed.
608+
Overriding ``__builtins__`` can be used to restrict or change the available
609+
names, but this is **not** a security mechanism: the executed code can
610+
still access all builtins.
611+
If the *locals* mapping is omitted it defaults to the
611612
*globals* dictionary. If both mappings are omitted, the source is
612613
executed with the *globals* and *locals* in the environment where
613614
:func:`eval` is called. Note, *eval()* will only have access to the
@@ -658,7 +659,7 @@ are always available. They are listed here in alphabetical order.
658659
.. warning::
659660

660661
This function executes arbitrary code. Calling it with
661-
user-supplied input may lead to security vulnerabilities.
662+
untrusted user-supplied input will lead to security vulnerabilities.
662663

663664
This function supports dynamic execution of Python code. *source* must be
664665
either a string or a code object. If it is a string, the string is parsed as
@@ -689,9 +690,10 @@ are always available. They are listed here in alphabetical order.
689690

690691
If the *globals* dictionary does not contain a value for the key
691692
``__builtins__``, a reference to the dictionary of the built-in module
692-
:mod:`builtins` is inserted under that key. That way you can control what
693-
builtins are available to the executed code by inserting your own
694-
``__builtins__`` dictionary into *globals* before passing it to :func:`exec`.
693+
:mod:`builtins` is inserted under that key.
694+
Overriding ``__builtins__`` can be used to restrict or change the available
695+
names, but this is **not** a security mechanism: the executed code can
696+
still access all builtins.
695697

696698
The *closure* argument specifies a closure--a tuple of cellvars.
697699
It's only valid when the *object* is a code object containing

0 commit comments

Comments
 (0)