B :`9@s(UdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddl mZd d lmZd d lmZd d lmZd d lmZejdejdejfdZdZeZejeed<eZ ejeed<ddhZ!ddhZ"ddhZ#ej$e%ddddddd d!gfej&e%ddd"d#d!gfej'e%d$d%d&d'd(dgfe e%d$d)dd(d*dd+dd,g ffZ(ej)ej)ej*ej+efdfed-<ejej,ed.d/d0Z-e.e/d1d2d3Z0eed4d5d6Z1ejee2d7d8d9Z3ejee2d7d:d;Z4Gdd?d?e5Z6Gd@dAdAe Z7GdBdCdCe7e Z8dS)DzA 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) UndefinedF.)ZboundiUNSAFE_FUNCTION_ATTRIBUTESUNSAFE_METHOD_ATTRIBUTESgi_framegi_codecr_framecr_codeag_codeag_frameaddcleardifference_updatediscardpopremovesymmetric_difference_updateupdatepopitem setdefaultappendreverseinsertsortextend appendleft extendleftpopleftrotate _mutable_spec)callablereturncCs8t|tjtjfr|jdkr dS|j}t|tr4|SdS)N)format format_map) isinstancetypes MethodTypeBuiltinMethodType__name____self__str)r*objr6?/opt/alt/python37/lib/python3.7/site-packages/jinja2/sandbox.pyinspect_format_methodSs  r8)argsr+cGs(t|}t|tkr$tdtd|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).)rangelen MAX_RANGE OverflowError)r9rngr6r6r7 safe_rangeas  r?)fr+cCs d|_|S)zMarks a function or method as unsafe. .. code-block: python @unsafe def delete(self): pass T)unsafe_callable)r@r6r6r7unsafeps rB)r5attrr+cCst|tjr|tkrdSnt|tjr<|tks6|tkrdSnt|trT|dkrdSnzt|tjtjtj frndSt|tj r|t krdSnFt tdrt|tj r|tkrdSn"t tdrt|tjr|tkrdS|dS)aTest 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 Tmro CoroutineTypeAsyncGeneratorType__)r.r/ FunctionTyperr0rtypeCodeType TracebackType FrameType GeneratorTypeUNSAFE_GENERATOR_ATTRIBUTEShasattrrEUNSAFE_COROUTINE_ATTRIBUTESrF!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES startswith)r5rCr6r6r7is_internal_attribute}s,      rScCs(x"tD]\}}t||r||kSqWdS)aThis 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.)r5rCZtypespecrBr6r6r7modifies_known_mutables  rTcseZdZUdZdZejejejej ej ej ej dZ ejeejejejgejffed<ejejdZejeejejgejffed<eZejeed<eZejeed<ejejd d fd d Zejeejed ddZejedddZeeejejejdddZ eeejejdddZ!ejej"eejfej"eje#fdddZ$ejeej"eje#fdddZ%ejee#ddd Z&d(eej'ejd!fejeejfej(ejed"d#d$Z)eejejejejd%d&d'Z*Z+S))SandboxedEnvironmentaThe 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)rVrWdefault_unop_tableintercepted_binopsintercepted_unopsN)r9kwargsr+cs4tj||t|jd<|j|_|j|_dS)Nr:) super__init__r?globalsr[copy binop_tabler\ unop_table)selfr9r_) __class__r6r7ras  zSandboxedEnvironment.__init__)r5rCvaluer+cCs|dpt|| 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)rfr5rCrhr6r6r7is_safe_attributesz&SandboxedEnvironment.is_safe_attribute)r5r+cCst|ddpt|dd S)zCheck 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``. rAFZ alters_data)getattr)rfr5r6r6r7is_safe_callable sz%SandboxedEnvironment.is_safe_callable)contextoperatorleftrightr+cCs|j|||S)zFor 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)rmrnargr+cCs|j||S)zFor 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)r5argumentr+c Csy||Sttfk rt|try t|}Wntk rFYnDXyt||}Wntk rjYn X||||r~|S|||SYnX|j ||dS)z(Subscribe an object from sandboxed code.)r5name) TypeError LookupErrorr.r4 ExceptionrkAttributeErrorrjunsafe_undefined undefined)rfr5rtrCrhr6r6r7getitem*s   zSandboxedEnvironment.getitem)r5 attributer+c Csryt||}Wn6tk rDy||Sttfk r>YnXYn X||||rX|S|||S|j||dS)zSubscribe an object from sandboxed code and prefer the attribute. The attribute passed *must* be a bytestring. )r5ru)rkryrvrwrjrzr{)rfr5r}rhr6r6r7rkAs  zSandboxedEnvironment.getattrcCs&|jd|dt|jd||tdS)z1Return an undefined object for unsafe attributes.zaccess to attribute z of z object is unsafe.)rur5exc)r{rIr2r )rfr5r}r6r6r7rzRs z%SandboxedEnvironment.unsafe_undefined.)sr9r_ format_funcr+cCst|trt||jd}nt|}|dk rl|jdkrlt|dksD|r`tdt||dk d|d}d}||||}t ||S) zIf 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.rSandboxedEscapeFormatterrSandboxedFormatterr2r;rvvformatrI)rfrr9r_rZ formatterrvr6r6r7 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)r8rrlr call)Z_SandboxedEnvironment__selfrrr9r_Zfmtr6r6r7rys  zSandboxedEnvironment.call)N),r2 __module__ __qualname____doc__Z sandboxedrnrsubmultruedivfloordivpowmodr[tZDictr4CallableAny__annotations__posnegr\ frozensetr] FrozenSetr^raboolrjrlr rqrsZUnionr r|rkrzTupleOptionalrr __classcell__r6r6)rgr7rUs< 0,     *rUcs0eZdZdZejeejedfdd ZZ S)ImmutableSandboxedEnvironmentzWorks 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)rgr6r7rjsz/ImmutableSandboxedEnvironment.is_safe_attribute) r2rrrrrr4rrjrr6r6)rgr7rsrcs\eZdZeejddfdd Zeejejej eejfej ejefdddZ Z S)rN)envr_r+c s||_tjf|dS)N)_envr`ra)rfrr_)rgr6r7raszSandboxedFormatter.__init__) field_namer9r_r+c CsVt|\}}||||}x2|D]*\}}|r<|j||}q |j||}q W||fS)N)r get_valuerrkr|) rfrr9r_firstrestr5is_attrir6r6r7 get_fields zSandboxedFormatter.get_field) r2rrr rrrar4SequenceMappingrrrr6r6)rgr7rsrc@s eZdZdS)rN)r2rrr6r6r6r7rsr)9rrnr/typingr_stringr collectionsrrstringrZ markupsaferrZ environmentr exceptionsr Zruntimer r ZTypeVarrrr r<setrSetr4rrrNrPrQ MutableSetrMutableMappingMutableSequencer)rZTyperrr8intr:r?rBrrSrTrUrrrr6r6r6r7sn          0 %R