3
 [ð]cG  ã               @   s<  d 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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 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! ddl"m#Z# ddl"m$Z$ dd„ Z d#Z%dd„ Z&da'dd„ Z(G dd„ de)ƒZ*dd„ Z+G dd „ d e)ƒZ,dS )$zŠ
    werkzeug.debug
    ~~~~~~~~~~~~~~

    WSGI application traceback debugger.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
é    N)Úchain)Úbasename)Újoiné   )Ú	text_type)Ú_log)Úparse_cookie)Úgen_salt)ÚBaseRequest)ÚBaseResponseé   )ÚConsole)Ú
debug_repr)Úget_current_traceback)Úrender_console_htmlc              O   s"   dd l }|jdtdd� t| |ŽS )Nr   z}'debug_repr' has moved to 'werkzeug.debug.repr.debug_repr' as of version 0.7. This old import will be removed in version 1.0.r   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningÚ_debug_repr)ÚargsÚkwargsr   © r   ú;/tmp/pip-build-w1as9q70/Werkzeug/werkzeug/debug/__init__.pyr   %   s    r   é<   é   é   c             C   s0   t | tƒr| jddƒ} tj| d ƒjƒ d d… S )Nzutf-8Úreplaces
   shittysalté   )Ú
isinstancer   ÚencodeÚhashlibÚmd5Ú	hexdigest)Úpinr   r   r   Úhash_pin6   s    
r%   c              C   s&   t } | d k	r| S dd„ }|ƒ  a } | S )Nc           3   S   sÊ  y t dƒ�} | jƒ }W d Q R X W n tk
r4   Y nX |jƒ jdƒd }|rP|S xFdD ]>}y"t |dƒ�} | jƒ jƒ S Q R X W qV tk
r’   wVY qVX qVW yPddlm}m} |d	d
dddg|d�jƒ d }t	j
d|ƒ}|d k	ræ|jdƒS W n ttfk
�r   Y nX d }ydd l}W n: tk
�rN   ydd l}W n tk
�rH   Y nX Y nX |d k	�rÆyT|j|jdd|j|jB ƒ�0}|j|dƒ\}	}
|
|jk�rž|	jdƒS |	S W d Q R X W n tk
�rÄ   Y nX d S )Nz/proc/self/cgroupz/docker/r   ú/etc/machine-idú/proc/sys/kernel/random/boot_idÚrbr   )ÚPopenÚPIPEZioregz-cZIOPlatformExpertDevicez-dÚ2)Ústdouts   "serial-number" = <([^>]+)r   zSOFTWARE\Microsoft\CryptographyZMachineGuidzutf-8)r&   r'   )ÚopenÚreadlineÚIOErrorÚstripÚ	partitionÚ
subprocessr)   r*   ÚcommunicateÚreÚsearchÚgroupÚOSErrorÚImportErrorÚwinregÚ_winregÚOpenKeyÚHKEY_LOCAL_MACHINEZKEY_READZKEY_WOW64_64KEYÚQueryValueExÚREG_SZr    ZWindowsError)ÚfÚvalueÚfilenamer)   r*   ÚdumpÚmatchÚwrZrkZmachineGuidZwrTyper   r   r   Ú	_generateE   s\    



z!get_machine_id.<locals>._generate)Ú_machine_id)ÚrvrE   r   r   r   Úget_machine_id?   s    A
rH   c               @   s   e Zd ZdZdd„ ZdS )Ú_ConsoleFramez]Helper class so that we can reuse the frame console code for the
    standalone console.
    c             C   s   t |ƒ| _d| _d S )Nr   )r   ÚconsoleÚid)ÚselfÚ	namespacer   r   r   Ú__init__�   s    
