@@ -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.
0 commit comments