o
    þÞhFd  ã                   @   s~  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZmZmZ d dlmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlm Z  d dl!m"Z"m#Z# d dl$m%Z% edddid�Z&dd„ Z'G dd„ de(ƒZ)G dd„ dƒZ*d,dd„Z+e j,dd„ ƒZ-dd„ Z.d d!„ Z/d"d#„ Z0G d$d%„ d%ƒZ1G d&d'„ d'ƒZ2d(d)„ Z3d*d+„ Z4dS )-é    N)ÚPath)Úsettings)ÚHttp404ÚHttpResponseÚHttpResponseNotFound)ÚContextÚEngineÚTemplateDoesNotExist)Úpprint)Úresolve)Útimezone)ÚMultiValueDict)Ú	force_str)Úimport_string)Ú_lazy_re_compile)ÚPY311Úget_docs_version)Ú*coroutine_functions_to_sensitive_variablesTÚi18nzdjango.templatetags.i18n)ÚdebugÚ	librariesc                 C   s   t tƒjd |  S )z½
    Return a path to a builtin template.

    Avoid calling this function at the module level or in a class-definition
    because __file__ may not exist, e.g. in frozen environments.
    Ú	templates)r   Ú__file__Úparent)Úname© r   úR/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/views/debug.pyÚbuiltin_template_path!   s   r   c                   @   s   e Zd ZdS )ÚExceptionCycleWarningN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   +   s    r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚCallableSettingWrapperzÅ
    Object to wrap callable appearing in settings.
    * Not to call in the debug page (#21345).
    * Not to break the debug page if the callable forbidding to set attributes
      (#23070).
    c                 C   s
   || _ d S ©N)Ú_wrapped)ÚselfÚcallable_settingr   r   r   Ú__init__7   ó   
zCallableSettingWrapper.__init__c                 C   s
   t | jƒS r#   )Úreprr$   ©r%   r   r   r   Ú__repr__:   r(   zCallableSettingWrapper.__repr__N)r   r    r!   Ú__doc__r'   r+   r   r   r   r   r"   /   s    r"   éô  c                 C   sF   t | ƒ| |||ƒ}|  d¡r| ¡ }t||d�S | ¡ }t||dd�S )z‰
    Create a technical server error response. The last three arguments are
    the values returned from sys.exc_info() and friends.
    z	text/html)Ústatusztext/plain; charset=utf-8)r.   Úcontent_type)Úget_exception_reporter_classÚacceptsÚget_traceback_htmlr   Úget_traceback_text)ÚrequestÚexc_typeÚ	exc_valueÚtbÚstatus_codeÚreporterÚhtmlÚtextr   r   r   Útechnical_500_response>   s   
ÿr<   c                   C   s   t tjƒƒ S r#   )r   r   Ú!DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   Ú%get_default_exception_reporter_filterN   s   r>   c                 C   s   t ƒ }t| d|ƒS )NÚexception_reporter_filter)r>   Úgetattr)r4   Údefault_filterr   r   r   Úget_exception_reporter_filterT   s   rB   c                 C   s   t tjƒ}t| d|ƒS )NÚexception_reporter_class)r   r   ÚDEFAULT_EXCEPTION_REPORTERr@   )r4   Ú default_exception_reporter_classr   r   r   r0   Y   s   ÿÿr0   c                 C   sB   | j }|d u rzt| jƒ}W n	 ty   Y nw |d u rdS |jS )NÚ )Úresolver_matchr   Úpathr   Ú
_func_path)r4   rG   r   r   r   Ú
get_callerb   s   ÿrJ   c                   @   sj   e Zd ZdZdZedejd�Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚSafeExceptionReporterFilterz‹
    Use annotations made by the sensitive_post_parameters and
    sensitive_variables decorators to filter out sensitive information.
    z********************z4API|AUTH|TOKEN|KEY|SECRET|PASS|SIGNATURE|HTTP_COOKIE)Úflagsc                    s¾   |t jkrd}nzˆ j |¡}W n ty   d}Y nw |r"ˆ j}n3t|tƒr3‡ fdd„| ¡ D ƒ}n"t|t	ƒrB‡ fdd„|D ƒ}nt|t
ƒrSt
‡ fdd„|D ƒƒ}n|}t|ƒr]t|ƒ}|S )z¦
        Cleanse an individual setting key/value of sensitive content. If the
        value is a dictionary, recursively cleanse the keys in that dictionary.
        TFc                    ó   i | ]\}}|ˆ   ||¡“qS r   ©Úcleanse_setting©Ú.0ÚkÚvr*   r   r   Ú
