Skip to content

Commit eb9ae65

Browse files
Warn that overriding __builtins__ for eval is not a security mechanism (GH-145773)
Co-authored-by: Ned Batchelder <ned@nedbatchelder.com>
1 parent ece7121 commit eb9ae65

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

608608
This function executes arbitrary code. Calling it with
609-
user-supplied input may lead to security vulnerabilities.
609+
untrusted user-supplied input will lead to security vulnerabilities.
610610

611611
The *source* argument is parsed and evaluated as a Python expression
612612
(technically speaking, a condition list) using the *globals* and *locals*
613613
mappings as global and local namespace. If the *globals* dictionary is
614614
present and does not contain a value for the key ``__builtins__``, a
615615
reference to the dictionary of the built-in module :mod:`builtins` is
616-
inserted under that key before *source* is parsed. That way you can
617-
control what builtins are available to the executed code by inserting your
618-
own ``__builtins__`` dictionary into *globals* before passing it to
619-
:func:`eval`. If the *locals* mapping is omitted it defaults to the
616+
inserted under that key before *source* is parsed.
617+
Overriding ``__builtins__`` can be used to restrict or change the available
618+
names, but this is **not** a security mechanism: the executed code can
619+
still access all builtins.
620+
If the *locals* mapping is omitted it defaults to the
620621
*globals* dictionary. If both mappings are omitted, the source is
621622
executed with the *globals* and *locals* in the environment where
622623
:func:`eval` is called. Note, *eval()* will only have access to the
@@ -671,7 +672,7 @@ are always available. They are listed here in alphabetical order.
671672
.. warning::
672673

673674
This function executes arbitrary code. Calling it with
674-
user-supplied input may lead to security vulnerabilities.
675+
untrusted user-supplied input will lead to security vulnerabilities.
675676

676677
This function supports dynamic execution of Python code. *source* must be
677678
either a string or a code object. If it is a string, the string is parsed as
@@ -702,9 +703,10 @@ are always available. They are listed here in alphabetical order.
702703

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

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

0 commit comments

Comments
 (0)