Skip to content

Commit

Permalink
Fixing bandit output
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaostrom-cb committed Mar 23, 2023
1 parent e927762 commit 03f76ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
25 changes: 19 additions & 6 deletions lib/salus/scanners/bandit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ def self.scanner_type
Salus::ScannerTypes::SAST
end

def clean_output(str)
encoding_options = {
:invalid => :replace, # Replace invalid byte sequences
:undef => :replace, # Replace anything not defined in ASCII
:replace => '', # Use a blank for those replacements
:universal_newline => true # Always break lines with \n
}

str.encode(Encoding.find('ASCII'), **encoding_options)
end

def run
# bandit compiled with python3
copts = config_options

shell_return = run_shell("bandit #{copts} -r -f json .", chdir: @repository.path_to_repo)


# From the Bandit docs:
#
# Bandit has the following behavior that we will track:
Expand All @@ -23,33 +35,34 @@ def run
# - bandit internal error - exit 2 and log to STDERR

if shell_return.success?
errs = JSON.parse(shell_return.stdout)['errors']
errs = JSON.parse(clean_output(shell_return.stdout))['errors']
if !errs.empty?
report_error(errs, status: shell_return.status)
report_stderr(errs)
return report_failure
elsif JSON.parse(shell_return.stdout)['metrics']['_totals']['loc'].zero?
elsif JSON.parse(clean_output(shell_return.stdout))['metrics']['_totals']['loc'].zero?
report_error(
'0 lines of code were scanned',
status: shell_return.status
)
report_stderr(shell_return.stderr)
report_stderr(clean_output(shell_return.stderr))
return report_failure
else
return report_success
end
end

if shell_return.status == 1
cleaned = clean_output(shell_return.stdout)
report_failure
report_stdout(shell_return.stdout)
log(shell_return.stdout)
report_stdout(cleaned)
log(cleaned)
else
report_error(
"bandit exited with an unexpected exit status, #{shell_return.stderr}",
status: shell_return.status
)
report_stderr(shell_return.stderr)
report_stderr(clean_output(shell_return.stderr))
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/sarif/bandit_sarif.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def parse_scan_report!
logs = @scan_report.log('')
return [] if logs.strip.empty?

encoding_options = {
:invalid => :replace, # Replace invalid byte sequences
:undef => :replace, # Replace anything not defined in ASCII
:replace => '', # Use a blank for those replacements
:universal_newline => true # Always break lines with \n
}
logs = logs.encode(Encoding.find('ASCII'), **encoding_options).sub!(/.*?{/m,'{')

parsed_result = JSON.parse(logs)
parsed_result['results'].concat(parsed_result['errors'])
rescue JSON::ParserError => e
Expand Down
36 changes: 0 additions & 36 deletions spec/fixtures/sarifs/diff/git_diff_yarn.txt

This file was deleted.

0 comments on commit 03f76ae

Please sign in to comment.