<dictcomp>‡   ó    z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<dictcomp>c                    ó   g | ]}ˆ   d |¡‘qS ©rF   rN   ©rQ   rS   r*   r   r   Ú
<listcomp>‰   ó    z?SafeExceptionReporterFilter.cleanse_setting.<locals>.<listcomp>c                    rV   rW   rN   rX   r*   r   r   rY   ‹   rZ   )r   ÚSESSION_COOKIE_NAMEÚhidden_settingsÚsearchÚ	TypeErrorÚcleansed_substituteÚ
isinstanceÚdictÚitemsÚlistÚtupleÚcallabler"   )r%   ÚkeyÚvalueÚis_sensitiveÚcleansedr   r*   r   rO   w   s&   
ÿ


z+SafeExceptionReporterFilter.cleanse_settingc                 C   s4   i }t tƒD ]}| ¡ r|  |tt|ƒ¡||< q|S )z‡
        Return a dictionary of the settings module with values of sensitive
        settings replaced with stars (*********).
        )Údirr   ÚisupperrO   r@   )r%   Úsettings_dictrR   r   r   r   Úget_safe_settings”   s   €z-SafeExceptionReporterFilter.get_safe_settingsc                    ó&   t |dƒsi S ‡ fdd„|j ¡ D ƒS )zU
        Return a dictionary of request.META with sensitive values redacted.
        ÚMETAc                    rM   r   rN   rP   r*   r   r   rT   ¥   rU   zESafeExceptionReporterFilter.get_safe_request_meta.<locals>.<dictcomp>)Úhasattrro   rb   ©r%   r4   r   r*   r   Úget_safe_request_metaŸ   ó   
z1SafeExceptionReporterFilter.get_safe_request_metac                    rn   )zX
        Return a dictionary of request.COOKIES with sensitive values redacted.
        ÚCOOKIESc                    rM   r   rN   rP   r*   r   r   rT   ­   rU   z@SafeExceptionReporterFilter.get_safe_cookies.<locals>.<dictcomp>)rp   rt   rb   rq   r   r*   r   Úget_safe_cookies§   rs   z,SafeExceptionReporterFilter.get_safe_cookiesc                 C   s
   t jdu S )a  
        This filter is to add safety in production environments (i.e. DEBUG
        is False). If DEBUG is True then your site is not safe anyway.
        This hook is provided as a convenience to easily activate or
        deactivate the filter on a per request basis.
        F)r   ÚDEBUGrq   r   r   r   Ú	is_active¯   s   
z%SafeExceptionReporterFilter.is_activec                 C   sB   t |dg ƒ}|  |¡r|r| ¡ }|D ]}||v r| j||< q|S )zç
        Replace the keys in a MultiValueDict marked as sensitive with stars.
        This mitigates leaking sensitive POST parameters if something like
        request.POST['nonexistent_key'] throws an exception (#21098).
        Úsensitive_post_parameters)r@   rw   Úcopyr_   )r%   r4   Úmultivaluedictrx   Úparamr   r   r   Úget_cleansed_multivaluedict¸   s   
