Your IP : 3.146.176.112


Current Path : /opt/alt/python37/lib/python3.7/site-packages/jinja2/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/jinja2/__pycache__/sandbox.cpython-37.opt-1.pyc

B

�:�`9�@s(UdZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZddlm
Z
ddlmZd	d
lmZd	dlmZd	dlmZd	d
lmZejdejdejfd�ZdZe�Zejeed<e�Z ejeed<ddhZ!ddhZ"ddhZ#ej$e%ddddddd d!g�fej&e%ddd"d#d!g�fej'e%d$d%d&d'd(dg�fe	e%d$d)dd(d*dd+dd,g	�ffZ(ej)ej)ej*ej+efdfed-<ejej,ed.�d/d0�Z-e.e/d1�d2d3�Z0eed4�d5d6�Z1ejee2d7�d8d9�Z3ejee2d7�d:d;�Z4Gd<d=�d=e�Z5Gd>d?�d?e5�Z6Gd@dA�dAe�Z7GdBdC�dCe7e
�Z8dS)Dz�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)�	Formatter)�EscapeFormatter)�Markup�)�Environment)�
SecurityError)�Context)�	Undefined�F.)Zboundi���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�popitem�
setdefault�append�reverse�insert�sort�extend�
appendleft�
extendleft�popleft�rotate�
_mutable_spec)�callable�returncCs8t|tjtjf�r|jdkr dS|j}t|t�r4|SdS)N)�format�
format_map)�
isinstance�types�
MethodType�BuiltinMethodType�__name__�__self__�str)r*�obj�r6�?/opt/alt/python37/lib/python3.7/site-packages/jinja2/sandbox.py�inspect_format_methodSs

r8)�argsr+cGs(t|�}t|�tkr$tdt�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)r9�rngr6r6r7�
safe_rangeas
r?)�fr+cCs
d|_|S)z�Marks a function or method as unsafe.

    .. code-block: python

        @unsafe
        def delete(self):
            pass
    T)�unsafe_callable)r@r6r6r7�unsafeps	rB)r5�attrr+cCs�t|tj�r|tkr�dSn�t|tj�r<|tks6|tkr�dSn�t|t�rT|dkr�dSnzt|tjtjtj	f�rndSt|tj
�r�|tkr�dSnFttd�r�t|tj
�r�|tkr�dSn"ttd�r�t|tj�r�|tkr�dS|�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�__)r.r/�FunctionTyperr0r�type�CodeType�
TracebackType�	FrameType�
GeneratorType�UNSAFE_GENERATOR_ATTRIBUTES�hasattrrE�UNSAFE_COROUTINE_ATTRIBUTESrF�!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES�
startswith)r5rCr6r6r7�is_internal_attribute}s,

rScCs(x"tD]\}}t||�r||kSqWdS)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)r.)r5rCZtypespecrBr6r6r7�modifies_known_mutable�s
rTcs�eZdZUdZdZejejejej	ej
ejejd�Z
ejeejejejgejffed<ejejd�Zejeejejgejffed<e�Zejeed<e�Zejeed<ejejd	d
��fdd�Zejeejed
�dd�Zejed�dd�Zeeejejejd�dd�Z eeejejd�dd�Z!ejej"eejfej"eje#fd�dd�Z$ejeej"eje#fd�dd�Z%ejee#d�dd �Z&d(eej'ejd!fejeejfej(ejed"�d#d$�Z)eejejejejd%�d&d'�Z*�Z+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)rVrW�default_unop_table�intercepted_binops�intercepted_unopsN)r9�kwargsr+cs4t�j||�t|jd<|j��|_|j��|_dS)Nr:)	�super�__init__r?�globalsr[�copy�binop_tabler\�
unop_table)�selfr9r_)�	__class__r6r7ra�s
zSandboxedEnvironment.__init__)r5rC�valuer+cCs|�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.
        �_)rRrS)rfr5rCrhr6r6r7�is_safe_attributesz&SandboxedEnvironment.is_safe_attribute)r5r+cCst|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``.
        rAFZalters_data)�getattr)rfr5r6r6r7�is_safe_callablesz%SandboxedEnvironment.is_safe_callable)�context�operator�left�rightr+cCs|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
        )rd)rfrmrnrorpr6r6r7�
call_binops	zSandboxedEnvironment.call_binop)rmrn�argr+cCs|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
        )re)rfrmrnrrr6r6r7�	call_unop!szSandboxedEnvironment.call_unop)r5�argumentr+cCs�y||Sttfk
r�t|t�r�yt|�}Wntk
rFYnDXyt||�}Wntk
rjYn X|�|||�r~|S|�||�SYnX|j	||d�S)z(Subscribe an object from sandboxed code.)r5�name)
�	TypeError�LookupErrorr.r4�	Exceptionrk�AttributeErrorrj�unsafe_undefined�	undefined)rfr5rtrCrhr6r6r7�getitem*s 
zSandboxedEnvironment.getitem)r5�	attributer+cCsryt||�}Wn6tk
rDy||Sttfk
r>YnXYn X|�|||�rX|S|�||�S|j||d�S)z�Subscribe an object from sandboxed code and prefer the
        attribute.  The attribute passed *must* be a bytestring.
        )r5ru)rkryrvrwrjrzr{)rfr5r}rhr6r6r7rkAszSandboxedEnvironment.getattrcCs&|jd|�dt|�j�d�||td�S)z1Return an undefined object for unsafe attributes.zaccess to attribute z of z object is unsafe.)rur5�exc)r{rIr2r
)rfr5r}r6r6r7rzRs
z%SandboxedEnvironment.unsafe_undefined.)�sr9r_�format_funcr+cCs�t|t�rt||jd�}nt|�}|dk	rl|jdkrlt|�dksD|r`tdt|�|dk	�d���|d}d}|�|||�}t	|�|�S)	z�If a format call is detected, then this is routed through this
        method so that our safety sandbox can be used for it.
        )�escapeNr-rz(format_map() takes exactly one argument z givenrr6)
