Skip to content

Commit 3b5c4a2

Browse files
miss-islingtonStanFromIrelandnedbat
authored
[3.13] Warn that overriding __builtins__ for eval is not a security mechanism (GH-145773) (GH-145807)
(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 eec26ea commit 3b5c4a2

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
@@ -591,17 +591,18 @@ are always available. They are listed here in alphabetical order.
591591
.. warning::
592592

593593
This function executes arbitrary code. Calling it with
594-
user-supplied input may lead to security vulnerabilities.
594+
untrusted user-supplied input will lead to security vulnerabilities.
595595

596596
The *source* argument is parsed and evaluated as a Python expression
597597
(technically speaking, a condition list) using the *globals* and *locals*
598598
mappings as global and local namespace. If the *globals* dictionary is
599599
present and does not contain a value for the key ``__builtins__``, a
600600
reference to the dictionary of the built-in module :mod:`builtins` is
601-
inserted under that key before *source* is parsed. That way you can
602-
control what builtins are available to the executed code by inserting your
603-
own ``__builtins__`` dictionary into *globals* before passing it to
604-
:func:`eval`. If the *locals* mapping is omitted it defaults to the
601+
inserted under that key before *source* is parsed.
602+
Overriding ``__builtins__`` can be used to restrict or change the available
603+
names, but this is **not** a security mechanism: the executed code can
604+
still access all builtins.
605+
If the *locals* mapping is omitted it defaults to the
605606
*globals* dictionary. If both mappings are omitted, the source is
606607
executed with the *globals* and *locals* in the environment where
607608
:func:`eval` is called. Note, *eval()* will only have access to the
@@ -652,7 +653,7 @@ are always available. They are listed here in alphabetical order.
652653
.. warning::
653654

654655
This function executes arbitrary code. Calling it with
655-
user-supplied input may lead to security vulnerabilities.
656+
untrusted user-supplied input will lead to security vulnerabilities.
656657

657658
This function supports dynamic execution of Python code. *source* must be
658659
either a string or a code object. If it is a string, the string is parsed as
@@ -683,9 +684,10 @@ are always available. They are listed here in alphabetical order.
683684

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

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

0 commit comments

Comments
 (0)