€z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc                 C   sv   |du ri S t |dg ƒ}|  |¡r8|r8|j ¡ }|dkr(|D ]}| j||< q|S |D ]}||v r5| j||< q*|S |jS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        Nrx   Ú__ALL__)r@   rw   ÚPOSTry   r_   )r%   r4   rx   ri   rR   r{   r   r   r   Úget_post_parametersÆ   s"   ÿ

€z/SafeExceptionReporterFilter.get_post_parametersc              
   C   sT   zt |tƒ}W n ty } zd ||¡W  Y d }~S d }~ww |r(|  ||¡}|S )Nz{!r} while evaluating {!r})r`   r   Ú	ExceptionÚformatr|   )r%   r4   rg   Úis_multivalue_dictÚer   r   r   Úcleanse_special_typesá   s   €ÿz1SafeExceptionReporterFilter.cleanse_special_typesc           
      C   sZ  d}|j jtj@ dkr$|j jdkr$t|j j› d|j j› �ƒ}t 	|d¡}|du rL|}|durL|j jdkrEd|j
v rE|j
d }t|ddƒ}n|j}|dus.i }|  |¡r‚|r‚|dkre|j
D ]}| j||< q\n/|j
 ¡ D ]\}}	||v rv| j}	n|  ||	¡}	|	||< qjn|j
 ¡ D ]\}}	|  ||	¡||< q‡|j jdkr©d|j
v r©| j|d< | j|d< | ¡ S )	ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        Nr   Úsensitive_variables_wrapperú:Úsensitive_variablesr}   Ú	func_argsÚfunc_kwargs)Úf_codeÚco_flagsÚinspectÚCO_COROUTINEÚco_nameÚhashÚco_filenameÚco_firstlinenor   ÚgetÚf_localsr@   Úf_backrw   r_   rb   r„   )
r%   r4   Útb_framer‡   rf   Úcurrent_frameÚwrapperri   r   rg   r   r   r   Úget_traceback_frame_variablesï   sL   ÿÿ

ö
ÿ
û	


z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)r   r    r!   r,   r_   r   ÚreÚIr\   rO   rm   rr   ru   rw   r|   r   r„   r˜   r   r   r   r   rK   l   s    ÿ	rK   c                   @   s~   e Zd ZdZedd„ ƒZedd„ ƒZddd„Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ Z	ddd„Zdd„ Zdd„ Zdd„ ZdS )ÚExceptionReporterz0Organize and coordinate reporting on exceptions.c                 C   ó   t dƒS )Nztechnical_500.html©r   r*   r   r   r   Úhtml_template_path9  ó   z$ExceptionReporter.html_template_pathc                 C   rœ   )Nztechnical_500.txtr�   r*   r   r   r   Útext_template_path=  rŸ   z$ExceptionReporter.text_template_pathFc                 C   sJ   || _ t| j ƒ| _|| _|| _|| _|| _t| jdd ƒ| _d| _	d | _
d S )NÚtemplate_debugF)r4   rB   Úfilterr5   r6   r7   Úis_emailr@   Útemplate_infoÚtemplate_does_not_existÚ
postmortem)r%   r4   r5   r6   r7   r£   r   r   r   r'   A  s   
zExceptionReporter.__init__c                 C   s    dj | jj| j ¡ | j ¡ d�S )z•
        Return an absolute URI from variables available in this request. Skip
        allowed hosts protection, so may return insecure URI.
        z{scheme}://{host}{path})ÚschemeÚhostrH   )r�   r4   r§   Ú_get_raw_hostÚget_full_pathr*   r   r   r   Ú_get_raw_insecure_uriM  s
   ýz'ExceptionReporter._get_raw_insecure_uric                 C   sÔ  | j rt| j tƒrd| _| jjp| jg| _|  ¡ }t|ƒD ]8\}}d|v rQg }|d D ]!\}}t	|ƒ}t
|ƒdkrEd|dd… t
|ƒf }| ||f¡ q+||d< |||< qd}| j r”t| j tƒr”t| jddƒ}t| jd	dƒ}	|dur”|	dur”| jjd
 }
t|
t|d dƒt|	d t
|
ƒƒ… ddd�}ddlm} | jdu r¢d}nzt| jjƒ}W n tyµ   d}Y nw i d| j“d|“d|“d| j“d| j | j¡“d| j | j¡ ¡ “d|“dt| j | j¡ ¡ ƒ“d| j ¡ “dt j!“ddt j"dd…  “dt# $¡ “d|ƒ “d t j%“d!| j&“d"| j“d#| j“}| jdu�r7| jj' ¡ |d$< | jj( ¡ |d%< |  )¡ |d&< t*| jƒ|d'< | j �rA| j j+|d(< | j�r_t| jƒ|d)< t| jd*dƒ }�r_d+d+ ,|¡ |d,< |�rh|d- |d.< |S )/z5Return a dictionary containing traceback information.TÚvarsi   u   %sâ€¦ <trimmed %d bytes string>r   rF   ÚstartNÚendé   é   ÚasciiÚreplace)Úerrors)Úget_versionz%[unable to retrieve the current user]r£   Úunicode_hintÚframesr4   Úrequest_metaÚrequest_COOKIES_itemsÚuser_strÚfiltered_POST_itemsr   Úsys_executableÚsys_version_infoz%d.%d.%dé   Úserver_timeÚdjango_version_infoÚsys_pathr¤   r¥   r¦   Úrequest_GET_itemsÚrequest_FILES_itemsÚrequest_insecure_uriÚraising_view_nameÚexception_typeÚexception_valueÚ	__notes__Ú
Úexception_noteséÿÿÿÿÚ	lastframe)-r5   Ú
issubclassr	   r¥   r6   Úchainr¦   Úget_traceback_framesÚ	enumerater
   ÚlenÚappendÚUnicodeErrorr@   Úargsr   ÚmaxÚminÚdjangor´   r4   ÚstrÚuserr€   r£   r¢   rr   ru   rb   rc   r   rm   ÚsysÚ
executableÚversion_infor   ÚnowrH   r¤   ÚGETÚFILESr«   rJ   r   Újoin)r%   r¶   ÚiÚframeÚ
frame_varsrR   rS   rµ   r­   r®   Úunicode_strr´   r¹   ÚcÚ	exc_notesr   r   r   Úget_traceback_dataX  s¨   
"ý
ýÿþýüûúùÿø
õôóòñðïîíz$ExceptionReporter.get_traceback_datac                 C   sV   | j jdd��}t | ¡ ¡}W d  ƒ n1 sw   Y  t|  ¡ dd�}| |¡S )z1Return HTML version of debug 500 HTTP error page.úutf-8©ÚencodingNF)Úuse_l10n)rž   ÚopenÚDEBUG_ENGINEÚfrom_stringÚreadr   ræ   Úrender©r%   ÚfhÚträ   r   r   r   r2   ¨  s
   ÿ
z$ExceptionReporter.get_traceback_htmlc                 C   sX   | j jdd��}t | ¡ ¡}W d  ƒ n1 sw   Y  t|  ¡ ddd�}| |¡S )z7Return plain text version of debug 500 HTTP error page.rç   rè   NF)Ú
autoescaperê   )r    rë   rì   rí   rî   r   ræ   rï   rð   r   r   r   r3   ¯  s
   ÿ
z$ExceptionReporter.get_traceback_textc                 C   s¢   d }t |dƒr z| |¡}W n	 ty   Y nw |d ur | ¡ }|d u rOz t|dƒ�}| ¡  ¡ }W d   ƒ W |S 1 s=w   Y  W |S  tyN   Y |S w |S )NÚ
get_sourceÚrb)rp   rô   ÚImportErrorÚ
splitlinesrë   rî   ÚOSError)r%   ÚfilenameÚloaderÚmodule_nameÚsourceÚfpr   r   r   Ú_get_source¶  s,   
ÿÿüþþzExceptionReporter._get_sourceNc                    sä   |   |||¡}|du rdg dg fS t|d tƒr=d‰ |dd… D ]}t d|¡}|r3|d  d¡‰  nq ‡ fdd„|D ƒ}td|| ƒ}	|| }
z||	|… }|| }||d |
… }W n tyk   dg dg f Y S w |	|||fS )	z—
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        Nr   r±   é   s   coding[:=]\s*([-\w.]+)r¯   c                    s   g | ]}t |ˆ d ƒ‘qS )r²   )r×   )rQ   Úslinerè   r   r   rY   Þ  rZ   z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)rþ   r`   Úbytesr™   r]   ÚdecoderÔ   Ú
IndexError)r%   rù   ÚlinenoÚcontext_linesrú   rû   rü   ÚlineÚmatchÚlower_boundÚupper_boundÚpre_contextÚcontext_lineÚpost_contextr   rè   r   Ú_get_lines_from_fileÇ  s,   þÿz&ExceptionReporter._get_lines_from_filec                 C   s4   t |dd ƒ}t |dd ƒ}t |dd ƒ}|p|rd S |S )NÚ	__cause__Ú__suppress_context__Ú__context__)r@   )r%   r6   ÚexplicitÚsuppress_contextÚimplicitr   r   r   Ú_get_explicit_or_implicit_causeë  s   z1ExceptionReporter._get_explicit_or_implicit_causec                 C   s¢   g }| j }|r | |¡ |  |¡}||v rt d| t¡ n|sg }|s&|S | ¡ }|s/| jn|j}	 | 	|  
||¡¡ z| ¡ }W n
 tyL   Y |S w |j}q3)NzHCycle in the exception chain detected: exception '%s' encountered again.)r6   rÑ   r  ÚwarningsÚwarnr   Úpopr7   Ú__traceback__ÚextendÚget_exception_traceback_framesr  )r%   Ú
