<s> import ast <EOL> import sys <EOL> import bandit <EOL> from bandit . core import test_properties as test <EOL> candidates = set ( [ "<STR_LIT:password>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] ) <EOL> def _report ( value ) : <EOL> return bandit . Issue ( <EOL> severity = bandit . LOW , <EOL> confidence = bandit . MEDIUM , <EOL> text = ( "<STR_LIT>" % value ) ) <EOL> @ test . checks ( '<STR_LIT>' ) <EOL> @ test . test_id ( '<STR_LIT>' ) <EOL> def hardcoded_password_string ( context ) : <EOL> """<STR_LIT>""" <EOL> node = context . node <EOL> if isinstance ( node . parent , ast . Assign ) : <EOL> for targ in node . parent . targets : <EOL> if isinstance ( targ , ast . Name ) and targ . id in candidates : <EOL> return _report ( node . s ) <EOL> elif isinstance ( node . parent , ast . Index ) and node . s in candidates : <EOL> assign = node . parent . parent . parent <EOL> if isinstance ( assign , ast . Assign ) and isinstance ( assign . value , <EOL> ast . Str ) : <EOL> return _report ( assign . value . s ) <EOL> elif isinstance ( node . parent , ast . Compare ) : <EOL> comp = node . parent <EOL> if isinstance ( comp . left , ast . Name ) and comp . left . id in candidates : <EOL> if isinstance ( comp . comparators [ <NUM_LIT:0> ] , ast . Str ) : <EOL> return _report ( comp . comparators [ <NUM_LIT:0> ] . s ) <EOL> @ test . checks ( '<STR_LIT>' ) <EOL> @ test . test_id ( '<STR_LIT>' ) <EOL> def hardcoded_password_funcarg ( context ) : <EOL> """<STR_LIT>""" <EOL> for kw in context . node . keywords : <EOL> if isinstance ( kw . value , ast . Str ) and kw . arg in candidates : <EOL> return _report ( kw . value . s ) <EOL> @ test . checks ( '<STR_LIT>' ) <EOL> @ test . test_id ( '<STR_LIT>' ) <EOL> def hardcoded_password_default ( context ) : <EOL> """<STR_LIT>""" <EOL> defs = [ None ] * ( len ( context . node . args . args ) - <EOL> len ( context . node . args . defaults ) ) <EOL> defs . extend ( context . node . args . defaults ) <EOL> for key , val in zip ( context . node . args . args , defs ) : <EOL> if isinstance ( key , ast . Name ) : <EOL> check = key . arg if sys . version_info . major > <NUM_LIT:2> else key . id <EOL> if isinstance ( val , ast . Str ) and check in candidates : <EOL> return _report ( val . s ) </s>
