a &gD@sdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z dd l mZdd l mZdd l mZdd l mZdd lmZddlmZdZerhdZneZhdZddhZddhZddhZejddeedefZ e!fZ"e#fZ$z6ddl%m%Z%m&Z&ddl'm'Z'e"e%e&f7Z"e e'f7Z Wne(yFYn0zddl)m*Z*e e*f7Z Wne(yvYn0e ej+f7Z e"ej,f7Z"e$ej-f7Z$e e.gdfe"e.gdfe$e.gd fee.gd!ffZ/Gd"d#d#ej0Z1d$d%Z2d&d'Z3d(d)Z4d*d+Z5Gd,d-d-eZ6Gd.d/d/e6Z7zdd0l8m9Z9Wne(yVd1d2Z9Yn0Gd3d4d4e:Z;Gd5d6d6e;e ZZ func_closureZ func_codeZ func_dictZ func_defaultsZ func_globals>Zim_funcZim_classZim_selfgi_framegi_codecr_framecr_codeag_codeag_frameignorezthe sets module)module)UserDict DictMixin)UserList)Set)addcleardifference_updatediscardpopremovesymmetric_difference_updateupdate)rrpopitem setdefaultr!)appendreverseinsertsortextendr) r$ appendleftrr( extendleftrpopleftrrotatec@s0eZdZdZddZddZddZdd Zd S) _MagicFormatMappingzThis 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. cCs||_||_d|_dS)Nr)_args_kwargs _last_indexselfargskwargsr52/usr/lib/python3.9/site-packages/jinja2/sandbox.py__init__sz_MagicFormatMapping.__init__cCsN|dkrD|j}|jd7_z |j|WSty:Yn0t|}|j|S)Nr)r0r. LookupErrorstrr/)r2keyidxr5r5r6 __getitem__s  z_MagicFormatMapping.__getitem__cCs t|jSN)iterr/r2r5r5r6__iter__sz_MagicFormatMapping.__iter__cCs t|jSr>)lenr/r@r5r5r6__len__sz_MagicFormatMapping.__len__N)__name__ __module__ __qualname____doc__r7r=rArCr5r5r5r6r-~s  r-cGs$t|}t|tkr tdt|S)zWA range that can't generate ranges with a length of more than MAX_RANGE items. zDRange too big. The sandbox blocks ranges larger than MAX_RANGE (%d).)r rB MAX_RANGE OverflowError)r3rngr5r5r6 safe_ranges rKcCs d|_|S)znMarks a function or method as unsafe. :: @unsafe def delete(self): pass T)unsafe_callable)fr5r5r6unsafes rNcCst|tjr|tvrdSnt|tjr<|tvs6|tvrdSnt|trT|dkrdSnzt|tjtjtj frndSt|tj r|t vrdSnFt tdrt|tj r|tvrdSn"t tdrt|tjr|tvrdS|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__) isinstancetypes FunctionTypeUNSAFE_FUNCTION_ATTRIBUTES MethodTypeUNSAFE_METHOD_ATTRIBUTEStypeCodeType TracebackType FrameType GeneratorTypeUNSAFE_GENERATOR_ATTRIBUTEShasattrrPUNSAFE_COROUTINE_ATTRIBUTESrQ!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES startswith)objattrr5r5r6is_internal_attributes.     recCs(tD]\}}t||r||vSqdS)aThis 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_specrS)rcrdZtypespecrNr5r5r6modifies_known_mutables  rgc@seZdZdZdZejejejej ej ej ej dZ ejejdZeZeZddZddZd d Zd d Zd dZddZddZddZddZddZddZdS)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**%)rirjcCsdS)aCalled 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 executed 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 Fr5)r2operatorr5r5r6intercept_unop8sz#SandboxedEnvironment.intercept_unopcOs>tj|g|Ri|t|jd<|j|_|j|_dS)Nrange) r r7rKglobalsdefault_binop_tablecopy binop_tabledefault_unop_table unop_tabler1r5r5r6r7Is  zSandboxedEnvironment.__init__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. _)rbrer2rcrdvaluer5r5r6is_safe_attributeOsz&SandboxedEnvironment.is_safe_attributecCst|ddpt|dd S)aCheck if an object is safely callable. Per default a function is considered safe unless the `unsafe_callable` attribute exists and is True. Override this method to alter the behavior, but this won't affect the `unsafe` decorator from this module. rLFZ alters_data)getattr)r2rcr5r5r6is_safe_callableXsz%SandboxedEnvironment.is_safe_callablecCs|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 )rt)r2contextrnleftrightr5r5r6 call_binopbszSandboxedEnvironment.call_binopcCs|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 )rv)r2r}rnargr5r5r6 call_unopkszSandboxedEnvironment.call_unopc Csz ||WSttfyt|trz t|}WntyDYnd0zt||}WntyfYnB0||}|dur|YS| |||r|YS| ||YSYn0|j ||dS)z(Subscribe an object from sandboxed code.Nrcname) TypeErrorr9rSr r: Exceptionr{AttributeErrorwrap_str_formatrzunsafe_undefined undefined)r2rcargumentrdryfmtr5r5r6getitemts&      zSandboxedEnvironment.getitemc Cszt||}Wn8tyFz||WYSttfy@Yn0Yn60||}|dur^|S||||rp|S|||S|j||dS)zSubscribe an object from sandboxed code and prefer the attribute. The attribute passed *must* be a bytestring. Nr)r{rrr9rrzrr)r2rc attributeryrr5r5r6r{s    zSandboxedEnvironment.getattrcCs|jd||jjf||tdS)z1Return an undefined object for unsafe attributes.z.access to attribute %r of %r object is unsafe.)rrcexc)r __class__rDr )r2rcrr5r5r6rs z%SandboxedEnvironment.unsafe_undefinedcst|tjtjfr|jdvr dS|jtts4dSt|jdkttr`t |j d}nt |}|j fdd}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)escapecsLr<|rtdt|dkr0tdt|d|d}d}||S)Nz'format_map() takes no keyword argumentsrz)format_map() takes exactly one argument (z given)rr5)rrB)r3r4Zf_selfZ is_format_mapZstr_typevformatr5r6wrappers z5SandboxedEnvironment.wrap_str_format..wrapper)rSrTrWBuiltinMethodTyperD__self__r:rYrSandboxedEscapeFormatterrSandboxedFormatterrr)r2ry formatterrr5rr6rs"     z$SandboxedEnvironment.wrap_str_formatcOs0||std|f|j|g|Ri|S)z#Call an object from sandboxed code.z%r is not safely callable)r|r call)Z_SandboxedEnvironment__selfZ_SandboxedEnvironment__contextZ_SandboxedEnvironment__objr3r4r5r5r6rs zSandboxedEnvironment.callN)rDrErFrGZ sandboxedrnrsubmultruedivfloordivpowmodrrposnegru frozensetZintercepted_binopsZintercepted_unopsror7rzr|rrrr{rrrr5r5r5r6rhs0       $rhc@seZdZdZddZdS)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. cCs t||||sdSt|| S)NF)rhrzrgrxr5r5r6rzsz/ImmutableSandboxedEnvironment.is_safe_attributeN)rDrErFrGrzr5r5r5r6rsr)formatter_field_name_splitcCs|Sr>)Z_formatter_field_name_split) field_namer5r5r6rsrc@seZdZddZddZdS)SandboxedFormatterMixincCs ||_dSr>)_envr2envr5r5r6r7sz SandboxedFormatterMixin.__init__c CsRt|\}}||||}|D]*\}}|r:|j||}q|j||}q||fSr>)rZ get_valuerr{r) r2rr3r4firstrestrcZis_attrir5r5r6 get_fields  z!SandboxedFormatterMixin.get_fieldN)rDrErFr7rr5r5r5r6rsrc@seZdZddZdS)rcCst||t|dSr>)rr7rrr5r5r6r7s zSandboxedFormatter.__init__NrDrErFr7r5r5r5r6rsrc@seZdZddZdS)rcCst||t||dSr>)rr7r)r2rrr5r5r6r7s z!SandboxedEscapeFormatter.__init__Nrr5r5r5r6rsr)>rGrnrTwarnings collectionsr functoolsrstringrZ markupsaferrZ_compatrr r r Z environmentr exceptionsr rHrVsetrXr^r`rafilterwarningsDeprecationWarningrDZ_mutable_set_typesdictZ_mutable_mapping_typeslistZ_mutable_sequence_typesrrr ImportErrorZsetsr MutableSetMutableMappingMutableSequencerrfMappingr-rKrNrergrhrZ_stringrobjectrrrr5r5r5r6s                    + %_