exceptionsr6   r¶   r7   r   r   r   rÎ   ñ  s:   

ÿýöýúz&ExceptionReporter.get_traceback_framesc                 c   sÂ  � |   |¡}t|ddƒ}|d u r||d ddœV  |d urß|jj d¡r'|j}q|jjj}|jjj}|j	d }|jj
 d¡}|jj
 d¡pEd	}	|  ||d
||	¡\}
}}}|
d u r_|}
g }d}g }d	 }}tr¯tt |jj ¡ |jd d ¡ƒ\}}}}|r¯|r¯d||  }d|tt|d ƒƒ d  }d|› |› �}dd| t|ƒt| ¡ ƒ   }d|› |› �}||||	 d¡r¹dnd|||d | j | j|j¡t|ƒ||||
d ||dœV  |j}|d usd S d S )Nr  TrØ   )Ú	exc_causeÚexc_cause_explicitr7   ÚtypeÚ__traceback_hide__r¯   Ú
__loader__r   rF   é   z<source code not available>rÿ   ú^ú rÈ   é   zdjango.rÖ   )r  r  r7   r  rù   Úfunctionr  r¬   Úidr
  r  r  Úpre_context_linenoÚcolnoÚtb_area_colno)r  r@   r•   r“   r’   Útb_nextrŠ   r�   rŽ   Ú	tb_linenoÚ	f_globalsr  r   ÚnextÚ	itertoolsÚisliceÚco_positionsÚtb_lastirÐ   r×   ÚlstripÚ
