3 �\�B � @ s� d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl m Z mZ ddlm Z ddlmZ dd lmZ d Zer�eddd ddg�Zne� Zedddg�Zeddg�Zeddg�Zeddg�ZddlZejddedd� ddlmZ efZefZe fZ!y6ddl"m"Z"m#Z# ddl$m$Z$ ee"e#f7 Zee$f7 ZW n e%k �rD Y nX ydd l&m'Z' ee'f7 ZW n e%k �rv Y nX dd!lm(Z(m)Z)m*Z* ee(f7 Zee)f7 Ze!e*f7 Z!ee+d"d#d$d%d&d'd(d)g�fee+d#d&d*d+d)g�fe!e+d,d-d.d/d0d'g�fee+d,d1d#d0d2d&d3d'd4g �ffZ,G d5d6� d6e�Z-d7d8� Z.d9d:� Z/d;d<� Z0d=d>� Z1d?d@� Z2G dAdB� dBe�Z3G dCdD� dDe3�Z4yddEl5m6Z6 W n e%k �r� dFdG� Z6Y nX G dHdI� dIe7�Z8G dJdK� dKe8e�Z9G dLdM� dMe8e�Z:dS )Naj jinja2.sandbox ~~~~~~~~~~~~~~ Adds a sandbox layer to Jinja as it was the default behavior in the old Jinja 1 releases. This sandbox is slightly different from Jinja 1 as the default behavior is easier to use. The behavior can be changed by subclassing the environment. :copyright: (c) 2017 by the Jinja Team. :license: BSD. � N)�Mapping)�Environment)� SecurityError)�string_types�PY2)�Markup)�EscapeFormatter)� Formatteri�� Zfunc_closureZ func_codeZ func_dictZ func_defaultsZfunc_globalsZim_classZim_funcZim_self�gi_frame�gi_code�cr_frame�cr_code�ag_code�ag_frame�ignorezthe sets modulezjinja2.sandbox)�module)�deque)�UserDict� DictMixin)�UserList)�Set)� MutableSet�MutableMapping�MutableSequence�add�clear�difference_update�discard�pop�remove�symmetric_difference_update�update�popitem� setdefault�append�reverse�insert�sort�extend� appendleft� extendleft�popleft�rotatec @ s0 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd S )�_MagicFormatMappingz�This class implements a dummy wrapper to fix a bug in the Python standard library for string formatting. See https://bugs.python.org/issue13598 for information about why this is necessary. c C s || _ || _d| _d S )Nr )�_args�_kwargs�_last_index)�self�args�kwargs� r4 �/usr/lib/python3.6/sandbox.py�__init__r s z_MagicFormatMapping.__init__c C sN |dkrD| j }| j d7 _ y | j| S tk r: Y nX t|�}| j| S )N� � )r0 r. �LookupError�strr/ )r1 �key�idxr4 r4 r5 �__getitem__w s z_MagicFormatMapping.__getitem__c C s t | j�S )N)�iterr/ )r1 r4 r4 r5 �__iter__� s z_MagicFormatMapping.__iter__c C s t | j�S )N)�lenr/ )r1 r4 r4 r5 �__len__� s z_MagicFormatMapping.__len__N)�__name__� __module__�__qualname__�__doc__r6 r= r? rA r4 r4 r4 r5 r- j s r- c C s: t | tjtjf� s| jdkr"d S | j}t |t�r6|S d S )N�format� format_map)rF rG )� isinstance�types� MethodType�BuiltinMethodTyperB �__self__r )�callable�objr4 r4 r5 �inspect_format_method� s rO c G s$ t | � }t|�tkr tdt ��|S )zWA range that can't generate ranges with a length of more than MAX_RANGE items. z+range too big, maximum size for range is %d)�ranger@ � MAX_RANGE� OverflowError)r2 �rngr4 r4 r5 � safe_range� s rT c C s d| _ | S )znMarks a function or method as unsafe. :: @unsafe def delete(self): pass T)�unsafe_callable)�fr4 r4 r5 �unsafe� s rW c C s� t | tj�r|tkr�dS n�t | tj�r<|tks6|tkr�dS n�t | t�rT|dkr�dS nzt | tjtjtj f�rndS t | tj �r�|tkr�dS nFttd�r�t | tj �r�|tkr�dS n"ttd�r�t | tj�r�|tkr�dS |jd�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�__)rH rI �FunctionType�UNSAFE_FUNCTION_ATTRIBUTESrJ �UNSAFE_METHOD_ATTRIBUTES�type�CodeType� TracebackType� FrameType� GeneratorType�UNSAFE_GENERATOR_ATTRIBUTES�hasattrrY �UNSAFE_COROUTINE_ATTRIBUTESrZ �!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES� startswith)rN �attrr4 r4 r5 �is_internal_attribute� s, rj c C s( x"t D ]\}}t| |�r||kS qW dS )a� This function checks if an attribute on a builtin mutable object (list, dict, set or deque) would modify it if called. It also supports the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and with Python 2.6 onwards the abstract base classes `MutableSet`, `MutableMapping`, and `MutableSequence`. >>> 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 (such as unicode) `False` is returned. >>> modifies_known_mutable("foo", "upper") False F)� _mutable_specrH )rN ri ZtypespecrW r4 r4 r5 �modifies_known_mutable� s rl c @ s� e Zd ZdZdZejejejej ej ejejd�Z ejejd�Ze� Ze� Zdd� Zdd� Zd d � Zdd� Zd d� Zdd� Zdd� Zdd� Zdd� Zddd�Zdd� ZdS )�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**�%)rn ro c C s dS )a� Called during template compilation with the name of a unary operator to check if it should be intercepted at runtime. If this method returns `True`, :meth:`call_unop` is excuted for this unary operator. The default implementation of :meth:`call_unop` will use the :attr:`unop_table` dictionary to perform the operator with the same logic as the builtin one. The following unary operators are interceptable: ``+`` and ``-`` Intercepted calls are always slower than the native operator call, so make sure only to intercept the ones you are interested in. .. versionadded:: 2.6 Fr4 )r1 �operatorr4 r4 r5 �intercept_unop+ s z#SandboxedEnvironment.intercept_unopc O s8 t j| f|�|� t| jd<