<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import sys <EOL> import uuid <EOL> import json <EOL> import time <EOL> import getpass <EOL> import hashlib <EOL> import mimetypes <EOL> from itertools import chain <EOL> from os . path import join , dirname , basename , isfile <EOL> from werkzeug . wrappers import BaseRequest as Request , BaseResponse as Response <EOL> from werkzeug . http import parse_cookie <EOL> from werkzeug . debug . tbtools import get_current_traceback , render_console_html <EOL> from werkzeug . debug . console import Console <EOL> from werkzeug . security import gen_salt <EOL> from werkzeug . _internal import _log <EOL> from werkzeug . _compat import text_type <EOL> from werkzeug . debug . repr import debug_repr <EOL> PIN_TIME = <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> <EOL> def hash_pin ( pin ) : <EOL> if isinstance ( pin , text_type ) : <EOL> pin = pin . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> return hashlib . md5 ( pin + b'<STR_LIT>' ) . hexdigest ( ) [ : <NUM_LIT:12> ] <EOL> _machine_id = None <EOL> def get_machine_id ( ) : <EOL> global _machine_id <EOL> rv = _machine_id <EOL> if rv is not None : <EOL> return rv <EOL> def _generate ( ) : <EOL> for filename in '<STR_LIT>' , '<STR_LIT>' : <EOL> try : <EOL> with open ( filename , '<STR_LIT:rb>' ) as f : <EOL> return f . readline ( ) . strip ( ) <EOL> except IOError : <EOL> continue <EOL> from subprocess import Popen , PIPE <EOL> try : <EOL> dump = Popen ( [ '<STR_LIT>' , '<STR_LIT:-c>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:2>' ] , <EOL> stdout = PIPE ) . communicate ( ) [ <NUM_LIT:0> ] <EOL> match = re . search ( b'<STR_LIT>' , dump ) <EOL> if match is not None : <EOL> return match . group ( <NUM_LIT:1> ) <EOL> except OSError : <EOL> pass <EOL> wr = None <EOL> try : <EOL> import winreg as wr <EOL> except ImportError : <EOL> try : <EOL> import _winreg as wr <EOL> except ImportError : <EOL> pass <EOL> if wr is not None : <EOL> try : <EOL> with wr . OpenKey ( wr . HKEY_LOCAL_MACHINE , <EOL> '<STR_LIT>' , <NUM_LIT:0> , <EOL> wr . KEY_READ | wr . KEY_WOW64_64KEY ) as rk : <EOL> return wr . QueryValueEx ( rk , '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> except WindowsError : <EOL> pass <EOL> _machine_id = rv = _generate ( ) <EOL> return rv <EOL> class _ConsoleFrame ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , namespace ) : <EOL> self . console = Console ( namespace ) <EOL> self . id = <NUM_LIT:0> <EOL> def get_pin_and_cookie_name ( app ) : <EOL> """<STR_LIT>""" <EOL> pin = os . environ . get ( '<STR_LIT>' ) <EOL> rv = None <EOL> num = None <EOL> if pin == '<STR_LIT>' : <EOL> return None , None <EOL> if pin is not None and pin . replace ( '<STR_LIT:->' , '<STR_LIT>' ) . isdigit ( ) : <EOL> if '<STR_LIT:->' in pin : <EOL> rv = pin <EOL> else : <EOL> num = pin <EOL> modname = getattr ( app , '<STR_LIT>' , <EOL> getattr ( app . __class__ , '<STR_LIT>' ) ) <EOL> try : <EOL> username = getpass . getuser ( ) <EOL> except ImportError : <EOL> username = None <EOL> mod = sys . modules . get ( modname ) <EOL> probably_public_bits = [ <EOL> username , <EOL> modname , <EOL> getattr ( app , '<STR_LIT>' , getattr ( app . __class__ , '<STR_LIT>' ) ) , <EOL> getattr ( mod , '<STR_LIT>' , None ) , <EOL> ] <EOL> private_bits = [ <EOL> str ( uuid . getnode ( ) ) , <EOL> get_machine_id ( ) , <EOL> ] <EOL> h = hashlib . md5 ( ) <EOL> for bit in chain ( probably_public_bits , private_bits ) : <EOL> if not bit : <EOL> continue <EOL> if isinstance ( bit , text_type ) : <EOL> bit = bit . encode ( '<STR_LIT:utf-8>' ) <EOL> h . update ( bit ) <EOL> h . update ( b'<STR_LIT>' ) <EOL> cookie_name = '<STR_LIT>' + h . hexdigest ( ) [ : <NUM_LIT:20> ] <EOL> if num is None : <EOL> h . update ( b'<STR_LIT>' ) <EOL> num = ( '<STR_LIT>' % int ( h . hexdigest ( ) , <NUM_LIT:16> ) ) [ : <NUM_LIT:9> ] <EOL> if rv is None : <EOL> for group_size in <NUM_LIT:5> , <NUM_LIT:4> , <NUM_LIT:3> : <EOL> if len ( num ) % group_size == <NUM_LIT:0> : <EOL> rv = '<STR_LIT:->' . join ( num [ x : x + group_size ] . rjust ( group_size , '<STR_LIT:0>' ) <EOL> for x in range ( <NUM_LIT:0> , len ( num ) , group_size ) ) <EOL> break <EOL> else : <EOL> rv = num <EOL> return rv , cookie_name <EOL> class DebuggedApplication ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , evalex = False , request_key = '<STR_LIT>' , <EOL> console_path = '<STR_LIT>' , console_init_func = None , <EOL> show_hidden_frames = False , lodgeit_url = None , <EOL> pin_security = True , pin_logging = True ) : <EOL> if lodgeit_url is not None : <EOL> from warnings import warn <EOL> warn ( DeprecationWarning ( '<STR_LIT>' ) ) <EOL> if not console_init_func : <EOL> console_init_func = None <EOL> self . app = app <EOL> self . evalex = evalex <EOL> self . frames = { } <EOL> self . tracebacks = { } <EOL> self . request_key = request_key <EOL> self . console_path = console_path <EOL> self . console_init_func = console_init_func <EOL> self . show_hidden_frames = show_hidden_frames <EOL> self . secret = gen_salt ( <NUM_LIT:20> ) <EOL> self . _failed_pin_auth = <NUM_LIT:0> <EOL> self . pin_logging = pin_logging <EOL> if pin_security : <EOL> if os . environ . get ( '<STR_LIT>' ) == '<STR_LIT:true>' and pin_logging : <EOL> _log ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . pin is None : <EOL> _log ( '<STR_LIT>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> _log ( '<STR_LIT:info>' , '<STR_LIT>' % self . pin ) <EOL> else : <EOL> self . pin = None <EOL> def _get_pin ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _pin , self . _pin_cookie = get_pin_and_cookie_name ( self . app ) <EOL> return self . _pin <EOL> def _set_pin ( self , value ) : <EOL> self . _pin = value <EOL> pin = property ( _get_pin , _set_pin ) <EOL> del _get_pin , _set_pin <EOL> @ property <EOL> def pin_cookie_name ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _pin , self . _pin_cookie = get_pin_and_cookie_name ( self . app ) <EOL> return self . _pin_cookie <EOL> def debug_application ( self , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> app_iter = None <EOL> try : <EOL> app_iter = self . app ( environ , start_response ) <EOL> for item in app_iter : <EOL> yield item <EOL> if hasattr ( app_iter , '<STR_LIT>' ) : <EOL> app_iter . close ( ) <EOL> except Exception : <EOL> if hasattr ( app_iter , '<STR_LIT>' ) : <EOL> app_iter . close ( ) <EOL> traceback = get_current_traceback ( <EOL> skip = <NUM_LIT:1> , show_hidden_frames = self . show_hidden_frames , <EOL> ignore_system_exceptions = True ) <EOL> for frame in traceback . frames : <EOL> self . frames [ frame . id ] = frame <EOL> self . tracebacks [ traceback . id ] = traceback <EOL> try : <EOL> start_response ( '<STR_LIT>' , [ <EOL> ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:0>' ) , <EOL> ] ) <EOL> except Exception : <EOL> environ [ '<STR_LIT>' ] . write ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> is_trusted = bool ( self . check_pin_trust ( environ ) ) <EOL> yield traceback . render_full ( evalex = self . evalex , <EOL> evalex_trusted = is_trusted , <EOL> secret = self . secret ) . encode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) <EOL> traceback . log ( environ [ '<STR_LIT>' ] ) <EOL> def execute_command ( self , request , command , frame ) : <EOL> """<STR_LIT>""" <EOL> return Response ( frame . console . eval ( command ) , mimetype = '<STR_LIT>' ) <EOL> def display_console ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if <NUM_LIT:0> not in self . frames : <EOL> if self . console_init_func is None : <EOL> ns = { } <EOL> else : <EOL> ns = dict ( self . console_init_func ( ) ) <EOL> ns . setdefault ( '<STR_LIT>' , self . app ) <EOL> self . frames [ <NUM_LIT:0> ] = _ConsoleFrame ( ns ) <EOL> is_trusted = bool ( self . check_pin_trust ( request . environ ) ) <EOL> return Response ( render_console_html ( secret = self . secret , <EOL> evalex_trusted = is_trusted ) , <EOL> mimetype = '<STR_LIT>' ) <EOL> def paste_traceback ( self , request , traceback ) : <EOL> """<STR_LIT>""" <EOL> rv = traceback . paste ( ) <EOL> return Response ( json . dumps ( rv ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def get_resource ( self , request , filename ) : <EOL> """<STR_LIT>""" <EOL> filename = join ( dirname ( __file__ ) , '<STR_LIT>' , basename ( filename ) ) <EOL> if isfile ( filename ) : <EOL> mimetype = mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] or '<STR_LIT>' <EOL> f = open ( filename , '<STR_LIT:rb>' ) <EOL> try : <EOL> return Response ( f . read ( ) , mimetype = mimetype ) <EOL> finally : <EOL> f . close ( ) <EOL> return Response ( '<STR_LIT>' , status = <NUM_LIT> ) <EOL> def check_pin_trust ( self , environ ) : <EOL> """<STR_LIT>""" <EOL> if self . pin is None : <EOL> return True <EOL> val = parse_cookie ( environ ) . get ( self . pin_cookie_name ) <EOL> if not val or '<STR_LIT:|>' not in val : <EOL> return False <EOL> ts , pin_hash = val . split ( '<STR_LIT:|>' , <NUM_LIT:1> ) <EOL> if not ts . isdigit ( ) : <EOL> return False <EOL> if pin_hash != hash_pin ( self . pin ) : <EOL> return None <EOL> return ( time . time ( ) - PIN_TIME ) < int ( ts ) <EOL> def _fail_pin_auth ( self ) : <EOL> time . sleep ( self . _failed_pin_auth > <NUM_LIT:5> and <NUM_LIT> or <NUM_LIT:0.5> ) <EOL> self . _failed_pin_auth += <NUM_LIT:1> <EOL> def pin_auth ( self , request ) : <EOL> """<STR_LIT>""" <EOL> exhausted = False <EOL> auth = False <EOL> trust = self . check_pin_trust ( request . environ ) <EOL> bad_cookie = False <EOL> if trust is None : <EOL> self . _fail_pin_auth ( ) <EOL> bad_cookie = True <EOL> elif trust : <EOL> auth = True <EOL> elif self . _failed_pin_auth > <NUM_LIT:10> : <EOL> exhausted = True <EOL> else : <EOL> entered_pin = request . args . get ( '<STR_LIT>' ) <EOL> if entered_pin . strip ( ) . replace ( '<STR_LIT:->' , '<STR_LIT>' ) == self . pin . replace ( '<STR_LIT:->' , '<STR_LIT>' ) : <EOL> self . _failed_pin_auth = <NUM_LIT:0> <EOL> auth = True <EOL> else : <EOL> self . _fail_pin_auth ( ) <EOL> rv = Response ( json . dumps ( { <EOL> '<STR_LIT>' : auth , <EOL> '<STR_LIT>' : exhausted , <EOL> } ) , mimetype = '<STR_LIT:application/json>' ) <EOL> if auth : <EOL> rv . set_cookie ( self . pin_cookie_name , '<STR_LIT>' % ( <EOL> int ( time . time ( ) ) , <EOL> hash_pin ( self . pin ) <EOL> ) , httponly = True ) <EOL> elif bad_cookie : <EOL> rv . delete_cookie ( self . pin_cookie_name ) <EOL> return rv <EOL> def log_pin_request ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . pin_logging and self . pin is not None : <EOL> _log ( '<STR_LIT:info>' , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> _log ( '<STR_LIT:info>' , '<STR_LIT>' % self . pin ) <EOL> return Response ( '<STR_LIT>' ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> request = Request ( environ ) <EOL> response = self . debug_application <EOL> if request . args . get ( '<STR_LIT>' ) == '<STR_LIT:yes>' : <EOL> cmd = request . args . get ( '<STR_LIT>' ) <EOL> arg = request . args . get ( '<STR_LIT:f>' ) <EOL> secret = request . args . get ( '<STR_LIT:s>' ) <EOL> traceback = self . tracebacks . get ( request . args . get ( '<STR_LIT>' , type = int ) ) <EOL> frame = self . frames . get ( request . args . get ( '<STR_LIT>' , type = int ) ) <EOL> if cmd == '<STR_LIT>' and arg : <EOL> response = self . get_resource ( request , arg ) <EOL> elif cmd == '<STR_LIT>' and traceback is not None and secret == self . secret : <EOL> response = self . paste_traceback ( request , traceback ) <EOL> elif cmd == '<STR_LIT>' and secret == self . secret : <EOL> response = self . pin_auth ( request ) <EOL> elif cmd == '<STR_LIT>' and secret == self . secret : <EOL> response = self . log_pin_request ( ) <EOL> elif self . evalex and cmd is not None and frame is not None and self . secret == secret and self . check_pin_trust ( environ ) : <EOL> response = self . execute_command ( request , cmd , frame ) <EOL> elif self . evalex and self . console_path is not None and request . path == self . console_path : <EOL> response = self . display_console ( request ) <EOL> return response ( environ , start_response ) </s>
