3
v^P                 @   sb  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 edddidZejdej dZ!dZ"ee#j$Z%G dd dZ&dd Z'dd Z(d+ddZ)e j* dd Z+dd  Z,G d!d" d"Z-G d#d$ d$e-Z.G d%d& d&Z/d'd( Z0d)d* Z1dS ),    N)Path)settings)Http404HttpResponseHttpResponseNotFound)ContextEngineTemplateDoesNotExist)pprint)resolve)timezone)MultiValueDict)	force_str)import_string)get_docs_versionTZi18nzdjango.templatetags.i18n)debug	librariesz#API|TOKEN|KEY|SECRET|PASS|SIGNATURE)flagsz********************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)selfZcallable_setting r   6/usr/lib/python3.6/site-packages/django/views/debug.py__init__)   s    zCallableSettingWrapper.__init__c             C   s
   t | jS )N)reprr   )r   r   r   r   __repr__,   s    zCallableSettingWrapper.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   "   s   r   c             C   sd   y6t j| rt}n"t|tr0dd |j D }n|}W n tk
rN   |}Y nX 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.
    c             S   s   i | ]\}}t |||qS r   )cleanse_setting).0kvr   r   r   
<dictcomp>:   s    z#cleanse_setting.<locals>.<dictcomp>)	HIDDEN_SETTINGSsearchCLEANSED_SUBSTITUTE
isinstancedictitems	TypeErrorcallabler   )keyvaluecleansedr   r   r   r    0   s    


r    c              C   s6   i } x,t tD ] }|j rt|tt|| |< qW | S )z{
    Return a dictionary of the settings module with values of sensitive
    settings replaced with stars (*********).
    )dirr   isupperr    getattr)settings_dictr"   r   r   r   get_safe_settingsH   s
    r4     c             C   sF   t | |||}| j r,|j }t||ddS |j }t||ddS 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/plain; charset=utf-8)statuscontent_typez	text/htmlN)ExceptionReporteris_ajaxget_traceback_textr   get_traceback_html)requestexc_type	exc_valuetbstatus_codereportertexthtmlr   r   r   technical_500_responseT   s    rD   c               C   s   t tj S )N)r   r   !DEFAULT_EXCEPTION_REPORTER_FILTERr   r   r   r   %get_default_exception_reporter_filterb   s    rF   c             C   s   t  }t| d|S )NZexception_reporter_filter)rF   r2   )r<   Zdefault_filterr   r   r   get_exception_reporter_filterh   s    rG   c               @   s    e Zd ZdZdd Zdd ZdS )ExceptionReporterFilterzv
    Base for all exception reporter filter classes. All overridable hooks
    contain lenient default behaviors.
    c             C   s   |d kri S |j S d S )N)POST)r   r<   r   r   r   get_post_parameterss   s    z+ExceptionReporterFilter.get_post_parametersc             C   s   t |jj S )N)listf_localsr*   )r   r<   tb_framer   r   r   get_traceback_frame_variablesy   s    z5ExceptionReporterFilter.get_traceback_frame_variablesN)r   r   r   r   rJ   rN   r   r   r   r   rH   m   s   rH   c               @   s8   e Zd Z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.
    c             C   s
   t jdk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   DEBUG)r   r<   r   r   r   	is_active   s    z%SafeExceptionReporterFilter.is_activec             C   sD   t |dg }| j|r@|r@|j }x|D ]}||kr(t||< q(W |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)r2   rQ   copyr'   )r   r<   ZmultivaluedictrR   paramr   r   r   get_cleansed_multivaluedict   s    
z7SafeExceptionReporterFilter.get_cleansed_multivaluedictc             C   s   |dkri S t |dg }| j|rv|rv|jj }|dkrRx|D ]}t||< q>W |S x|D ]}||krXt||< qXW |S n|jS dS )zk
        Replace the values of POST parameters marked as sensitive with
        stars (*********).
        NrR   __ALL__)r2   rQ   rI   rS   r'   )r   r<   rR   r/   r"   rT   r   r   r   rJ      s    