z_ConsoleFrame.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rN   r   r   r   r   rI   Š   s   rI   c                s®  t jjdƒ}d}d‰|dkr dS |dk	rJ|jddƒjƒ rJd|krF|}n|‰t| d| jjƒ}ytj	ƒ }W n t
tfk
r‚   d}Y nX tjj|ƒ}||t| d| jjƒt|ddƒg}ttjƒ ƒtƒ g}tjƒ }x6t||ƒD ](}	|	sàqÖt|	tƒrô|	jd	ƒ}	|j|	ƒ qÖW |jd
ƒ d|jƒ dd…  }
ˆdk�rN|jdƒ dt|jƒ dƒ dd… ‰|dk�r¦xLdD ]@‰ tˆƒˆ  dk�r^dj‡ ‡fdd„tdtˆƒˆ ƒD ƒƒ}P �q^W ˆ}||
fS )aQ  Given an application object this returns a semi-stable 9 digit pin
    code and a random key.  The hope is that this is stable between
    restarts to not make debugging particularly frustrating.  If the pin
    was forcefully disabled this returns `None`.

    Second item in the resulting tuple is the cookie name for remembering.
    ZWERKZEUG_DEBUG_PINNÚoffú-Ú rP   rO   Ú__file__zutf-8s
   cookiesaltZ__wzdé   s   pinsaltz%09dé   é	   é   é   é   r   c             3   s&   | ]}ˆ||ˆ  … j ˆ d ƒV  qdS )Ú0N)Úrjust)Ú.0Úx)Ú
group_sizeÚnumr   r   ú	<genexpr>Ý   s   z*get_pin_and_cookie_name.<locals>.<genexpr>)NN)rZ   r[   r\   )ÚosÚenvironÚgetr   ÚisdigitÚgetattrÚ	__class__rP   ÚgetpassÚgetuserr8   ÚKeyErrorÚsysÚmodulesrO   ÚstrÚuuidÚgetnoderH   r!   r"   r   r   r   r    Úupdater#   ÚintÚlenr   Úrange)Úappr$   rG   ÚmodnameÚusernameÚmodZprobably_public_bitsZprivate_bitsÚhÚbitZcookie_namer   )ra   rb   r   Úget_pin_and_cookie_name”   sR    







r|   c               @   s”   e Zd ZdZd#dd„Zd	d
„ Zdd„ ZeeeƒZ[[e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d „ Zd!d"„ ZdS )$ÚDebuggedApplicationaQ  Enables debugging support for a given application::

        from werkzeug.debug import DebuggedApplication
        from myapp import app
        app = DebuggedApplication(app, evalex=True)

    The `evalex` keyword argument allows evaluating expressions in a
    traceback's frame context.

    .. versionadded:: 0.9
       The `lodgeit_url` parameter was deprecated.

    :param app: the WSGI application to run debugged.
    :param evalex: enable exception evaluation feature (interactive
                   debugging).  This requires a non-forking server.
    :param request_key: The key that points to the request object in ths
                        environment.  This parameter is ignored in current
                        versions.
    :param console_path: the URL for a general purpose console.
    :param console_init_func: the function that is executed before starting
                              the general purpose console.  The return value
                              is used as initial namespace.
    :param show_hidden_frames: by default hidden traceback frames are skipped.
                               You can show them by setting this parameter
                               to `True`.
    :param pin_security: can be used to disable the pin based security system.
    :param pin_logging: enables the logging of the pin system.
    Fúwerkzeug.requestú/consoleNTc
             C   sÄ   |d k	r"ddl m}
 |
dtdd� |s*d }|| _|| _i | _i | _|| _|| _|| _	|| _
tdƒ| _d| _|	| _|rºtjjdƒdkrÀ|	rÀtd	d
ƒ | jd kr¨td	dƒ qÀtdd| j ƒ nd | _d S )Nr   )r   z…'lodgeit_url' is no longer used as of version 0.9 and will be removed in version 1.0. Werkzeug uses https://gist.github.com/ instead.r   )r   rW   ZWERKZEUG_RUN_MAINÚtrueÚwarningz * Debugger is active!z- * Debugger PIN disabled. DEBUGGER UNSECURED!Úinfoz * Debugger PIN: %s)r   r   r   rv   ÚevalexÚframesÚ
tracebacksÚrequest_keyÚconsole_pathÚconsole_init_funcÚshow_hidden_framesr	   ÚsecretÚ_failed_pin_authÚpin_loggingrd   re   rf   r   r$   )rL   rv   rƒ   r†   r‡   rˆ   r‰   Zlodgeit_urlZpin_securityrŒ   r   r   r   r   rN     s4    


