diff --git a/lib/salus/scanners/bandit.rb b/lib/salus/scanners/bandit.rb index 70f3d24a..c6169681 100644 --- a/lib/salus/scanners/bandit.rb +++ b/lib/salus/scanners/bandit.rb @@ -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: @@ -23,17 +35,17 @@ 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 @@ -41,15 +53,16 @@ def run 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 diff --git a/lib/sarif/bandit_sarif.rb b/lib/sarif/bandit_sarif.rb index ea365e76..1d019ee3 100644 --- a/lib/sarif/bandit_sarif.rb +++ b/lib/sarif/bandit_sarif.rb @@ -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 diff --git a/spec/fixtures/sarifs/diff/git_diff_yarn.txt b/spec/fixtures/sarifs/diff/git_diff_yarn.txt deleted file mode 100644 index 10815378..00000000 --- a/spec/fixtures/sarifs/diff/git_diff_yarn.txt +++ /dev/null @@ -1,36 +0,0 @@ -diff --git a/yarn.lock b/yarn.lock -index 06e7d3ba9ef..e3f8fbb5889 100644 ---- a/yarn.lock -+++ b/yarn.lock -@@ -10599,10 +10599,10 @@ base64-arraybuffer@^0.2.0: - resolved "https://registry-npm.com/base64-arraybuffer/-/base64-arraybuffer-0.2.0.tgz#4b944fac0191aa5907afe2d8c999ccc57ce80f45" - integrity sha512-7emyCsu1/xiBXgQZrscw/8KPRT44I4Yq9Pe6EGs3aPRTsWuggML1/1DTuZUuIaJPIm1FTDUVXl4x/yW8s0kQDQ== - --jspdf@2.5.1: -- version "2.5.1" -- resolved "https://registry.yarnpkg.com/jspdf/-/jspdf-2.5.1.tgz#00c85250abf5447a05f3b32ab9935ab4a56592cc" -- integrity sha512-hXObxz7ZqoyhxET78+XR34Xu2qFGrJJ2I2bE5w4SM8eFaFEkW2xcGRVUss360fYelwRSid/jT078kbNvmoW0QA== -+jspdf@2.3.1: -+ version "2.3.1" -+ resolved "https://registry-npm.com/jspdf/-/jspdf-2.3.1.tgz#313d117234b546469694a1fd81a1e02411647576" -+ integrity sha512-1vp0USP1mQi1h7NKpwxjFgQkJ5ncZvtH858aLpycUc/M+r/RpWJT8PixAU7Cw/3fPd4fpC8eB/Bj42LnsR21YQ== - dependencies: -- "@babel/runtime" "^7.14.0" - atob "^2.1.2" - btoa "^1.2.1" - fflate "^0.4.8" -@@ -31897,12 +31889,12 @@ text-encoding@0.7.0: - resolved "https://registry-npm.com/text-encoding/-/text-encoding-0.7.0.tgz#f895e836e45990624086601798ea98e8f36ee643" - integrity sha512-oJQ3f1hrOnbRLOcwKz0Liq2IcrvDeZRHXhd9RgLrsT+DjWY/nty1Hi7v3dtkaEYbPYe0mUoOfzRrMwfXXwgPUA== - --text-segmentation@^1.0.2, text-segmentation@^1.0.3: -- version "1.0.3" -- resolved "https://registry.yarnpkg.com/text-segmentation/-/text-segmentation-1.0.3.tgz#52a388159efffe746b24a63ba311b6ac9f2d7943" -- integrity sha512-iOiPUo/BGnZ6+54OsWxZidGCsdU8YbE4PSpdPinp7DeMtUJNJBoJ/ouUSTJjHkh1KntHaltHl/gDs2FC4i5+Nw== -+text-segmentation@^1.0.2: -+ version "1.0.2" -+ resolved "https://registry-npm.com/text-segmentation/-/text-segmentation-1.0.2.tgz#1f828fa14aa101c114ded1bda35ba7dcc17c9858" -+ integrity sha512-uTqvLxdBrVnx/CFQOtnf8tfzSXFm+1Qxau7Xi54j4OPTZokuDOX8qncQzrg2G8ZicAMOM8TgzFAYTb+AqNO4Cw== - dependencies: -- utrie "^1.0.2" -+ utrie "^1.0.1"