startswithr¢   r˜   r4   r&  )r%   r6   r7   r  r  rù   r%  r  rú   rû   r'  r
  r  r  r(  r)  Ú_Ústart_columnÚ
end_columnÚ	underlineÚspacesÚtb_area_spacesr   r   r   r    s”   €
ü


ûûÿÿÿþÿÿï¿z0ExceptionReporter.get_exception_traceback_frames)F)NN)r   r    r!   r,   Úpropertyrž   r    r'   r«   ræ   r2   r3   rþ   r  r  rÎ   r  r   r   r   r   r›   6  s"    


P
ÿ$!r›   c           
      C   sŒ  z	|j d d }W n tttfy   | jdd… }Y nw z	|j d d }W n tttfy=   d}| jr9| jjnd}Y n;w d}|rt| jdkrxt|ƒdkrxt|d ƒdkrxt|d d d	d
ƒt|d d dd
ƒ  krrdkrxt	| ƒS  nt	| ƒS t| dt
jƒ}t|tjƒrˆ|j}tdƒjdd��}t | ¡ ¡}W d  ƒ n1 s¢w   Y  tƒ }t|t
j|||t|ƒ| | ¡ t| ƒdœ	ƒ}	t| |	¡ƒS )zBCreate a technical 404 error response. `exception` is the Http404.r   rH   r¯   NÚtriedTFú/Úapp_namerF   Ú	namespaceÚadminÚurlconfztechnical_404.htmlrç   rè   )	r@  Úroot_urlconfÚrequest_pathÚurlpatternsÚresolvedÚreasonr4   r   rÄ   )rÓ   r  r^   ÚKeyErrorÚ	path_inforG   r;  rÐ   r@   Údefault_urlconfr   ÚROOT_URLCONFr`   ÚtypesÚ
ModuleTyper   r   rë   rì   rí   rî   r>   r   r×   rm   rJ   r   rï   )
r4   Ú	exceptionÚ	error_urlr;  rD  r@  rñ   rò   Úreporter_filterrä   r   r   r   Útechnical_404_response`  sX   ÿþ
ÿþüÿ÷ÿrO  c                 C   sZ   t dƒjdd��}t | ¡ ¡}W d  ƒ n1 sw   Y  tdtƒ iƒ}t| |¡ƒS )z+Create an empty URLconf 404 error response.zdefault_urlconf.htmlrç   rè   NÚversion)	r   rë   rì   rí   rî   r   r   r   rï   )r4   rñ   rò   rä   r   r   r   rH  �  s   ÿÿÿrH  )r-   )5Ú	functoolsrŒ   r.  r™   rÙ   rJ  r  Úpathlibr   Údjango.confr   Údjango.httpr   r   r   Údjango.templater   r   r	   Údjango.template.defaultfiltersr
   Údjango.urlsr   Údjango.utilsr   Údjango.utils.datastructuresr   Údjango.utils.encodingr   Údjango.utils.module_loadingr   Údjango.utils.regex_helperr   Údjango.utils.versionr   r   Údjango.views.decorators.debugr   rì   r   ÚUserWarningr   r"   r<   Ú	lru_cacher>   rB   r0   rJ   rK   r›   rO  rH  r   r   r   r   Ú<module>   sP    þ


	
 K  ,/