f9UdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddl mZd d lmZd d lmZd d lmZd d lmZej,dej.dej0fZdZeZej:eed<eZ ej:eed<ddhZ!ddhZ"ddhZ#ejHe%gdfejLe%gdfejNe%gdfee%gdffZ(ejRejRejTej0ejVefdfed<dej.dej0fd ejXefd!Z-d"e.d e/fd#Z0d$ed efd%Z1d&ej0d'ed e2fd(Z3d&ej0d'ed e2fd)Z4Gd*d+eZ5Gd,d-e5Z6Gd.d/e Z7Gd0d1e7e Z8y)2zA sandbox layer that ensures unsafe operations cannot be performed. Useful when the template itself comes from an untrusted source. N)abc)deque) Formatter)formatter_field_name_split)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_speccallablereturnct|tjtjfr|jdvry|j }t|t r|Sy)N)format format_map) isinstancetypes MethodTypeBuiltinMethodType__name____self__str)r,objs W/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/jinja2/sandbox.pyinspect_format_methodr:TsO 5##U%<%<=    ": :   C#s argsc`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 r9 safe_rangerCbs> ,C 3x) $+R )  Jr;fcd|_|S)zMarks a function or method as unsafe. .. code-block: python @unsafe def delete(self): pass T)unsafe_callable)rDs r9unsaferGqsA Hr;r8attrctt|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__)r1r2 FunctionTyperr3rtypeCodeType TracebackType FrameType GeneratorTypeUNSAFE_GENERATOR_ATTRIBUTEShasattrrKUNSAFE_COROUTINE_ATTRIBUTESrL!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES startswith)r8rHs r9is_internal_attributerY~s#u))* - - C)) * - -9Q1Q C  5= C%..%*=*=uO P C,, - . .  (ZU=P=P-Q . . , -* U % %3 4 4 ??4  r;cDtD]\}}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+r1)r8rHtypespecrGs r9modifies_known_mutabler\s,&*& c8 $6> !* r;c 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& d&ded ejNej$d fd ejeej$fd!ejPej"d ej$fd ef d"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)r_r`default_unop_tableintercepted_binopsintercepted_unopsr<kwargsr-Nct||i|t|jd<|jj |_|jj |_y)Nr>) super__init__rCglobalsrdcopy binop_tablere unop_table)selfr<rh __class__s r9rkzSandboxedEnvironment.__init__sP $)&) * W3388:11668r;r8rHvaluecB|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. _)rXrY)rpr8rHrrs r9is_safe_attributez&SandboxedEnvironment.is_safe_attributes#OOC(L,A#t,LMMr;c<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``. rFF alters_data)getattr)rpr8s r9is_safe_callablez%SandboxedEnvironment.is_safe_callable s* C*E 2 Xgc=RW6X  r;contextoperatorleftrightc.|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 )rn)rprzr{r|r}s r9 call_binopzSandboxedEnvironment.call_binops*t)$66r;argc,|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 )ro)rprzr{rs r9 call_unopzSandboxedEnvironment.call_unop"s)tx(--r;argumentcB ||S#ttf$rut|trb t|} t ||}|j |||r|cYS|j ||cYS#t$rYnwxYw#t$rYnwxYwYnwxYw|j||S)z(Subscribe an object from sandboxed code.r8name) TypeError LookupErrorr1r7rxruunsafe_undefinedAttributeError Exception undefined)rpr8rrHrrs r9getitemzSandboxedEnvironment.getitem+s Dx= ;' D(C( Dx=DD 'T 2 11#xG#(L#44S(CC * !  D~~#H~55sTB  A9 A*B B * A63B 5A66B 9 BB BB  B  attributec t||}|j|||r|S|j||S#t$r  ||cYS#tt f$rYnwxYwYnwxYw|j ||S)zSubscribe an object from sandboxed code and prefer the attribute. The attribute passed *must* be a bytestring. r)rxrurrrrr)rpr8rrrs r9rxzSandboxedEnvironment.getattrBs 9C+E%%c9e< ((i8 8  9~%{+    ~~#I~66s2 5 AAAAAAAAch|jd|dt|jd||tS)z1Return an undefined object for unsafe attributes.zaccess to attribute z of z object is unsafe.)rr8exc)rrOr5r )rpr8rs r9rz%SandboxedEnvironment.unsafe_undefinedSsE~~"9-0S ""%%7 9   r;s. format_funcc:t|trt||j}n t |}|C|j dk(r4t |dk7s|rtdt ||duzd|d}d}|j|||}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. )escapeNr0r z(format_map() takes exactly one argument z givenr) r1rSandboxedEscapeFormatterrSandboxedFormatterr5r?rvformatrO)rprr<rhr formatterrvs r9 format_stringz"SandboxedEnvironment.format_string]s a 0ahhGI*40I  "{';';|'K4yA~D V4%789A !WFD   q$ /tAwr{r;_SandboxedEnvironment__context_SandboxedEnvironment__objct|}||j||||S|j|st|d|j|g|i|S)z#Call an object from sandboxed code.z is not safely callable)r:rryr call)_SandboxedEnvironment__selfrrr<rhfmts r9rzSandboxedEnvironment.callzsh$E* ?''T65A A&&u-5)+B CD Dy~~e5d5f55r;N),r5 __module__ __qualname____doc__ sandboxedr{rsubmultruedivfloordivpowmodrdtDictr7CallableAny__annotations__posnegre frozensetrf FrozenSetrgrkboolruryr rrUnionr rrxrTupleOptionalrr __classcell__rqs@r9r^r^s I \\ \\ \\   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  ! "6.71557S7QWWQUUI=M5N7" AEE c i ;?  ggaeeSj!sAEEz"  ZZ 3: 67  :66uu6uu 6 %% 6  6r;r^c\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. r8rHrrr-cBt||||syt|| S)NF)rjrur\)rpr8rHrrrqs r9ruz/ImmutableSandboxedEnvironment.is_safe_attributes'w(dE:)#t444r;) r5rrrrrr7rrurrs@r9rrs6 5QUU5#5aee555r;rc eZdZdedej ddffd Zdedejej dejeej fdejej effdZ xZ S) renvrhr-Nc 2||_t|di|y)Nr)_envrjrk)rprrhrqs r9rkzSandboxedFormatter.__init__s  "6"r; field_namer<ct|\}}|j|||}|D]@\}}|r|jj||}%|jj ||}B||fSr)r get_valuerrxr) rprr<rhfirstrestr8is_attris r9 get_fieldzSandboxedFormatter.get_fieldsm1< tnnUD&1JGQii''Q/ii''Q/  Ezr;) r5rrr rrrkr7SequenceMappingrrrrs@r9rrsu#K#155#T#  %&ZZ%6 @A #quu*@U    r;rc eZdZy)rN)r5rrrr;r9rrsr;r)9rr{r2typingr collectionsrrstringr_stringr markupsaferr environmentr exceptionsr runtimer r TypeVarrrrr@setrSetr7rrrTrVrW MutableSetrMutableMappingMutableSequencer+rTyperrr:intr>rCrGrrYr\r^rrrrr;r9rs .&$% AIIcCJ/0  *-AEE#J.(+u!%%*, *95 *95&/ $;!    EF MN   1(I qwwqwwqvvaee}akk#.>>?DE(V AJJsAEEz$: qzz#  c e   a  A  "!quu"!C"!D"!JST2N6;N6b 5$8 5$ 1? r;