U a9@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.)boundiUNSAFE_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+objr7OC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-8gbwd15f\jinja2\sandbox.pyinspect_format_methodSs  r9)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)r:rngr7r7r8 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)rAr7r7r8unsafeps rC)r6attrr,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/r0 FunctionTyperr1rtypeCodeType TracebackType FrameType GeneratorTypeUNSAFE_GENERATOR_ATTRIBUTEShasattrrFUNSAFE_COROUTINE_ATTRIBUTESrG!UNSAFE_ASYNC_GENERATOR_ATTRIBUTES startswith)r6rDr7r7r8is_internal_attribute}s0     rTcCs(tD]\}}t||r||kSqdS)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/)r6rDZtypespecrCr7r7r8modifies_known_mutables  rUcseZdZUdZdZejejejej 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)rWrXdefault_unop_tableintercepted_binopsintercepted_unopsN)r:kwargsr,cs4tj||t|jd<|j|_|j|_dS)Nr;) super__init__r@globalsr\copy binop_tabler] unop_table)selfr:r` __class__r7r8rbs  zSandboxedEnvironment.__init__r6rDvaluer,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. _)rSrTrgr6rDrkr7r7r8is_safe_attributesz&SandboxedEnvironment.is_safe_attribute)r6r,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``. rBFZ alters_data)getattr)rgr6r7r7r8is_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 )re)rgrqrrrsrtr7r7r8 call_binops zSandboxedEnvironment.call_binop)rqrrargr,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 )rf)rgrqrrrvr7r7r8 call_unop!szSandboxedEnvironment.call_unop)r6argumentr,c Csz ||WSttfk rt|trz t|}Wntk rHYnLXzt||}Wntk rlYn(X||||r|YS|||YSYnX|j ||dS)z(Subscribe an object from sandboxed code.r6name) TypeError LookupErrorr/r5 ExceptionroAttributeErrorrnunsafe_undefined undefined)rgr6rxrDrkr7r7r8getitem*s    zSandboxedEnvironment.getitem)r6 attributer,c Csxzt||}Wn<tk rJz||WYSttfk rDYnXYn X||||r^|S|||S|j||dS)zSubscribe an object from sandboxed code and prefer the attribute. The attribute passed *must* be a bytestring. ry)ror~r{r|rnrr)rgr6rrkr7r7r8roAs  zSandboxedEnvironment.getattrcCs&|jd|dt|jd||tdS)z1Return an undefined object for unsafe attributes.zaccess to attribute z of z object is unsafe.)rzr6exc)rrJr3r )rgr6rr7r7r8rRs z%SandboxedEnvironment.unsafe_undefined.)sr:r` 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 givenrr7) r/rSandboxedEscapeFormatterrSandboxedFormatterr3r<r{vformatrJ)rgrr:r`r formatterrvr7r7r8 format_string\s z"SandboxedEnvironment.format_string)_SandboxedEnvironment__context_SandboxedEnvironment__objr:r`r,cOsJt|}|dk r |||||S||s8t|d|j|f||S)z#Call an object from sandboxed code.Nz is not safely callable)r9rrpr call)Z_SandboxedEnvironment__selfrrr:r`fmtr7r7r8rys  zSandboxedEnvironment.call)N),r3 __module__ __qualname____doc__Z sandboxedrrrsubmultruedivfloordivpowmodr\tDictr5CallableAny__annotations__posnegr] frozensetr^ FrozenSetr_rbboolrnrpr rurwUnionr rrorTupleOptionalrr __classcell__r7r7rhr8rVsZ  ,(        rVcs0eZdZdZejeejedfdd 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. rjcs t|||sdSt|| S)NF)rarnrUrmrhr7r8rnsz/ImmutableSandboxedEnvironment.is_safe_attribute) r3rrrrrr5rrnrr7r7rhr8rsrcs\eZdZeejddfdd Zeejejej eejfej ejefdddZ Z S)rN)envr`r,c s||_tjf|dSN)_envrarb)rgrr`rhr7r8rbszSandboxedFormatter.__init__) field_namer:r`r,c CsRt|\}}||||}|D]*\}}|r:|j||}q|j||}q||fSr)r get_valuerror) rgrr:r`firstrestr6is_attrir7r7r8 get_fields  zSandboxedFormatter.get_field) r3rrr rrrbr5SequenceMappingrrrr7r7rhr8rs  rc@s eZdZdS)rN)r3rrr7r7r7r8rsr)9rrrr0typingr_stringr collectionsrrstringrZ markupsaferr environmentr exceptionsr Zruntimer r TypeVarrrr r=setrSetr5rrrOrQrR MutableSetrMutableMappingMutableSequencer*rTyperrr9intr;r@rCrrTrUrVrrrr7r7r7r8s          (+ %R