z/SafeExceptionReporterFilter.get_post_parametersc             C   sL   yt |t}W n( tk
r6 } zdj||S d }~X nX |rH| j||}|S )Nz{!r} while evaluating {!r})r(   r   	ExceptionformatrU   )r   r<   r.   Zis_multivalue_dicter   r   r   cleanse_special_types   s    z1SafeExceptionReporterFilter.cleanse_special_typesc       	      C   s  |j }d}x@|dk	rJ|jjdkrBd|jkrB|jd }t|dd}P |j }qW i }| j|r|r|dkrxR|jD ]}t||< qnW qxb|jj D ]*\}}||krt}n| j||}|||< qW n(x&|jj D ]\}}| j||||< qW |jjdkod|jkrt|d< t|d< |j S )ze
        Replace the values of variables marked as sensitive with
        stars (*********).
        NZsensitive_variables_wrappersensitive_variablesrV   Z	func_argsZfunc_kwargs)	f_backf_codeco_namerL   r2   rQ   r'   r*   rZ   )	r   r<   rM   current_framer[   wrapperr/   namer.   r   r   r   rN      s4    



z9SafeExceptionReporterFilter.get_traceback_frame_variablesN)	r   r   r   r   rQ   rU   rJ   rZ   rN   r   r   r   r   rO   }   s   	rO   c               @   sD   e Zd ZdZdddZdd Zdd Zd	d
 ZdddZdd Z	dS )r8   z0Organize and coordinate reporting on exceptions.Fc             C   sJ   || _ t| j | _|| _|| _|| _|| _t| jdd | _d| _	d | _
d S )Ntemplate_debugF)r<   rG   filterr=   r>   r?   is_emailr2   template_infotemplate_does_not_exist
postmortem)r   r<   r=   r>   r?   rd   r   r   r   r      s    zExceptionReporter.__init__c             C   sT  | j r*t| j tr*d| _| jjp&| jg| _| j }xt|D ]t\}}d|krg }xN|d D ]B\}}t	|}t
|dkrd|dd t
|f }|j||f qZW ||d< |||< q<W d}| j ot| j tr4t| jdd}t| jd	d}	|dk	o|	dk	r4| jjd
 }
t|
t|d dt|	d t
|
 ddd}ddlm} | jdkrRd}n,yt| jj}W n tk
r|   d}Y nX | j||| j|t| jj| jj t tjdtj dd  t!j" | tj#| j$| j| jd}| jdk	r| jj%j |d< | jj&j |d< | jj'j |d< | j r(| j j(|d< | jr>t| j|d< |rP|d |d< |S )z5Return a dictionary containing traceback information.Tvarsi   u   %s… <trimmed %d bytes string>r    startNend      asciireplace)errors)get_versionz%[unable to retrieve the current user]z%d.%d.%d   )rd   unicode_hintframesr<   user_strZfiltered_POST_itemsr   Zsys_executableZsys_version_infoserver_timeZdjango_version_infoZsys_pathre   rf   rg   Zrequest_GET_itemsZrequest_FILES_itemsZrequest_COOKIES_itemsZexception_typeZexception_valueZ	lastframe))r=   
issubclassr	   rf   r>   chainrg   get_traceback_frames	enumerater
   lenappendUnicodeErrorr2   argsr   maxmindjangorq   r<   struserrW   rd   rK   rc   rJ   r*   r4   sys
executableversion_infor   nowpathre   GETFILESCOOKIESr   )r   rt   iframeZ
frame_varsr"   r#   rs   rj   rk   Zunicode_strrq   ru   cr   r   r   get_traceback_data  sp    "


z$ExceptionReporter.get_traceback_datac             C   sH   t tddjdd}tj|j }W dQ R X t| j dd}|j|S )z1Return HTML version of debug 500 HTTP error page.	templatesztechnical_500.htmlzutf-8)encodingNF)use_l10n)	r   CURRENT_DIRopenDEBUG_ENGINEfrom_stringreadr   r   render)r   fhtr   r   r   r   r;   I  s    z$ExceptionReporter.get_traceback_htmlc             C   sJ   t tddjdd}tj|j }W dQ R X t| j ddd}|j|S )z7Return plain text version of debug 500 HTTP error page.r   ztechnical_500.txtzutf-8)r   NF)
autoescaper   )	r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r:   P  s    z$ExceptionReporter.get_traceback_textNc                s<  d}t |drBy|j|}W n tk
r0   Y nX |dk	rB|j }|dkry&t|d}|j j }W dQ R X W n tk
r   Y nX |dkrdg dg fS t|d trd x6|dd D ]&}t	j
d|}	|	r|	jdjd P qW  fd	d
|D }td|| }
|| }||
| }|| }||d | }|
|||fS )z
        Return context_lines before and after lineno from file.
        Return (pre_context_lineno, pre_context, context_line, post_context).
        N
