� <��g�:��H�UdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZdd lmZd d lmZd d lmZd d lmZd dlmZejdejdejf���ZdZe��Zeje e!d<e��Z"eje e!d<ddhZ#ddhZ$ddhZ%ej&e'gd���fej(e'gd���fej)e'gd���fe e'gd���ffZ*ej+ej+ej,ejej-e fdfe!d<d e.d!e/fd"�Z0d#ed!efd$�Z1d%ejd&e d!e2fd'�Z3d%ejd&e d!e2fd(�Z4Gd)�d*e��Z5Gd+�d,e5��Z6Gd-�d.e ��Z7Gd/�d0e7e��Z8dS)1z�A sandbox layer that ensures unsafe operations cannot be performed. Useful when the template itself comes from an untrusted source. �N)�formatter_field_name_split)�abc)�deque)�update_wrapper)� Formatter)�EscapeFormatter)�Markup�)� Environment)� SecurityError)�Context)� Undefined�F.)�boundi���UNSAFE_FUNCTION_ATTRIBUTES�UNSAFE_METHOD_ATTRIBUTES�gi_frame�gi_code�cr_frame�cr_code�ag_code�ag_frame)�add�clear�difference_update�discard�pop�remove�symmetric_difference_update�update)rr�popitem� setdefaultr )�appendrr�reverse�insert�sort�extendr) r#� appendleftrr'� extendleftr�popleftr�rotate� _mutable_spec�args�returnc�x�t|�}t|��tkrtdt�d����|S)zWA range that can't generate ranges with a length of more than MAX_RANGE items. z@Range too big. The sandbox blocks ranges larger than MAX_RANGE (z).)�range�len� MAX_RANGE� OverflowError)r-�rngs �^/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/jinja2/sandbox.py� safe_ranger6WsN�� ��,�C� �3�x�x�)���� )�$� )� )� )� � � � �J��fc��d|_|S)z�Marks a function or method as unsafe. .. code-block: python @unsafe def delete(self): pass T)�unsafe_callable)r8s r5�unsafer;fs���A�� �Hr7�obj�attrc��t|tj��r |tvrdS�nt|tj��r|tvs |t vrdSn�t|t ��r |dkrdSn�t|tjtjtj f��rdSt|tj ��r |tvrdSnuttd��r&t|tj ��r |tvrdSn:ttd��r%t|tj��r |t vrdS|�d��S)a�Test if the attribute given is an internal python attribute. For example this function returns `True` for the `func_code` attribute of python objects. This is useful if the environment method :meth:`~SandboxedEnvironment.is_safe_attribute` is overridden. >>> from jinja2.sandbox import is_internal_attribute >>> is_internal_attribute(str, "mro") True >>> is_internal_attribute(str, "upper") False T�mro� CoroutineType�AsyncGeneratorType�__)� isinstance�types� FunctionTyper� MethodTyper�type�CodeType� TracebackType� FrameType� GeneratorType�UNSAFE_GENERATOR_ATTRIBUTES�hasattrr@�UNSAFE_COROUTINE_ATTRIBUTESrA�!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES� startswith)r<r=s r5�is_internal_attributerQssh���#�u�)�*�*�� �-� -� -��4� .� �C��)� *� *�� �-� -� -��9Q�1Q�1Q��4�2R� �C�� � �� �5�=�=��4� � �C�%�.�%�*=�u��O� P� P� ��t� �C��,� -� -� � �.� .� .��4� /� ��� (� (��Z��U�=P�-Q�-Q�� �.� .� .��4� /� ��,� -� -��*� �U� %�3�3�� �4� 4� 4��4� �?�?�4� � � r7c�L�tD]\}}t||��r||vcS�dS)a�This function checks if an attribute on a builtin mutable object (list, dict, set or deque) or the corresponding ABCs would modify it if called. >>> modifies_known_mutable({}, "clear") True >>> modifies_known_mutable({}, "keys") False >>> modifies_known_mutable([], "append") True >>> modifies_known_mutable([], "index") False If called with an unsupported object, ``False`` is returned. >>> modifies_known_mutable("foo", "upper") False F)r,rC)r<r=�typespecr;s r5�modifies_known_mutablerT�sD��&*�"�"���&� �c�8� $� $� "��6�>� !� !� !� "� �5r7c ����eZdZUdZdZejejejej ej ej ej d�Z ejeejejejgejffed<ejejd�Zejeejejgejffed<e��Zejeed<e��Zejeed<d ejd ejd d f�fd � Zdejdedejd efd�Zdejd efd�Zdededejdejd ejf d�Z dededejd ejfd�Z!dejdej"eejfd ej"eje#ffd�Z$dejded ej"eje#ffd�Z%dejded e#fd�Z&dejd ej'ejdeffd �Z(d!ed"ejd ejd ejd ejf d#�Z)�xZ*S)$�SandboxedEnvironmenta�The sandboxed environment. It works like the regular environment but tells the compiler to generate sandboxed code. Additionally subclasses of this environment may override the methods that tell the runtime what attributes or functions are safe to access. If the template tries to access insecure code a :exc:`SecurityError` is raised. However also other exceptions may occur during the rendering so the caller has to ensure that all exceptions are caught. T)�+�-�*�/z//z**�%�default_binop_table)rWrX�default_unop_table�intercepted_binops�intercepted_unopsr-�kwargsr.Nc����t��j|i|��t|jd<|j���|_|j���|_dS)Nr0) �super�__init__r6�globalsr\�copy� binop_tabler]� unop_table)�selfr-r`� __class__s �r5rczSandboxedEnvironment.__init__�s\��������$�)�&�)�)�)� *�� �W���3�8�8�:�:����1�6�6�8�8����r7r<r=�valuec�N�|�d��pt||�� S)aYThe sandboxed environment will call this method to check if the attribute of an object is safe to access. Per default all attributes starting with an underscore are considered private as well as the special attributes of internal python objects as returned by the :func:`is_internal_attribute` function. �_)rPrQ)rhr<r=rjs r5�is_safe_attributez&SandboxedEnvironment.is_safe_attribute�s)���O�O�C�(�(�L�,A�#�t�,L�,L�M�Mr7c�H�t|dd��pt|dd�� S)z�Check if an object is safely callable. By default callables are considered safe unless decorated with :func:`unsafe`. This also recognizes the Django convention of setting ``func.alters_data = True``. r:F� alters_data)�getattr)rhr<s r5�is_safe_callablez%SandboxedEnvironment.is_safe_callables0�� �C�*�E� 2� 2� X�g�c�=�RW�6X�6X� � r7�context�operator�left�rightc�0�|j|||��S)z�For intercepted binary operator calls (:meth:`intercepted_binops`) this function is executed instead of the builtin operator. This can be used to fine tune the behavior of certain operators. .. versionadded:: 2.6 )rf)rhrrrsrtrus r5� call_binopzSandboxedEnvironment.call_binop s��*�t���)�$��6�6�6r7�argc�.�|j||��S)z�For intercepted unary operator calls (:meth:`intercepted_unops`) this function is executed instead of the builtin operator. This can be used to fine tune the behavior of certain operators. .. versionadded:: 2.6 )rg)rhrrrsrxs r5� call_unopzSandboxedEnvironment.call_unops��)�t��x�(��-�-�-r7�argumentc�� ||S#ttf$r�t|t��r� t|��} t ||��}|�|��}|�|cYS|�|||��r|cYS|�||��cYS#t$rYnwxYw#t$rYnwxYwYnwxYw|� ||���S)z(Subscribe an object from sandboxed code.N�r<�name) � TypeError� LookupErrorrC�strrp�wrap_str_formatrm�unsafe_undefined�AttributeError� Exception� undefined)rhr<r{r=rj�fmts r5�getitemzSandboxedEnvironment.getitem s/�� D��x�=� ���;�'� D� D� D��(�C�(�(� D�D��x�=�=�D� D� '��T� 2� 2��#�2�2�5�9�9���?�#&�J�J�J��1�1�#�x��G�G�)�#(�L�L�L�#�4�4�S�(�C�C�C�C�C��*����������� !�����D�������  D����$�~�~�#�H�~�5�5�5s\� �&C�B/�B�C�,C�C� B,�)C�+B,�,C�/ B<�9C�;B<�<C�C� attributec�F� t||��}|�|��}|�|S|�|||��r|S|�||��S#t$r% ||cYS#t t f$rYnwxYwYnwxYw|�||���S)z�Subscribe an object from sandboxed code and prefer the attribute. The attribute passed *must* be a bytestring. Nr})rpr�rmr�r�rr�r�)rhr<r�rjr�s r5rpzSandboxedEnvironment.getattr:s��� 9��C��+�+�E��&�&�u�-�-�C���� ��%�%�c�9�e�<�<� �� ��(�(��i�8�8� 8��� � � � ��9�~�%�%�%���{�+� � � ��� ������  �����~�~�#�I�~�6�6�6s5�A� B �%A/�,B �/B�B �B�B �B c�p�|�d|�dt|��j�d�||t���S)z1Return an undefined object for unsafe attributes.zaccess to attribute z of z object is unsafe.)r~r<�exc)r�rG�__name__r )rhr<r�s r5r�z%SandboxedEnvironment.unsafe_undefinedNsR���~�~� 9�9� 9� 9��S� � �"� 9� 9� 9���� � � � r7.c�������t|tjtjf��r |jdvrdS|j�t�t ��sdSt����|jdk�t�t��rt|�j ���}nt|��}|j �dtjdtjdt f����fd� }t||��S) aIf the given value is a ``str.format`` or ``str.format_map`` method, return a new function than handles sandboxing. This is done at access rather than in :meth:`call`, so that calls made without ``call`` are also sandboxed. )�format� format_mapNr�)�escaper-r`r.c�����rN|rtd���t|��dkr tdt|���d����|d}d}���||����S)Nz'format_map() takes no keyword argumentsr z)format_map() takes exactly one argument (z given)r�)rr1)r-r`�f_self� is_format_map�str_type�vformats ����r5�wrapperz5SandboxedEnvironment.wrap_str_format.<locals>.wrapperss����� ��O�#�$M�N�N�N��t�9�9��>�>�#�V�C��I�I�V�V�V�����a������8�G�G�F�D�&�9�9�:�:� :r7)rCrDrF�BuiltinMethodTyper��__self__r�rGr �SandboxedEscapeFormatterr��SandboxedFormatterr��t�Anyr)rhrj� formatterr�r�r�r�r�s @@@@r5r�z$SandboxedEnvironment.wrap_str_formatXs ������ � �E�$�e�&=�>� � � � �^�#;� ;� ;��4�����&�#�&�&� ��4� $�V� � ����,�6� � �f�f� %� %� 1�0��f�m�L�L�L�I�I�*�4�0�0�I��#�� ;�1�5� ;�A�E� ;�c� ;� ;� ;� ;� ;� ;� ;� ;� ;��g�u�-�-�-r7�_SandboxedEnvironment__context�_SandboxedEnvironment__objc�r�|�|��st|�d����|j|g|�Ri|��S)z#Call an object from sandboxed code.z is not safely callable)rqr �call)�_SandboxedEnvironment__selfr�r�r-r`s r5r�zSandboxedEnvironment.call�sT���&�&�u�-�-� E��5� C� C� C�D�D� D��y�~�e�5�d�5�5�5�f�5�5�5r7)+r�� __module__� __qualname__�__doc__� sandboxedrsr�sub�mul�truediv�floordiv�pow�modr\r��Dictr��Callabler��__annotations__�pos�negr]� frozensetr^� FrozenSetr_rc�boolrmrqr rwrz�Unionrr�rpr��Optionalr�r�� __classcell__�ris@r5rVrV�sa�����������I� �\� �\� �\� � ����l� �\�K�K�����Q�Z��������0E�%F� F�G�����\� �\�C�C����s�A�J���w���~�$>�>�?����&,5�9�;�;��� �C�(�6�6�6�+4�)�+�+��q�{�3�'�5�5�5�9�a�e�9�q�u�9��9�9�9�9�9�9� N�Q�U�N�#�N�a�e�N��N�N�N�N�  �A�E�  �d�  �  �  �  � 7�� 7�*-� 7�56�U� 7�CD�5� 7� �� 7� 7� 7� 7�.��.�C�.�a�e�.���.�.�.�.�6��5�6�$%�G�C���J�$7�6� ���� �!� "�6�6�6�6�47�1�5�7�S�7�Q�W�Q�U�I�=M�5N�7�7�7�7�( �A�E� �c� �i� � � � �*.�Q�U�*.�q�z�!�*�S�#�X�:N�/O�*.�*.�*.�*.�X 6�� 6��u� 6��u� 6� �%� 6� �� 6� 6� 6� 6� 6� 6� 6� 6r7rVc�H��eZdZdZdejdedejdef�fd� Z�xZ S)�ImmutableSandboxedEnvironmentz�Works exactly like the regular `SandboxedEnvironment` but does not permit modifications on the builtin mutable objects `list`, `set`, and `dict` by using the :func:`modifies_known_mutable` function. r<r=rjr.c�p��t���|||��sdSt||�� S)NF)rbrmrT)rhr<r=rjris �r5rmz/ImmutableSandboxedEnvironment.is_safe_attribute�s9����w�w�(�(��d�E�:�:� ��5�)�#�t�4�4�4�4r7) r�r�r�r�r�r�r�r�rmr�r�s@r5r�r��sg��������� 5�Q�U�5�#�5�a�e�5��5�5�5�5�5�5�5�5�5�5r7r�c ���eZdZdedejddf�fd� Zdedejejdej eejfdej ejeffd�Z �xZ S) r��envr`r.Nc �H��||_t��jdi|��dS)Nr�)�_envrbrc)rhr�r`ris �r5rczSandboxedFormatter.__init__�s-����� ������"�"�6�"�"�"�"�"r7� field_namer-c���t|��\}}|�|||��}|D]>\}}|r|j�||��}�#|j�||��}�?||fS)N)r� get_valuer�rpr�) rhr�r-r`�first�restr<�is_attr�is r5� get_fieldzSandboxedFormatter.get_field�s���1��<�<� ��t��n�n�U�D�&�1�1��� 0� 0�J�G�Q�� 0��i�'�'��Q�/�/����i�'�'��Q�/�/����E�z�r7) r�r�r�r r�r�rcr��Sequence�Mapping�Tupler�r�r�s@r5r�r��s��������#�K�#�1�5�#�T�#�#�#�#�#�#� �� �%&�Z���%6� �@A� �#�q�u�*�@U� � ������ � � � � � � � � r7r�c��eZdZdS)r�N)r�r�r�r�r7r5r�r��s�������Dr7r�)9r�rsrD�typingr��_stringr� collectionsrr� functoolsr�stringr� markupsaferr � environmentr � exceptionsr �runtimer r�TypeVarr�r�rr2�setr�Setr�r�rrLrNrO� MutableSetr��MutableMapping�MutableSequencer,r��Typer��intr0r6r;r�rQrTrVr�r�r�r�r7r5�<module>r�s���������� � � � �����.�.�.�.�.�.�������������$�$�$�$�$�$�������&�&�&�&�&�&�������$�$�$�$�$�$�%�%�%�%�%�%������������� �A�I�c���C���J�/�0�0�0�� � �*-�����A�E�#�J�.�.�.�(+�s�u�u��!�%��*�,�,�,� *�9�5�� *�9�5��&/� �$;�!� ��� � � � � � �� ��� �E�E�E�F�F�� ��� � W� W� W� � �� �� � � � � � ��5*I� �q�w�q�w�q�v�a�e�}�a�k�#�.>�>�?��D�E�*�*�*�Z �c� �e� � � � �  �a�  �A�  �  �  �  �"!�q�u�"!�C�"!�D�"!�"!�"!�"!�J����S��T�����2`6�`6�`6�`6�`6�;�`6�`6�`6�F 5� 5� 5� 5� 5�$8� 5� 5� 5����������$ � � � � �1�?� � � � � r7
Memory