r.r�SandboxedEscapeFormatterr��SandboxedFormatterr2r;rv�vformatrI)rfrr9r_r�Z	formatter�rvr6r6r7�
format_string\s
z"SandboxedEnvironment.format_string)�_SandboxedEnvironment__context�_SandboxedEnvironment__objr9r_r+cOsJt|�}|dk	r |�||||�S|�|�s8t|�d���|j|f|�|�S)z#Call an object from sandboxed code.Nz is not safely callable)r8r�rlr
�call)Z_SandboxedEnvironment__selfr�r�r9r_Zfmtr6r6r7r�ys
zSandboxedEnvironment.call)N),r2�
__module__�__qualname__�__doc__Z	sandboxedrnr�sub�mul�truediv�floordiv�pow�modr[�tZDictr4�Callable�Any�__annotations__�pos�negr\�	frozensetr]�	FrozenSetr^ra�boolrjrlrrqrsZUnionrr|rkrz�Tuple�Optionalr�r��
__classcell__r6r6)rgr7rU�s<
	0,		
 *rUcs0eZdZdZejeejed��fdd�Z�Z	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.
    )r5rCrhr+cs t��|||�sdSt||�S)NF)r`rjrT)rfr5rCrh)rgr6r7rj�sz/ImmutableSandboxedEnvironment.is_safe_attribute)
r2r�r�r�r�r�r4r�rjr�r6r6)rgr7r��sr�cs\eZdZeejdd��fdd�Zeejejej	eejfej
ejefd�dd�Z�ZS)r�N)�envr_r+cs||_t�jf|�dS)N)�_envr`ra)rfr�r_)rgr6r7ra�szSandboxedFormatter.__init__)�
field_namer9r_r+c	CsVt|�\}}|�|||�}x2|D]*\}}|r<|j�||�}q |j�||�}q W||fS)N)r�	get_valuer�rkr|)	rfr�r9r_�first�restr5�is_attr�ir6r6r7�	get_field�szSandboxedFormatter.get_field)
r2r�r�r	r�r�rar4�Sequence�Mappingr�r�r�r6r6)rgr7r��sr�c@seZdZdS)r�N)r2r�r�r6r6r6r7r��sr�)9r�rnr/�typingr��_stringr�collectionsrr�stringrZ
markupsaferrZenvironmentr	�
exceptionsr
ZruntimerrZTypeVarr�r�r
r<�setr�Setr4r�rrNrPrQ�
MutableSetr��MutableMapping�MutableSequencer)r�ZTyper�r�r8�intr:r?rBr�rSrTrUr�r�r�r6r6r6r7�<module>sn0
%R


?>