Skip to content

Commit

Permalink
Fixing some UTF8 encoding issues in file names
Browse files Browse the repository at this point in the history
The names of skipped files were not being encoded properly in
output reports.

Change-Id: I38055512d71b3268b5241d50f1aa01a4b28ed332
Closes-Bug: #1647925
  • Loading branch information
Timothy Kelsey authored and Travis McPeak committed Jan 6, 2017
1 parent 0acf9f9 commit 17c737a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
10 changes: 10 additions & 0 deletions bandit/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def __init__(self, config, agg_type, debug=False, verbose=False,
self.progress = b_constants.progress_increment
self.scores = []

def get_skipped(self):
ret = []
# "skip" is a tuple of name and reason, decode just the name
for skip in self.skipped:
if isinstance(skip[0], bytes):
ret.append((skip[0].decode('utf-8'), skip[1]))
else:
ret.append(skip)
return ret

def get_issue_list(self,
sev_level=b_constants.LOW,
conf_level=b_constants.LOW):
Expand Down
7 changes: 6 additions & 1 deletion bandit/core/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ def run_tests(self, raw_context, checktype):
if (result is not None and
result.lineno not in self.nosec_lines and
temp_context['lineno'] not in self.nosec_lines):
result.fname = temp_context['filename']

if isinstance(temp_context['filename'], bytes):
result.fname = temp_context['filename'].decode('utf-8')
else:
result.fname = temp_context['filename']

if result.lineno is None:
result.lineno = temp_context['lineno']
result.linerange = temp_context['linerange']
Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):

# build the skipped string to insert in the report
skipped_str = ''.join('%s <b>reason:</b> %s<br>' % (fname, reason)
for fname, reason in manager.skipped)
for fname, reason in manager.get_skipped())
if skipped_str:
skipped_text = skipped_block.format(files_list=skipped_str)
else:
Expand Down
2 changes: 1 addition & 1 deletion bandit/formatters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
'''

machine_output = {'results': [], 'errors': []}
for (fname, reason) in manager.skipped:
for (fname, reason) in manager.get_skipped():
machine_output['errors'].append({'filename': fname,
'reason': reason})

Expand Down
5 changes: 3 additions & 2 deletions bandit/formatters/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
(manager.metrics.data['_totals']['nosec']))

bits.append(get_metrics(manager))
bits.append(header("Files skipped (%i):", len(manager.skipped)))
bits.extend(["\t%s (%s)" % skip for skip in manager.skipped])
skipped = manager.get_skipped()
bits.append(header("Files skipped (%i):", len(skipped)))
bits.extend(["\t%s (%s)" % skip for skip in skipped])
do_print(bits)

if fileobj.name != sys.stdout.name:
Expand Down
5 changes: 3 additions & 2 deletions bandit/formatters/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ def report(manager, fileobj, sev_level, conf_level, lines=-1):
bits.append('\tTotal lines skipped (#nosec): %i' %
(manager.metrics.data['_totals']['nosec']))

skipped = manager.get_skipped()
bits.append(get_metrics(manager))
bits.append("Files skipped (%i):" % len(manager.skipped))
bits.extend(["\t%s (%s)" % skip for skip in manager.skipped])
bits.append("Files skipped (%i):" % len(skipped))
bits.extend(["\t%s (%s)" % skip for skip in skipped])
result = '\n'.join([bit for bit in bits]) + '\n'

with fileobj:
Expand Down

0 comments on commit 17c737a

Please sign in to comment.