g:|UdZddlZddlZddlZddlmZddlmZ ddl m Z ddl m Z ddlmZddlmZdd lmZd d lmZd d lmZd d lmZd dlmZej4dej6dej8fZdZeZ ejBe"e#d<eZ$ejBe"e#d<ddhZ%ddhZ&ddhZ'ejPe)gdfejTe)gdfejVe)gdfe e)gdffZ,ejZejZej\ej^e"fdfe#d<d e0d!e1fd"Z2d#ed!efd$Z3d%ej8d&e"d!e4fd'Z5d%ej8d&e"d!e4fd(Z6Gd)d*eZ7Gd+d,e7Z8Gd-d.eZ9Gd/d0e9eZ:y#e $r ddlm Z YwxYw)1zA 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) UndefinedF.)boundiUNSAFE_FUNCTION_ATTRIBUTESUNSAFE_METHOD_ATTRIBUTESgi_framegi_codecr_framecr_codeag_codeag_frame)addcleardifference_updatediscardpopremovesymmetric_difference_updateupdate)rrpopitem setdefaultr )appendreverseinsertsortextendr) r# appendleftrr' extendleftrpopleftrrotate _mutable_specargsreturnc`t|}t|tkDrtdtd|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)r-rngs 0/usr/lib/python3/dist-packages/jinja2/sandbox.py safe_ranger6Ws> ,C 3x) $+R )  Jfcd|_|S)zMarks a function or method as unsafe. .. code-block: python @unsafe def delete(self): pass T)unsafe_callable)r8s r5unsafer;fsA Hr7objattrctt|tjr |tvryt|tjr|tvs|t vryt|t r|dk(ryt|tjtjtjfryt|tjr |tvrgyttdr#t|tjr |tvr4yttdr#t|tjr |t vry|j#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 FunctionTyper MethodTypertypeCodeType TracebackType FrameType GeneratorTypeUNSAFE_GENERATOR_ATTRIBUTEShasattrr@UNSAFE_COROUTINE_ATTRIBUTESrA!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES startswith)r<r=s r5is_internal_attributerQss#u))* - - C)) * - -9Q1Q C  5= C%..%*=*=uO P C,, - . .  (ZU=P=P-Q . . , -* U % %3 4 4 ??4  r7cDtD]\}}t||s||vcSy)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,rC)r<r=typespecr;s r5modifies_known_mutablerTs1&*"& c8 $6> !" r7c \eZdZUdZdZej ejejejejejejdZ ejeej"ej$ej$gej$ffed<ej(ej*dZejeej"ej$gej$ffed<eZej2eed<eZej2eed<d ej$d ej$d d ffd Zdej$dedej$d efdZdej$d efdZdededej$dej$d ej$f dZ dededej$d ej$fdZ!dej$dejDeej$fd ejDej$e#ffdZ$dej$ded ejDej$e#ffdZ%dej$ded e#fdZ&dej$d ejNej"deffd Z(d!ed"ej$d ej$d ej$d ej$f d#Z)xZ*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)rWrXdefault_unop_tableintercepted_binopsintercepted_unopsr-kwargsr.Nct||i|t|jd<|jj |_|jj |_y)Nr0) super__init__r6globalsr\copy binop_tabler] unop_table)selfr-r` __class__s r5rczSandboxedEnvironment.__init__sP $)&) * W3388:11668r7r<r=valuecB|jdxs t|| 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 r5is_safe_attributez&SandboxedEnvironment.is_safe_attributes#OOC(L,A#t,LMMr7c<t|ddxs t|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``. r:F alters_data)getattr)rhr<s r5is_safe_callablez%SandboxedEnvironment.is_safe_callables* C*E 2 Xgc=RW6X  r7contextoperatorleftrightc.|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 )rf)rhrrrsrtrus r5 call_binopzSandboxedEnvironment.call_binop s*t)$66r7argc,|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 )rg)rhrrrsrxs r5 call_unopzSandboxedEnvironment.call_unops)tx(--r7argumentcp ||S#ttf$rt|try t|} t ||}|j |}||cYS|j |||r|cYS|j||cYS#t$rYnwxYw#t$rYnwxYwYnwxYw|j||S)z(Subscribe an object from sandboxed code.r<name) TypeError LookupErrorrCstrrpwrap_str_formatrmunsafe_undefinedAttributeError Exception undefined)rhr<r{r=rjfmts r5getitemzSandboxedEnvironment.getitem s Dx= ;' D(C(Dx=D D 'T 2#2259?#&J11#xG#(L#44S(CC* !  D$~~#H~55sZB" B BB"B"-B" B  B" B  B" BB"BB"!B" attributec t||}|j|}||S|j|||r|S|j||S#t$r  ||cYS#t t f$rYnwxYwYnwxYw|j||S)zSubscribe an object from sandboxed code and prefer the attribute. The attribute passed *must* be a bytestring. r})rprrmrrrrr)rhr<rrjrs r5rpzSandboxedEnvironment.getattr:s 9C+E&&u-C %%c9e< ((i8 8  9~%{+    ~~#I~66s5 A A3AA3A-*A3,A--A32A3ch|jd|dt|jd||tS)z1Return an undefined object for unsafe attributes.zaccess to attribute z of z object is unsafe.)r~r<exc)rrG__name__r )rhr<rs r5rz%SandboxedEnvironment.unsafe_undefinedNsE~~"9-0S ""%%7 9   r7.ct|tjtjfr|jdvry|j tt syt|jdk(ttrt|j}n t|}|jdtjdtjdt ffd }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.cr:|r tdt|dk7rtdt|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_mapstr_typevformats r5wrapperz5SandboxedEnvironment.wrap_str_format..wrapperssa#$MNNt9>#CCI;gVaGFD&9: :r7)rCrDrFBuiltinMethodTyper__self__rrGr SandboxedEscapeFormatterrSandboxedFormatterrtAnyr)rhrj formatterrrrrrs @@@@r5rz$SandboxedEnvironment.wrap_str_formatXs  E$$e&=&=> ^^#; ;&#& $V ,6  ff %0fmmLI*40I## ;155 ;AEE ;c ; ;gu--r7_SandboxedEnvironment__context_SandboxedEnvironment__objcl|j|st|d|j|g|i|S)z#Call an object from sandboxed code.z is not safely callable)rqr call)_SandboxedEnvironment__selfrrr-r`s r5rzSandboxedEnvironment.calls@&&u-5)+B CD Dy~~e5d5f55r7)+r __module__ __qualname____doc__ sandboxedrsrsubmultruedivfloordivpowmodr\rDictrCallabler__annotations__posnegr] frozensetr^ FrozenSetr_rcboolrmrqr rwrzUnionrrrprOptionalrr __classcell__ris@r5rVrVsI \\ \\ \\   ll \\KQZZ0E%F FG\\ \\CsAJJw~$>>?&,5; C(6+4+q{{3'59aee9quu99 NQUUN#NaeeNN  AEE  d   7 7*- 756UU 7CD55 7  7..C.aee..6556$%GGCJ$76  ! "6471557S7QWWQUUI=M5N7( AEE c i *.QUU*.qzz!**S#X:N/O*.X 6 6uu 6uu 6 %% 6  6r7rVc\eZdZdZdej dedej deffd ZxZ 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. r<r=rjr.cBt||||syt|| S)NF)rbrmrT)rhr<r=rjris r5rmz/ImmutableSandboxedEnvironment.is_safe_attributes'w(dE:)#t444r7) rrrrrrrrrmrrs@r5rrs6 5QUU5#5aee555r7rc eZdZdedej ddffd Zdedejej dejeej fdejej effdZ xZ S) renvr`r.Nc 2||_t|di|y)Nr)_envrbrc)rhrr`ris r5rczSandboxedFormatter.__init__s  "6"r7 field_namer-ct|\}}|j|||}|D]@\}}|r|jj||}%|jj ||}B||fS)N)r get_valuerrpr) rhrr-r`firstrestr<is_attris r5 get_fieldzSandboxedFormatter.get_fieldsr1< tnnUD&1 0JGQii''Q/ii''Q/  0 Ezr7) rrrr rrrcrSequenceMappingTuplerrrs@r5rrsu#K#155#T#  %&ZZ%6 @A #quu*@U    r7rc eZdZy)rN)rrrrr7r5rrsr7r);rrsrDtypingr_stringr collectionsrcollections.abcr ImportError functoolsrstringr markupsaferr environmentr exceptionsr runtimer rTypeVarrrrr2setrSetrrrrLrNrO MutableSetrMutableMappingMutableSequencer,rTyperintr0r6r;rrQrTrVrrrrr7r5rs  ."%%&$% AIIcCJ/0  *-AEE#J.(+u!%%*, *95 *95&/ $;!    EF MN   1(B qwwqwwqvvq{{3'778#=>(V c e   a  A  "!quu"!C"!D"!JST2`6;`6F 5$8 5$ 1? Q "!!"sF,, F;:F;