zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
U
Þ=®dø8ã@s(UdZddlZddlZddlZddlmZddlmZddlm    Z    ddl
m Z ddl m Z ddl mZd    d
lmZd    d lmZd    d lmZd    d lmZejdejdejfdZdZeƒZejeed<eƒZ ejeed<ddhZ!ddhZ"ddhZ#ej$e%ddddddd d!gƒfej&e%ddd"d#d!gƒfej'e%d$d%d&d'd(dgƒfe    e%d$d)dd(d*dd+dd,g    ƒffZ(ej)ej)ej*ej+efdfed-<ejej,ed.œd/d0„Z-e.e/d1œd2d3„Z0eed4œd5d6„Z1ejee2d7œd8d9„Z3ejee2d7œd:d;„Z4Gd<d=„d=eƒZ5Gd>d?„d?e5ƒZ6Gd@dA„dAe ƒZ7GdBdC„dCe7e ƒZ8dS)Dz„A 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)Ú    UndefinedÚF.)Úboundi †ÚUNSAFE_FUNCTION_ATTRIBUTESÚUNSAFE_METHOD_ATTRIBUTESÚgi_frameÚgi_codeÚcr_frameÚcr_codeÚag_codeÚag_frameÚaddÚclearÚdifference_updateÚdiscardÚpopÚremoveÚsymmetric_difference_updateÚupdateÚpopitemÚ
setdefaultÚappendÚreverseÚinsertÚsortÚextendÚ
appendleftÚ
extendleftÚpopleftÚrotateÚ _mutable_spec)ÚcallableÚreturncCs8t|tjtjfƒr|jdkr dS|j}t|tƒr4|SdS)N)ÚformatÚ
format_map)Ú
isinstanceÚtypesÚ
MethodTypeÚBuiltinMethodTypeÚ__name__Ú__self__Ústr)r+Úobj©r7úEd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\jinja2/sandbox.pyÚinspect_format_methodSs
ÿþ
r9)Úargsr,cGs(t|Ž}t|ƒtkr$tdt›dƒ‚|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).)ÚrangeÚlenÚ    MAX_RANGEÚ OverflowError)r:Úrngr7r7r8Ú
safe_rangeas  
ÿr@)Úfr,cCs
d|_|S)zMarks a function or method as unsafe.
 
    .. code-block: python
 
        @unsafe
        def delete(self):
            pass
    T)Úunsafe_callable)rAr7r7r8Úunsafeps    rC)r6Úattrr,cCsØt|tjƒr|tkrÎdSn´t|tjƒr<|tks6|tkrÎdSn’t|tƒrT|dkrÎdSnzt|tjtjtj    fƒrndSt|tj
ƒrˆ|t krÎdSnFt tdƒr¬t|tj ƒr¬|tkrÎdSn"t tdƒrÎt|tjƒrÎ|tkrÎdS| d¡S)a´Test 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
    TÚmroÚ CoroutineTypeÚAsyncGeneratorTypeÚ__)r/r0Ú FunctionTyperr1rÚtypeÚCodeTypeÚ TracebackTypeÚ    FrameTypeÚ GeneratorTypeÚUNSAFE_GENERATOR_ATTRIBUTESÚhasattrrFÚUNSAFE_COROUTINE_ATTRIBUTESrGÚ!UNSAFE_ASYNC_GENERATOR_ATTRIBUTESÚ
startswith)r6rDr7r7r8Úis_internal_attribute}s0  
  ÿrTcCs(tD]\}}t||ƒr||kSqdS)aðThis 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/)r6rDZtypespecrCr7r7r8Úmodifies_known_mutable¢s 