zDebuggedApplication.__init__c             C   s"   t | dƒst| jƒ\| _| _| jS )NÚ_pin)Úhasattrr|   rv   r�   Ú_pin_cookie)rL   r   r   r   Ú_get_pin4  s    
zDebuggedApplication._get_pinc             C   s
   || _ d S )N)r�   )rL   r@   r   r   r   Ú_set_pin9  s    zDebuggedApplication._set_pinc             C   s"   t | dƒst| jƒ\| _| _| jS )zThe name of the pin cookie.r�   )rŽ   r|   rv   r�   r�   )rL   r   r   r   Úpin_cookie_name?  s    
z#DebuggedApplication.pin_cookie_namec             c   s  d}y6| j ||ƒ}x|D ]
}|V  qW t|dƒr8|jƒ  W nÐ tk
�r
   t|dƒr^|jƒ  td| jdd�}x|jD ]}|| j|j< qvW || j|j< y|dddgƒ W n" tk
rÊ   |d j	dƒ Y n.X t
| j|ƒƒ}|j| j|| jd�jddƒV  |j|d ƒ Y nX dS )z6Run the application and conserve the traceback frames.NÚcloser   T)Úskipr‰   Zignore_system_exceptionsz500 INTERNAL SERVER ERRORúContent-Typeútext/html; charset=utf-8úX-XSS-Protectionr]   zwsgi.errorszpDebugging middleware caught exception in streamed response at a point where response headers were already sent.
)rƒ   Úevalex_trustedrŠ   zutf-8r   )r•   r–   )r—   r]   )rv   rŽ   r“   Ú	Exceptionr   r‰   r„   rK   r…   ÚwriteÚboolÚcheck_pin_trustZrender_fullrƒ   rŠ   r    Úlog)rL   re   Ústart_responseZapp_iterÚitemÚ	tracebackÚframeÚ
is_trustedr   r   r   Údebug_applicationF  s<    




z%DebuggedApplication.debug_applicationc             C   s   t |jj|ƒdd�S )zExecute a command in a console.z	text/html)Úmimetype)ÚResponserJ   Úeval)rL   ÚrequestÚcommandr¡   r   r   r   Úexecute_commandw  s    z#DebuggedApplication.execute_commandc             C   sh   d| j krB| jdkri }nt| jƒ ƒ}|jd| jƒ t|ƒ| j d< t| j|jƒƒ}t	t
| j|d�dd�S )zDisplay a standalone shell.r   Nrv   )rŠ   r˜   z	text/html)r¤   )r„   rˆ   ÚdictÚ
setdefaultrv   rI   r›   rœ   re   r¥   r   rŠ   )rL   r§   Únsr¢   r   r   r   Údisplay_console{  s    

z#DebuggedApplication.display_consolec             C   s   |j ƒ }ttj|ƒdd�S )z/Paste the traceback and return a JSON response.zapplication/json)r¤   )Úpaster¥   ÚjsonÚdumps)rL   r§   r    rG   r   r   r   Úpaste_tracebackŠ  s    z#DebuggedApplication.paste_tracebackc             C   sj   t dt|ƒƒ}ytjt|ƒ}W n tk
r6   d}Y nX |dk	r^tj|ƒd pPd}t||d�S tddd�S )	z0Return a static resource from the shared folder.ZsharedNr   zapplication/octet-stream)r¤   z	Not Foundi”  )Ústatus)	r   r   ÚpkgutilÚget_dataÚ__package__r7   Ú	mimetypesÚ
guess_typer¥   )rL   r§   rA   Údatar¤   r   r   r   Úget_resource�  s    
z DebuggedApplication.get_resourcec             C   sr   | j dkrdS t|ƒj| jƒ}| s,d|kr0dS |jddƒ\}}|jƒ sLdS |t| j ƒkr^dS tjƒ t t	|ƒk S )a!  Checks if the request passed the pin test.  This returns `True` if the
        request is trusted on a pin/cookie basis and returns `False` if not.
        Additionally if the cookie's stored pin hash is wrong it will return
        `None` so that appropriate action can be taken.
        NTú|Fr   )