get_sourcerbr   rn      s   coding[:=]\s*([-\w.]+)rl   c                s   g | ]}t | d qS )ro   )r   )r!   sline)r   r   r   
<listcomp>y  s    z:ExceptionReporter._get_lines_from_file.<locals>.<listcomp>)hasattrr   ImportError
splitlinesr   r   OSErrorr(   bytesrer&   groupdecoder   )r   filenamelinenocontext_linesloadermodule_namesourcefplinematchZlower_boundZupper_boundpre_contextcontext_linepost_contextr   )r   r   _get_lines_from_fileW  s<    
z&ExceptionReporter._get_lines_from_filec             C   sz  dd }g }| j }x$|r6|j| ||}||krP qW g }|sD|S |j }|sV| jn|j}x|d k	rt|jjjdr|j}q`|jj	j
}|jj	j}|jd }|jjjd}	|jjjdpd}
| j||d|	|
\}}}}|d kr|}g }d	}g }|j||t|d
d||
jdrdnd|||d | jj| j|jt|||||d d |j rl|rl|j }|j}q`|j}q`W |S )Nc             S   s    t | dd }t | dd }|p|S )N	__cause____context__)r2   )r>   ZexplicitZimplicitr   r   r   explicit_or_implicit_cause  s    zJExceptionReporter.get_traceback_frames.<locals>.explicit_or_implicit_causeZ__traceback_hide__rl   
__loader__r   ri      z<source code not available>r   Tzdjango.r   r   )Z	exc_causeZexc_cause_explicitr?   typer   functionr   rh   idr   r   r   pre_context_lineno)r>   r}   popr?   __traceback__rM   rL   gettb_nextr]   co_filenamer^   	tb_lineno	f_globalsr   r2   
startswithrc   rN   r<   r   )r   r   
exceptionsr>   rt   r?   r   r   r   r   r   r   r   r   r   r   r   r   rz     s`    





z&ExceptionReporter.get_traceback_frames)F)NN)
r   r   r   r   r   r   r;   r:   r   rz   r   r   r   r   r8      s   
G
-r8   c          )   C   s  y|j d d }W n( tttfk
r:   | jdd }Y nX y|j d d }W n tttfk
rl   g }Y nnX | s| jdkrt|dkrt|d dkrt|d d ddt|d d d	d  kod
kn  rt| S t| dt	j
}t|tjr|j}d}yt| j}W n tk
r"   Y n\X |j}t|dr>|j}n"t|dr`t|jdr`|jj}t|dr~|j}d||f }ttddjdd}	tj|	j }
W dQ R X t|t	j
||t|| t |d}t|
j|ddS )zBCreate a technical 404 error response. `exception` is the Http404.r   r   rl   Ntried/app_nameri   	namespaceadminurlconfr   	__class__r   z%s.%sr   ztechnical_404.htmlzutf-8)r   )r   Zroot_urlconfZrequest_pathurlpatternsreasonr<   r   Zraising_view_namez	text/html)r7   ) r   
IndexErrorr+   KeyError	path_infor   r|   r2   default_urlconfr   ROOT_URLCONFr(   types
ModuleTyper   r   r   funcr   r   r   r   r   r   r   r   r   r   r   r4   r   r   )r<   	exceptionZ	error_urlr   r   Zcallerresolver_matchobjmoduler   r   r   r   r   r   technical_404_response  sT    

8
r   c             C   sN   t tddjdd}tj|j }W dQ R X tdt i}t|j	|ddS )	z+Create an empty URLconf 404 error response.r   zdefault_urlconf.htmlzutf-8)r   Nversionz	text/html)r7   )
r   r   r   r   r   r   r   r   r   r   )r<   r   r   r   r   r   r   r     s
    r   )r5   )2	functoolsr   r   r   pathlibr   django.confr   django.httpr   r   r   django.templater   r   r	   Zdjango.template.defaultfiltersr
   django.urlsr   django.utilsr   django.utils.datastructuresr   django.utils.encodingr   django.utils.module_loadingr   django.utils.versionr   r   compile
IGNORECASEr%   r'   __file__parentr   r   r    r4   rD   	lru_cacherF   rG   rH   rO   r8   r   r   r   r   r   r   <module>   s@   

w X8