rUcsèeZdZUdZdZejejejej    ej
ej ej dœZ ejeejejejgejffed<ejejdœZejeejejgejffed<eƒZejeed<eƒZejeed<ejejd    d
œ‡fd d „ Zejeejed œdd„Zejedœdd„Zeeejejejdœdd„Z eeejejdœdd„Z!ejej"eejfej"eje#fdœdd„Z$ejeej"eje#fdœdd„Z%ejee#dœdd „Z&d(eej'ejd!fejeejfej(ejed"œd#d$„Z)eejejejejd%œd&d'„Z*‡Z+S))ÚSandboxedEnvironmentaéThe 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)rWrXÚdefault_unop_tableÚintercepted_binopsÚintercepted_unopsN)r:Úkwargsr,cs4tƒj||Žt|jd<|j ¡|_|j ¡|_dS)Nr;)    ÚsuperÚ__init__r@Úglobalsr\ÚcopyÚ binop_tabler]Ú
unop_table)Úselfr:r`©Ú    __class__r7r8rbüs
 zSandboxedEnvironment.__init__©r6rDÚvaluer,cCs| d¡pt||ƒ 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.
        Ú_)rSrT©rgr6rDrkr7r7r8Úis_safe_attributesz&SandboxedEnvironment.is_safe_attribute)r6r,cCst|ddƒpt|ddƒ S)zêCheck 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)rgr6r7r7r8Úis_safe_callable sÿz%SandboxedEnvironment.is_safe_callable)ÚcontextÚoperatorÚleftÚrightr,cCs|j|||ƒS)z÷For 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)rqrrÚargr,cCs|j||ƒS)zõFor 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)r6Úargumentr,c Cs¨z
||WSttfk
r˜t|tƒr”z t|ƒ}Wntk
rHYnLXzt||ƒ}Wntk
rlYn(X| |||¡r„|YS| ||¡YSYnX|j    ||dS)z(Subscribe an object from sandboxed code.©r6Úname)
Ú    TypeErrorÚ LookupErrorr/r5Ú    ExceptionroÚAttributeErrorrnÚunsafe_undefinedÚ    undefined)rgr6rxrDrkr7r7r8Úgetitem*s 
 
 zSandboxedEnvironment.getitem)r6Ú    attributer,c Csxzt||ƒ}Wn<tk
rJz||WYSttfk
rDYnXYn X| |||¡r^|S| ||¡S|j||dS)z€Subscribe an object from sandboxed code and prefer the
        attribute.  The attribute passed *must* be a bytestring.
        ry)ror~r{r|rnrr€)rgr6r‚rkr7r7r8roAs  zSandboxedEnvironment.getattrcCs&|jd|›dt|ƒj›d||tdS)z1Return an undefined object for unsafe attributes.zaccess to attribute z of z object is unsafe.)rzr6Úexc)r€rJr3r
)rgr6r‚r7r7r8rRs ûz%SandboxedEnvironment.unsafe_undefined.)Úsr:r`Ú format_funcr,cCs†t|tƒrt||jd}nt|ƒ}|dk    rl|jdkrlt|ƒdksD|r`tdt|ƒ|dk    ›dƒ‚|d}d}| |||¡}t    |ƒ|ƒS)    z…If 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/rÚSandboxedEscapeFormatterr†ÚSandboxedFormatterr3r<r{ÚvformatrJ)rgr„r:r`r…Ú    formatterÚrvr7r7r8Ú 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)r9rŒrpr
Úcall)Z_SandboxedEnvironment__selfrrŽr:r`Úfmtr7r7r8rys 
zSandboxedEnvironment.call)N),r3Ú
__module__Ú __qualname__Ú__doc__Z    sandboxedrrrÚsubÚmulÚtruedivÚfloordivÚpowÚmodr\ÚtÚDictr5ÚCallableÚAnyÚ__annotations__ÚposÚnegr]Ú    frozensetr^Ú    FrozenSetr_rbÚboolrnrpr rurwÚUnionr rrorÚTupleÚOptionalrŒrÚ __classcell__r7r7rhr8rV»sZ
 
ù,þ(     þ 
þ  û
ú úrVcs0eZdZdZejeejedœ‡fdd„ Z‡Z    S)ÚImmutableSandboxedEnvironmentzÓWorks 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)rarnrUrmrhr7r8rn’sz/ImmutableSandboxedEnvironment.is_safe_attribute)
r3r‘r’r“ršrr5r£rnr§r7r7rhr8r¨Œsr¨cs\eZdZeejddœ‡fdd„ Zeejejej    eejfej
ejefdœdd„Z ‡Z S)rˆN)Úenvr`r,c s||_tƒjf|ŽdS©N)Ú_envrarb)rgr©r`rhr7r8rbšszSandboxedFormatter.__init__)Ú
field_namer:r`r,c    CsRt|ƒ\}}| |||¡}|D]*\}}|r:|j ||¡}q|j ||¡}q||fSrª)rÚ    get_valuer«ror)    rgr¬r:r`ÚfirstÚrestr6Úis_attrÚir7r7r8Ú    get_fieldžs  zSandboxedFormatter.get_field) r3r‘r’r    ršrrbr5ÚSequenceÚMappingr¥r²r§r7r7rhr8rˆ™s 
þrˆc@s eZdZdS)r‡N)r3r‘r’r7r7r7r8r‡«sr‡)9r“rrr0ÚtypingršÚ_stringrÚ collectionsrrÚstringrZ
markupsaferrÚ environmentr    Ú
exceptionsr
Zruntimer r ÚTypeVarrœrr r=ÚsetrÚSetr5ržrrOrQrRÚ
MutableSetr¡ÚMutableMappingÚMutableSequencer*r¥ÚTyper¢r¦r9Úintr;r@rCr£rTrUrVr¨rˆr‡r7r7r7r8Ú<module>s€          øÿþþþ÷ÿþè(+ %R