r$   r   rf   r’   Úsplitrg   r%   ÚtimeÚPIN_TIMErs   )rL   re   ÚvalÚtsZpin_hashr   r   r   rœ   ›  s    
z#DebuggedApplication.check_pin_trustc             C   s*   t j| jdkrdndƒ |  jd7  _d S )NrZ   g      @g      à?r   )r¼   Úsleepr‹   )rL   r   r   r   Ú_fail_pin_auth­  s    z"DebuggedApplication._fail_pin_authc             C   sà   d}d}| j |jƒ}d}|dkr.| jƒ  d}nX|r8d}nN| jdkrHd}n>|jjdƒ}|jƒ jddƒ| jjddƒkr~d| _d}n| jƒ  t	t
j||d	œƒd
d�}|rÌ|j| jdttjƒ ƒt| jƒf dd� n|rÜ|j| jƒ |S )zAuthenticates with the pin.FNTé
   r$   rT   rU   r   )ÚauthÚ	exhaustedzapplication/json)r¤   z%s|%s)Úhttponly)rœ   re   rÁ   r‹   r   rf   r0   r   r$   r¥   r¯   r°   Ú
set_cookier’   rs   r¼   r%   Zdelete_cookie)rL   r§   rÄ   rÃ   ÚtrustÚ
bad_cookieZentered_pinrG   r   r   r   Úpin_auth±  s6    

zDebuggedApplication.pin_authc             C   s2   | j r*| jdk	r*tddƒ tdd| j ƒ tdƒS )zLog the pin if needed.Nr‚   z= * To enable the debugger you need to enter the security pin:z * Debugger pin code: %srU   )rŒ   r$   r   r¥   )rL   r   r   r   Úlog_pin_requestà  s
    z#DebuggedApplication.log_pin_requestc       
      C   sj  t |ƒ}| j}|jjdƒdk�r4|jjdƒ}|jjdƒ}|jjdƒ}| jj|jjdtd�ƒ}| jj|jjdtd�ƒ}	|d	krŽ|rŽ| j||ƒ}n¤|d
kr¶|dk	r¶|| jkr¶| j	||ƒ}n||dkrÔ|| jkrÔ| j
|ƒ}n^|dkrð|| jkrð| jƒ }nB| j�r`|dk	�r`|	dk	�r`| j|k�r`| j|ƒ�r`| j|||	ƒ}n,| j�r`| jdk	�r`|j| jk�r`| j|ƒ}|||ƒS )zDispatch the requests.Z__debugger__ÚyesÚcmdr?   ÚsÚtb)ÚtypeZfrmÚresourcer®   NZpinauthZprintpin)ÚRequestr£   r   rf   r…   rs   r„   r¹   rŠ   r±   rÉ   rÊ   rƒ   rœ   r©   r‡   Úpathr­   )
rL   re   rž   r§   ÚresponserÌ   ÚargrŠ   r    r¡   r   r   r   Ú__call__é  s6    



zDebuggedApplication.__call__)Fr~   r   NFNTT)rO   rP   rQ   rR   rN   r�   r‘   Úpropertyr$   r’   r£   r©   r­   r±   r¹   rœ   rÁ   rÉ   rÊ   rÕ   r   r   r   r   r}   ç   s0          
%
1/	r}   i  i€Q i€:	 )-rR   rj   r!   r¯   r¶   rd   r³   r4   rm   r¼   rp   Ú	itertoolsr   Úos.pathr   r   Ú_compatr   Z	_internalr   Úhttpr   Úsecurityr	   Zwrappersr
   rÑ   r   r¥   rJ   r   Úreprr   r   Ztbtoolsr   r   r½   r%   rF   rH   ÚobjectrI   r|   r}   r   r   r   r   Ú<module>
   s>   K
S