Skip to content

Commit afa6a05

Browse files
committed
annotationlib: add note on security to docs
1 parent f070f54 commit afa6a05

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Doc/library/annotationlib.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ and :func:`call_annotate_function`, as well as the
4545
:func:`call_evaluate_function` function for working with
4646
:term:`evaluate functions <evaluate function>`.
4747

48+
.. warning::
49+
50+
Most functionality in this module can execute arbitrary code; see
51+
:ref:`the security section <annotationlib-security>` for more information.
4852

4953
.. seealso::
5054

@@ -603,3 +607,22 @@ Below are a few examples of the behavior with unsupported expressions:
603607
>>> def ifexp(x: 1 if y else 0): ...
604608
>>> get_annotations(ifexp, format=Format.STRING)
605609
{'x': '1'}
610+
611+
.. _annotationlib-security:
612+
613+
Security
614+
--------
615+
616+
Much of the functionality in this module involves executing arbitrary code. For example,
617+
:func:`get_annotations` may call an arbitrary :term:`annotate function`, and
618+
:meth:`ForwardRef.evaluate` may call :func:`eval` on an arbitrary string. Code contained
619+
in an annotation might make arbitrary system calls, enter an infinite loop, or perform any
620+
other operation. This is also true for access to the :attr:`~object.__annotations__` attribute,
621+
and for various functions in the :mod:`typing` module that work with annotations, such as
622+
:func:`typing.get_type_hints`.
623+
624+
Any security issues arising from these facts also apply immediately after importing
625+
code that may contain untrusted annotations: importing code can always cause arbitrary operations
626+
to be performed. However, it is unsafe to accept strings or other input from an untrusted source and
627+
pass them to any of the APIs for introspecting annotations, for example by editing an
628+
``__annotations__`` dictionary or directly creating a :class:`ForwardRef` object.

Doc/library/inspect.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,11 @@ Classes and functions
12891289
This is an alias for :func:`annotationlib.get_annotations`; see the documentation
12901290
of that function for more information.
12911291

1292+
.. warning::
1293+
1294+
This function may execute arbitrary code contained in annotations.
1295+
See :ref:`annotationlib-security` for more information.
1296+
12921297
.. versionadded:: 3.10
12931298

12941299
.. versionchanged:: 3.14

Doc/library/typing.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,11 @@ Introspection helpers
33673367
See also :func:`annotationlib.get_annotations`, a lower-level function that
33683368
returns annotations more directly.
33693369

3370+
.. warning::
3371+
3372+
This function may execute arbitrary code contained in annotations.
3373+
See :ref:`annotationlib-security` for more information.
3374+
33703375
.. note::
33713376

33723377
If any forward references in the annotations of *obj* are not resolvable
@@ -3513,6 +3518,11 @@ Introspection helpers
35133518
See the documentation for :meth:`annotationlib.ForwardRef.evaluate` for
35143519
the meaning of the *owner*, *globals*, *locals*, *type_params*, and *format* parameters.
35153520

3521+
.. warning::
3522+
3523+
This function may execute arbitrary code contained in annotations.
3524+
See :ref:`annotationlib-security` for more information.
3525+
35163526
.. versionadded:: 3.14
35173527

35183528
.. data:: NoDefault

0 commit comments

Comments
 (0)